Simple thing … i thought. Create a view and use it later in the same SQL script.
Let’s say we have a script as follows:
CREATE VIEW someView AS (...)
DROP VIEW someView
If I try to parse it SQL Management complaints there’s an error around DROP.
If I execute them separately (create first, then drop) they work both fine.
Is there any way to create a view and use it in a single SQL script?
I could wrap further statements in string an then EXEC it but it’s a bit inconvenient.
Code example was fixed (missing VIEW)
More meaningful example:
create view TEST as (select name from spt_values where number=1);
drop view TEST
Is it possible to execute it at once?
I got the error:
Msg 156, Level 15, State 1, Procedure TEST, Line 2
Incorrect syntax near the keyword 'drop'.
Running create statement separately and then dropping view works perfectly.
Separate your query with
GOkeyword like query bellow: