SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ADD A COLUMN TO A TABLE IN T-SQL


ADD A COLUMN TO A TABLE IN T-SQL

Author
Message
Harry Drenth
Harry Drenth
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 181
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
Adi Cohn
Adi Cohn
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7745 Visits: 6594
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/
Harry Drenth
Harry Drenth
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 181
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 ]

dharris-1058198
dharris-1058198
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 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.
vimal_panchal
vimal_panchal
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 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?
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63933 Visits: 18570
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

Atif-ullah Sheikh
Atif-ullah Sheikh
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4994 Visits: 5204
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


shashi24365
shashi24365
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 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....
dmoore2168
dmoore2168
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 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



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search