incorrect syntax error CREATE VIEW must be the only statement in the batch

Many useful statements in SQL Server have to be at the beginning of a batch. These include create triggercreate stored procedure, and many others.

What is a batch? It is basically a unit of compilation. It also limits the scope of local variables. I advise you to read about batches in the documentation.

The simplest way to start a batch is to use GO. Sometimes, you might want to put a statement in the middle of a block of code and you cannot use GO (say, in the body of a stored procedure). This that case, you would use dynamic SQL instead.

Leave a Comment