Link to home
Start Free TrialLog in
Avatar of muskad202
muskad202

asked on

determine default constraint on a col and delete it

Hi!

I'll be writing a script, where given a col name, I need to determine the name of the default constraint (if any) that exists on it, and delete it. At present, I am using the foll query to determine the constraint name:


select t_obj.name as TABLE_NAME
,c_obj.name as CONSTRAINT_NAME
,col.name as COLUMN_NAME
from
sysobjects c_obj
join sysobjects t_obj on c_obj.parent_obj = t_obj.id
join sysconstraints con on c_obj.id = con.constid
join syscolumns col on t_obj.id = col.id and con.colid = col.colid
where t_obj.name = '<table name>'
and c_obj.xtype = 'D'
and col.name = '<column name>'

if i was running this manually from the query analyzer, then what i could do is, run it, then see which constraints are returned, and then maybe run another "ALTER TABLE DROP CONSTRAINT" query ... but from a script, i cant do that since i dont know the name of the constraint unless i "look" at the results returned

can anyone help me?

thanks :)
muskad202
Avatar of Ved Prakash Agrawal
Ved Prakash Agrawal
Flag of India image

HI tried to place the result  into temp table and use the while loop and place the alter command inside loop.

you can dynamic query place inside while loop
and then you can place logic inside that


ved
Avatar of muskad202
muskad202

ASKER

Hi!

can u tell me the syntax for using a while loop in TSQL?

thanks,
muskad202
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial