第二个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包含哪些内容)?