Hussein El Sibai
Hussein El Sibai

Reputation: 53

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

OK so here's my code trying to create a view but it keeps on telling me create view must only be the only statement in the batch tried everything need help

CREATE VIEW  [Vendors List] 
AS
    SELECT 
        VendorID, 
        SUM(InvoiceTotal) AS [YTD Invoice Total], 
        SUM(PaymentTotal) AS [YTD Payment Total], 
        SUM(CreditTotal) AS [YTD Credit Total]
    FROM 
        dbo.Invoices
    GROUP BY 
        VendorID

Upvotes: 1

Views: 6262

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

Many useful statements in SQL Server have to be at the beginning of a batch. These include create trigger, create 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.

Upvotes: 2

Related Questions