Dropping foreign key using variable as name

后端 未结 3 1366
迷失自我
迷失自我 2021-01-23 03:36

I use the following:

DECLARE @ConstraintName varchar(255);
SELECT @ConstraintName = CONSTRAINT_NAME 
FROM 
   INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE 
WHERE 
         


        
3条回答
  •  后悔当初
    2021-01-23 04:02

    DECLARE @constraintName VARCHAR(50);
    DECLARE @runString VARCHAR(2000);
    
    select @constraintName = CONSTRAINT_NAME
    from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
    WHERE TABLE_NAME = 'TheTable' AND COLUMN_NAME = 'TheColumn';
    
    
    SET @runString = CONCAT('ALTER TABLE TheTable DROP CONSTRAINT ', @constraintName);
    PREPARE stmt1 FROM @runString ;
    EXECUTE stm1;
    DEALLOCATE PREPARE stmt1;
    

提交回复
热议问题