Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

ADD A COLUMN TO A TABLE IN T-SQL Expand / Collapse
Author
Message
Posted Monday, February 23, 2009 3:13 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, September 15, 2014 1:43 AM
Points: 16, Visits: 167
I want to add a column to a table in T-SQL by using the following command:

ALTER TABLE DELIVERYINTRIP ADD PODID UNIQUEIDENTIFIER NULL

This works the first time when the column doesn't exist yet.

However I want to prevent the alter table command to be executed in case the column already exists.

I can test if it exists with the following command:

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'DBID_TRIP'
AND COLUMN_NAME = 'SESSIONID'


How do I combine the test and the alter table command in T-SQL for SQL CE since BEGIN and END statements are not supported?

My solution would be:

IF NOT EXISTS (SELECT *
WHERE NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'DBID_TRIP'
AND COLUMN_NAME = 'SESSIONID')
BEGIN
ALTER TABLE DELIVERYINTRIP ADD PODID UNIQUEIDENTIFIER NULL
END

Anyone an idea or solution in T-SQL.

Regards,

Harry Drenth
Post #662439
Posted Monday, February 23, 2009 3:50 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 16, 2014 1:05 AM
Points: 2,112, Visits: 5,489
I don’t have SQL Server CE edition, so I can’t check it, but why do you think that you need to use begin end block? Can’t you just use this code?

IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'DELIVERYINTRIP'
AND COLUMN_NAME = 'PODID')
ALTER TABLE DELIVERYINTRIP ADD PODID UNIQUEIDENTIFIER NULL



Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #662449
Posted Wednesday, February 25, 2009 7:23 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, September 15, 2014 1:43 AM
Points: 16, Visits: 167
hi adi,

This is the result:

Major Error 0x80040E14, Minor Error 25501
> IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'DELIVERYINTRIP'
AND COLUMN_NAME = 'PODID')
ALTER TABLE DELIVERYINTRIP ADD PODID UNIQUEIDENTIFIER NULL
There was an error parsing the query. [ Token line number = 1,Token line offset = 1,Token in error = IF ]
Post #664226
Posted Thursday, April 2, 2009 6:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, December 18, 2009 1:14 PM
Points: 4, Visits: 11
From "SQL Server 2005 Compact Edition Books Online"

"Queries that can typically be run on SQL Server Compact Edition can also run on Microsoft SQL Server; however, many of the features of Transact-SQL are absent from SQL Server Compact Edition, and only a single SQL statement can be executed in a command.
...
Note:
SQL Server Compact Edition does not support scripting because scripting depends on SQL Management Objects (SMO). SMO is not a feature of SQL Server Compact Edition."


Technically, an IF ... THEN ... construct is two statements (the IF evaluation and the THEN action). Also, while "IF" and "THEN" are reserved words in SSCE (for compatibility with the rest of the SQL world), they are not defined in the grammar available for SSCE.
Post #688758
Posted Thursday, April 1, 2010 12:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 23, 2010 8:15 AM
Points: 3, Visits: 5
I am facing the same problem, is anybody found any solution for this?
How can we achieve this?

Is there any alternative for this?
Post #894448
Posted Thursday, April 1, 2010 12:21 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 3:06 PM
Points: 17,805, Visits: 15,716
vimal_panchal (4/1/2010)
I am facing the same problem, is anybody found any solution for this?
How can we achieve this?

Is there any alternative for this?



You would be better served by starting a new thread on this.

Post your question with relevant error messages, queries, table structures and sample data if necessary.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #894452
Posted Monday, June 14, 2010 4:46 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 5:48 AM
Points: 3,241, Visits: 5,002
Check this...

http://msdn.microsoft.com/en-us/library/ms174123(SQL.90).aspx

And you can use if condition in your application code. Generate data set from the query to check the column using Information_Schema and take decission at application level code.

Stored Procedures are NOT supported in SQL Server CE.



----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here

Post #936711
Posted Thursday, February 17, 2011 10:15 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 19, 2011 12:12 AM
Points: 2, Visits: 47
when m running this query <>go
IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '[DBO].CUSTOMER'
AND COLUMN_NAME = 'CUSTOMER_ID')
ALTER TABLE [DBO].CUSTOMER ADD CUSTOMER_ID int NULL
go
</>
AND ITS GIVING ERROR:
<>
Msg 2705, Level 16, State 4, Line 5
Column names in each table must be unique. Column name 'CUSTOMER_ID' in table '[DBO].CUSTOMER' is specified more than once.

</>
BUT HERE I WANT "Query executed successfully" is it possible to do this???

please help!!!! thanks in advance....
Post #1066146
Posted Friday, April 1, 2011 10:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 19, 2011 8:20 AM
Points: 4, Visits: 219
Remove the [DBO] from the WHERE table_name = '[DBO].CUSTOMER'

Your IF NOT EXISTS should look like:

IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'CUSTOMER'
AND COLUMN_NAME = 'CUSTOMER_ID')
ALTER TABLE [DBO].CUSTOMER ADD CUSTOMER_ID int NULL



Post #1087584
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse