本文介绍了一种方法来检查SQL 2005中是否存在外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
您可以使用以下脚本: / p>
IF EXISTS(SELECT *
FROM sys.foreign_keys
WHERE object_id = OBJECT_ID(N'[dbo] [FK_NAME]')
AND parent_object_id = OBJECT_ID(N'[dbo]。[MyTable]'))
BEGIN
- do stuff
END
这可以通过展开表格并右键单击现有的FK并选择脚本键作为DROP TO然后你会从SQL获得一个生成的脚本。
Is there an easy way to check if a foreign key exists for a column in a table? I am writing a script which will add the foreign key only if it does not exist.
You can use this script:
IF EXISTS (SELECT *
FROM sys.foreign_keys
WHERE object_id = OBJECT_ID(N'[dbo].[FK_NAME]')
AND parent_object_id = OBJECT_ID(N'[dbo].[MyTable]'))
BEGIN
-- do stuff
END
This can be done if you expand out the table and right click on an existing FK and choose script key as "DROP TO" and then you will get a generated script from SQL.
这篇关于一种方法来检查SQL 2005中是否存在外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!