案例
目前我正在处理数据库种子脚本,使用sqlcmd
执行。例如,此脚本示例:
IF (SELECT COUNT(*) FROM Genders)=0
BEGIN
PRINT N'Seeding table Genders...'
SET IDENTITY_INSERT Genders ON
GO
INSERT INTO Genders (GenderId, Description) VALUES (0, 'Onbekend');
INSERT INTO Genders (GenderId, Description) VALUES (1, 'Vrouw');
INSERT INTO Genders (GenderId, Description) VALUES (2, 'Man');
INSERT INTO Genders (GenderId, Description) VALUES (3, 'Onzijdig');
INSERT INTO Genders (GenderId, Description) VALUES (4, 'Vrouwman');
INSERT INTO Genders (GenderId, Description) VALUES (5, 'Manvrouw');
SET IDENTITY_INSERT Genders OFF
END
GO
问题
但是,如果我在SQL Server Management Studio中以sqlcmd
模式执行它,它会给我这个错误:
用Google搜索,但无法弄清楚我做错了什么。没有IF / BEGIN / END如果有效,但我喜欢先进行检查。Msg 102,Level 15,State 1,Line 5'ON'附近的语法不正确。
Msg 102,Level 15,State 1,Line 10'END'附近的语法不正确。
问题:
提前致谢!!
答案 0 :(得分:5)
GO
和BEGIN
内不能有END
。试试以下
SET IDENTITY_INSERT Genders ON
IF (SELECT COUNT(*) FROM Genders)=0
BEGIN
PRINT N'Seeding table Genders...'
INSERT INTO Genders ...
END
SET IDENTITY_INSERT Genders OFF
GO