在某些列上执行并回滚

时间:2017-10-03 10:00:20

标签: sql sql-server

已编辑:

我执行此程序,它在2列上解除绑定并回滚..有什么问题?

CREATE PROCEDURE MYchange_bindefault AS
DECLARE @TableName sysname,
        @ColumnName sysname,
        @sql nvarchar(4000);

DECLARE ByDefaults CURSOR FAST_FORWARD FOR
select o.name AS TableName,c.name AS ColumnName from sys.columns c
JOIN sys.syscomments t on c.default_object_id = t.id
JOIN sys.objects o ON o.object_id = c.object_id
 where text = 'create default default_modifiedBy as SUSER_SNAME()';
OPEN ByDefaults;
FETCH NEXT FROM ByDefaults INTO @TableName,@ColumnName;
WHILE @@FETCH_STATUS = 0 
BEGIN
    SET @sql = ' sp_unbindefault '''+@TableName+'.'+@ColumnName+ ''';'
    EXEC sys.sp_executesql  @stmt = @sql;
    SET @sql = 'alter table '+ @TableName +' ADD CONSTRAINT modifiedby DEFAULT dbo.ModifiedBy()
 FOR '+ @ColumnName + ';'
    EXEC sys.sp_executesql  @stmt = @sql;
    FETCH NEXT FROM ByDefaults INTO @TableName, @ColumnName;
END
CLOSE ByDefaults;
DEALLOCATE ByDefaults;

0 个答案:

没有答案