Hello,I'm trying to execute a sql file with the following contents using sql cmd.sqlcmd -S localhost\dbInstance -i Sample.sql -v filepath="C:\Sql\"
Sample.sql contents:USE Sample_dbGOBEGIN 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
When I execute the sqlcmd, it throws the following error: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 5Incorrect syntax near the keyword 'VIEW'.
Question:Why am I not able to create view and alter view from sqlcmd, while I'm able to alter table?When I comment out the CREATE VIEW and ALTER VIEW statement, the script executed fine.Thanks!