I was looking this article
and I have just a question how to add IFEXISTS()
in the select
part:
I want to run @command
only if there some rows. I think it will throw an error if I dont check select
part?
declare @table_name nvarchar(256)
declare @Command nvarchar(max) = ''
set @table_name = N'ATableName'
select @Command = @Command + 'ALTER TABLE ' + @table_name + ' drop constraint ' + d.name + CHAR(10)+ CHAR(13)
from sys.tables t
join sys.default_constraints d on d.parent_object_id = t.object_id
join sys.columns c on c.object_id = t.object_id
and c.column_id = d.parent_column_id
where t.name = @table_name and c.name in ('column1','column2','column3')
--print @Command
execute (@Command)