Why does SQL Server keep creating a DF constraint?

前端 未结 3 1745
离开以前
离开以前 2021-01-31 14:31

I\'m trying to create upgrade and backout scripts in SQL. The upgrade script adds a column like so:

IF NOT EXISTS (SELECT * FROM sys.columns WHERE Name = N\'Col         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 15:09

    Run this if you want remove constraint:

    DECLARE @tableName NVARCHAR(255) = '[INSERT]';
    DECLARE @first5CharsFromColumnName NVARCHAR(255) = '[INSERT]';
    DECLARE @name NVARCHAR(255);
    SELECT @name = d.name FROM dbo.sysobjects d
    INNER JOIN dbo.sysobjects t ON t.id = d.parent_obj
    WHERE d.name LIKE '%'+@first5CharsFromColumnName+'%' AND d.type = 'D' AND t.name = @tableName
    
    IF @name IS NOT NULL BEGIN
        EXEC('ALTER TABLE '+@tableName+' DROP CONSTRAINT ' + @name);
    END
    

提交回复
热议问题