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 |
johnsql
Posting Yak Master
161 Posts |
Posted - 2008-01-28 : 15:13:41
|
Buddies, I have a bit-typed column that is defined with default value of 1 ( a contraint). The constraint name of the column is always CHANGED whenever the column is drop and then re-created. So, I'd like to use a dynamic script as below to get dynamic name of the column's constraint name.Anytime, I would like to drop the column, so I must dropped the default-value constraint first for the column. However, I get the error like "Incorrect syntax near '@ctName' "declare @ColumnNameHavingDefaultValue varchar(100), @ctName as varchar(100)set @ColumnNameHavingDefaultValue = 'UserActive'-- find out constraint nameselect @ctName=a.namefrom sysobjects ainner join syscolumns b on a.info= b.colid and b.name=@ColumnNameHavingDefaultValuewhere a.type='D' and a.name like 'DF__WebUser%' and a.parent_obj=(select object_id('WebUser'))GO-- drop constraint --> must change 'name of constraint'ALTER TABLE WebUser DROP @ctName -- ERROR HAPPENS AT THIS LINEGO-- drop columnALTER TABLE WebUser DROP column UserActive |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-01-28 : 15:43:21
|
dynamic sql will help you there.. |
 |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-01-28 : 15:48:26
|
try something like thisdeclare @sql_string varchar(100)set @sql_string='ALTER TABLE WebUser DROP '+@ctName exec @sql_string |
 |
|
|
|
|