Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why CREATE VIEW must be first statement in a batch? [closed]

Tags:

sql

sql-server

Is there any logical reason for which CREATE VIEW must be first statement in a batch? What is a rationale behind such design?

like image 480
Artur Krajewski Avatar asked Aug 27 '13 12:08

Artur Krajewski


People also ask

Which statement must be the first in a query batch?

'CREATE VIEW' must be the first statement in a query batch. 'CREATE VIEW' must be the first statement in a query batch. Invalid object name 'playerView'.

Is the CREATE VIEW statement the only statement in the query?

Just as the error says, the CREATE VIEW statement needs to be the only statement in the query batch. I came across this question when I was trying to create a couple of views within the same statement, what worked well for me is using dynamic SQL.

What is the difference between create and execute statements in SQL?

The CREATE statement must start the batch. All other statements that follow in that batch will be interpreted as part of the definition of the first CREATE statement. A table cannot be changed and then the new columns referenced in the same batch. If an EXECUTE statement is the first statement in a batch, the EXECUTE keyword is not required.

How do I use a view in a SELECT statement?

You can use a view in a SELECT statement in exactly the same way as you can use a table. If you wish to create additional views to simplify the queries which follow, include them in this file. */ When I run each CREATE VIEW separately, it seems to run it correctly with no errors.


1 Answers

It's not so much that it must be the first statement in the batch, but rather that it must be the only statement in the batch. For the same reason CREATE PROCEDURE, CREATE FUNCTION, etc. all have to be in their own batch ... they need to be compiled independently of other code. One reason is to ensure that anything in the batch created before the object actually exists when it is created, and anything that refers to the object afterward has something to point to. Another is to help prevent this stuff:

-- some other code here

CREATE PROCEDURE dbo.whatever
AS
  -- procedure code here

-- some other code here

We don't want "some other code here" to be included in the definition of the stored procedure. We also wouldn't know what to do with this:

CREATE PROCEDURE dbo.proc1
AS
  -- some code here
-- a comment here
CREATE PROCEDURE dbo.proc2
AS

Since we can legally place comments before CREATE PROCEDURE, how does the parser know which object the comment belongs to?

And for the nit-picky, the same thing applies to views. (Note that a procedure was used in the example simply because a procedure can contain multiple statements while a view can't; and a procedure with multiple statements is far more common than a view with multiple comments.) But we can demonstrate the same type of problem - ask yourself if you don't separate the two views into their own batches, how do you know which view should own comment4?

-- comment1
CREATE VIEW dbo.view1
AS
  -- comment2
  SELECT 
  -- comment3
  * from sys.objects
  -- comment4
CREATE VIEW dbo.view2
AS
  -- comment5
  SELECT 
  -- comment6
  * from sys.objects

In Management Studio, you work around this by placing a GO between object scripts. Note that GO is not T-SQL, it is an app-specific batch separator; from other interfaces, you will need to transmit your batches separately in other ways.

Again, the answer is in the first sentence: a script to create or alter any module in SQL Server - including views, procedures, functions, and triggers - must be the only statement in the batch.

like image 195
Aaron Bertrand Avatar answered Nov 09 '22 22:11

Aaron Bertrand