Finding a Primary Key Constraint on the fly in SQL Server 2005 -



Finding a Primary Key Constraint on the fly in SQL Server 2005 -

i have next sql:

alter table dbo.ps_uservariables drop constraint pk_ps_uservariables; alter table dbo.ps_uservariables add together primary key (varnumber, subjectid, userid, datasetid, listid, uservartitle);

since have multiple environments, pk_ps_uservariables constraint name different on different databases. how write script gets name adds script?

while typical best practice explicitly name constraints, can them dynamically catalog views:

declare @table nvarchar(512), @sql nvarchar(max); select @table = n'dbo.ps_uservariables'; select @sql = 'alter table ' + @table + ' drop constraint ' + name + ';' sys.key_constraints [type] = 'pk' , [parent_object_id] = object_id(@table); exec sp_executesql @sql; alter table dbo.ps_uservariables add together constraint ...

sql sql-server sql-server-2005 primary-key

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -