Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Gyte
Starting Member
23 Posts |
Posted - 2007-08-30 : 08:05:03
|
How can you check if a field in a table exists or not with SQL ?I'm trying to add a field to a table with the following query :ALTER TABLE ContactADD CountyCode nvarchar(10) It doesn't however check if the field already exists or not. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-30 : 08:11:19
|
[code]IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMN WHERE TABLE_NAME = 'Contact' AND COLUMN_NAME = 'CountyCode') ALTER TABLE Contact ADD CountyCode nvarchar(10)[/code] E 12°55'05.25"N 56°04'39.16" |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-30 : 08:19:35
|
orIf col_length('table_name','column_name') is nullAlter table.......MadhivananFailing to plan is Planning to fail |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-08-30 : 09:12:41
|
Can you put ALTER TABLE in an IF? Or is it just CREATE TABLE that has to be at the top of a statement block?(Can be got round with EXEC 'ALTER TABLE ...' of course ...)Kristen |
|
|
rcurrey
Starting Member
30 Posts |
Posted - 2007-08-30 : 11:56:24
|
Yes you can have an alter table add column inside the if statement.Thanks,Rich |
|
|
|
|
|