我正在尝试使用 sql cmd 执行具有以下内容的 sql 文件。
sqlcmd -S localhost\dbInstance -i Sample.sql -v filepath="C:\Sql\"
Sample.sql 内容:
USE Sample_db
GO
BEGIN
BEGIN TRANSACTION;
BEGIN TRY
CREATE VIEW [dbo].[Test_View]
AS SELECT * from Sample_table;
ALTER VIEW [dbo].[Sample_View]
AS SELECT * FROM table_9;
ALTER TABLE [Sample_Table_2] ADD Col_4 VARCHAR(20);
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber ,
ERROR_SEVERITY() AS ErrorSeverity ,
ERROR_STATE() AS ErrorState ,
ERROR_PROCEDURE() AS ErrorProcedure ,
ERROR_LINE() AS ErrorLine ,
ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END
GO
当我执行 sqlcmd 时,它会引发以下错误:
C:\Sql>sqlcmd -S localhost\dbInstance -i Sample.sql -v filepath="C:\Sql\"
Changed database context to 'Sample_db'.
Msg 156, Level 15, State 1, Server localhost\dbInstance, Line 5
Incorrect syntax near the keyword 'VIEW'.
问题: 为什么我不能从 sqlcmd 创建视图和更改视图,而我可以更改表?当我注释掉 CREATE VIEW 和 ALTER VIEW 语句时,脚本执行得很好。
谢谢!