The truth: I am not good at SQL, and after some research I am unable to figure this out. I am reading a chapter on utilizing views and am unable to get this query to work :
USE [jm0235242]
CREATE VIEW InvoiceBasic
AS
SELECT VendorName, InvoiceNumber, InvoiceTotal
FROM Vendors JOIN Invoices ON Vendors.VendorID = Invoices.VendorID
It has issues with my USE statement:
‘CREATE VIEW’ must be the first statement in a query batch”.
My question is, how am I supposed to specify which database it is supposed to look at if I cannot use my USE statement. Thanks for any help, and happy thanksgiving!
You need to make sure that the
CREATE VIEWis the first statement in a SQL batch – as the error clearly explains. So use this:When you run this in SQL Server Management Studio, the
GOkeyword ends a batch and executes it – and theCREATE VIEWis now the first statement in the second batch, and it’s happy and works, tooUpdate: the question why exactly the
CREATE VIEW(and alsoALTER VIEW, for that matter) must be the first statement of a batch eludes me – I’ve never heard any compelling explanation for this. Maybe a Microsoftie or someone close to the team knows this and can explain for us all? 🙂