|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 3:56 AM
Points: 16,
Visits: 146
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 7:46 AM
Points: 2,018,
Visits: 4,913
|
|
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/
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 3:56 AM
Points: 16,
Visits: 146
|
|
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 ]
|
|
|
|
|
Forum 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.
|
|
|
|
|
Forum 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?
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 6:30 PM
Points: 18,733,
Visits: 12,330
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Thursday, March 14, 2013 4:15 AM
Points: 3,240,
Visits: 4,960
|
|
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
|
|
|
|
|
Forum 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....
|
|
|
|
|
Forum 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
|
|
|
|