第二个ALTER TABLE
CHECK CONSTRAINT
应该在一个IF (NOT) EXISTS
以便脚本可以重复执行吗?
IF NOT EXISTS (
SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_NAME ='fk_RoleId'
)
BEGIN
ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [fk_RoleId] FOREIGN KEY([RoleId])
REFERENCES [dbo].[webpages_Roles] ([RoleId])
END
GO
-- Put me in an IF ... () BEGIN ... END?
ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [fk_RoleId]
GO
它不会导致任何错误,但如果它应该在一个IF
子句中,你如何测试它是否已经运行(if 可以包含什么)?