-1

I have a table that is the result of an INNER JOIN and then to that table I have to a apply a bunch of queries, so at the end the whole code in SQL is really big and in the future (and that's the main problem) I will have problems to understand what did I do.

So for this reason I am considering the possibility of creating views, so in each step I have a view created and I know what does each one of the queries.

So with that goal I started to use IF ELSE statements to create views if they dont' exist etc. but I'm stumbling with a lot of errors and problems.

First of all, this is the way I'm creating a view with the IF statement:

-- This portion of code is common in all the views
IF NOT EXISTS
(
    SELECT 1
    FROM sys.views
    WHERE Name = 'NAME_OF_THE_VIEW'
)
BEGIN

    EXEC('CREATE VIEW NAME_OF_THE_VIEW AS SELECT 1 as Val')

END

GO
ALTER VIEW NAME_OF_THE_VIEW
AS

-- Here I put the query of the view

SELECT *
FROM table_1

When I execute this code it works, but the SQL Server Management Studio underlines "NAME_OF_THE_VIEW" in the ALTER VIEW statement and when I hover the mouse it says: Invalid object name 'NAME_OF_THE_VIEW'. I don't understand why if there's a supposed error it still works.

The other problem is that when I introduce more code like the code above in order to create other views in the same script, the whole ALTER VIEW statement is underlined and when I hover this message appears; Incorrect syntax: 'ALTER VIEW' must be the only statement in the batch.

So the question is: hoy can I put everything in the same script, where I can create views to avoid doing a lot of subqueries, and without getting this errors? The SQL-Server version is 15.

6
  • Run the SQL... You won't get the error. That's intellisense saying that the VIEW doesn't exist, because at the time of it parsing the SQL is doesn't, and the VIEW is being created in a separate scope; so it has no idea it's being created. TL;DR: The SQL you've provided works fine: db<>fiddle Commented Feb 21, 2022 at 14:12
  • if you add more after what you've shown us, you'll need another "GO" to keep your "ALTER VIEW..." in its own batch. (Or else use EXEC('...') which always executes in a separate batch). Commented Feb 21, 2022 at 14:16
  • the problem is the red underline extends to other parts of the code so eventually I will be in a situation where everything is underlined and I will get a real error and I will not be able to detect the problem. And also, I don't understand why it works and at the same time it underlines and advise of syntax errors. Commented Feb 21, 2022 at 14:16
  • 1
    IntelliSense reports possible errors as suggestions, not a certain runtime error. IntelliSense does not consider dynamic SQL. Commented Feb 21, 2022 at 14:17
  • @Miquel No, it's just that the Intellisense for a session doesn't update the way that you think it would, but that doesn't affect the compiler at all. If it bothers you, just create a new session and see if the lines are there. Commented Feb 21, 2022 at 14:18

1 Answer 1

2

So the question is: hoy can I put everything in the same script, where I can create views to avoid doing a lot of subqueries, and without getting this errors?

There's no need to check for existence of the view. Just CREATE OR ALTER VIEW

CREATE OR ALTER VIEW NAME_OF_THE_VIEW
AS

-- Here I put the query of the view

SELECT *
FROM table_1

GO

CREATE OR ALTER VIEW NAME_OF_THE_OTHER_VIEW
AS

-- Here I put the query of the view

SELECT *
FROM table_1
Sign up to request clarification or add additional context in comments.

5 Comments

And I would be able to replicate this many times in the same script?
Seperated by GO, yes.
In which position should the "GO" be placed?
On a line by itself separating the script into separate batches. CREATE OR ALTER VIEW must be the only statement in a batch. See updated answer.
Thank you very much, very good and simple solution for all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.