gpt4 book ai didi

mysql - 使用变量作为名称删除外键

转载 作者:行者123 更新时间:2023-11-29 16:07:57 25 4
gpt4 key购买 nike

我使用以下内容:

DECLARE @ConstraintName varchar(255);
SELECT @ConstraintName = CONSTRAINT_NAME
FROM
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE
TABLE_NAME = 'TheTable'
AND COLUMN_NAME = 'TheColumn';

alter table TheTable drop constraint @ConstraintName;

但是,@ConstraintName 附近的语法不正确。这可能是一件小事,但我无法弄清楚。我应该改变什么才能取消约束?

SQLFiddle:http://sqlfiddle.com/#!2/6709e/3

最佳答案

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;

关于mysql - 使用变量作为名称删除外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55553588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com