Create view and use it in a single SQL script - sql

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 GO keyword like query bellow:
CREATE VIEW someView AS ()
GO
DROP VIEW someView
GO

Regardless of which particular DBMS you are using, you should create a script separating your SQL statements with ';'.
For example
CREATE VIEW someView as (...);
<<some other sql statements>>
DROP VIEW someView;

Related

how to remove Error for schema bindings in redshift

I want to be able to make CTE to make the below SQL work, I am getting the error
ERROR: Cannot replace a normal view with a late binding view for the below SQL, any way I could change it up so that it doesnt bind with schema views?
CREATE OR REPLACE
VIEW "dev"."XXBRK_DAILY_FX_RATES" ("F_C", "CURRENCY", "C_D", "C_R") AS
SELECT DISTINCT GL.GL_R.F_C, GL.GL_R.CURRENCY,
GL.GL_R.DATE, GL.GL_R.C_R
FROM GL.GL_R
with no schema binding
WHERE GL.GL_R.C_T='Corporate'
UNION ALL
SELECT DISTINCT GL.GL_R.F_C, GL.GL_R.F_C CURRENCY, GL.GL_R.DATE, 1
FROM GL.GL_R;
So you seem to have a statement issue. The last 4 lines are after the ';' and not part of the statement being run. I'm guessing that these are extraneous and posted by mistake.
Views on Redshift come in several types - normal and late binding are 2. The view "dev"."XXBRK_DAILY_FX_RATES" seems to already exist in your cluster so your command is trying to replace it, not create it. The error message is correct, you cannot replace a view with a view of a different type. You need to drop the view, then recreate it as late binding.
Now be careful as other objects dependent on this view will be impacted when you drop it (especially if you CASCADE the drop). When you drop and recreate the view it is a new object in the database but replacing a view just make a new definition for the same object. Understand the impacts of drop to your database before you execute it.

Executing multiple drop table statements in 1 sql file

I am trying to drop 200 tables from my database and save the sql statements to 1 .sql file so they can be run in one go on each copy of the database.
I would like to use a separate drop table statement for each table so I can easily reference what line any errors appear on.
I have tried running a query in the following format
DROP TABLE schema.tablename GO
DROP TABLE schema.tablename GO
DROP TABLE schema.tablename GO
When I execute the query, I get
Incorrect syntax near 'GO'.
For each line.
What is the proper way of doing this that will still allow me to easily locate errors on the tables that are unable to be dropped?
The GO has to be on a separate line, or use a semi-colon
DROP TABLE schema.tablename
GO
DROP TABLE schema.tablename;
Note in the documentation that:
GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.
So if you are trying to execute this as a single "command" then go will not work - you'll need to execute it as separate commands.
If you are using a batch command processor, then also note:
A Transact-SQL statement cannot occupy the same line as a GO command. However, the line can contain comments.
So try putting the GO commands on their own lines:
DROP TABLE schema.tablename
GO
DROP TABLE schema.tablename
GO
DROP TABLE schema.tablename
GO

How to use an IF statement within a view to drop/use a #Temp Table?

I need to create a view, which begins with the following SQL:
IF OBJECT_ID('tempdb..#TempTable') Is Not null
Drop Table #TempTable
I am also rebuilding this #TempTable further on in the view, and then using data in the Temptable for the rest of the query - for better performance. See:
Reduce cost for Table Valued Function - XML Reader in query plan - how?
However, SSMS is telling me:
Incorrect syntax near the keyword 'IF'
Views or functions are not allowed on temporary tables.
Table names that begin with '#' denote temporary tables.
Is there any way to use the IF statement, drop the Temptable, and then use the rebuilt Temptable in the view?
ANSWER - I need to use a stored procedure, not a view.
A stored procedure is quite different from a view. A view is simply an encapsulated query.
However, a stored procedure cannot be used in a SELECT statement. You can only execute it using EXEC.
What you probably want is a user-defined function (UDF). UDFs return a table, so they can be referenced in the FROM clause of a query. Unlike a view, they can contain multiple statements and can have parameters.
As a note, there is no need to delete temporary tables in stored procedures or functions. If the temporary table is declared in the body of the code, then it is automatically deleted when the code is exited. However, you might want to use a table variable instead.

PhpPgAdmin Syntax error when creating View

I am attempting to create a View in PhpPgAdmin (PostGreSQL db) which has the following SQL statement:
DELETE FROM myTable WHERE myTable.error IS NULL;
PhpPgAdmin gives me the following error:
ERROR: syntax error at or near "DELETE" at character 59
In statement:
CREATE OR REPLACE VIEW "Schema1"."Delete empty errors" AS DELETE FROM myTable WHERE myTable.error IS NULL;
As far as I can tell this SQL statement is valid, and I have delete privileges for the table. Is the DELETE statement not allowed in Views? Any ideas what I am doing wrong?
Views are used to display the data from SELECT statements only (usually when the SELECT is complex). Views cannot contain DELETES, UPDATES, or INSERTS.
Perhaps you want a function?
EDIT: As OMG Ponies points out, you can have updateable views, but thats where you would issue a DELETE to an existing view and then use a RULE to rewrite the query as a DELETE.
And please, please don't wrap a function call to do a DELETE as a side effect in a view. Its unexpected and Jesus shoots a puppy every time this happens.

CREATE OR REPLACE VIEW sql error

Trying to update a table view using:
CREATE OR REPLACE VIEW [vtable] AS SELECT * FROM Files_Table ORDER BY File
The table is returning the old view, not the updated.
Statement tested in the Sqlite database browser:
Error message from database engine: near "OR": syntax error
but didn't get this in the program?
Any idea why it's not updating?
SQLite does not support the CREATE OR REPLACE syntax. The only database that I know which supports that syntax is Oracle, but I am guessing there are others.
Drop the view and create it with the new definition:
DROP VIEW IF EXISTS [vtable]; -- "OR REPLACE"
CREATE VIEW [vtable] AS SELECT * FROM Files_Table ORDER BY File;
I ran into the same error when using CREATE OR REPLACE in MS SQL Server. The following worked for me:
ALTER VIEW [vtable] AS
SELECT *
FROM Files_Table
ORDER BY File;