Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop view if exists

I have script where I want to first drop view and then create it. I know how to drop table:

IF EXISTS (SELECT * FROM sys.tables WHERE name = 'table1' AND type = 'U') DROP TABLE table1; 

so I did the same for views:

IF EXISTS (SELECT * FROM sys.views WHERE name = 'view1' AND type = 'U') DROP VIEW view1; create view1 as(......) 

and then I got error:

'CREATE VIEW' must be the first statement in a query batch.

like image 751
4est Avatar asked Mar 21 '16 14:03

4est


People also ask

What is drop table if exists?

Description. The DROP TABLE statement deletes the specified table, and any data associated with it, from the database. The IF EXISTS clause allows the statement to succeed even if the specified tables does not exist.

How do I drop a procedure if exists?

DROP PROCEDURE removes the definition of one or more existing procedures. To execute this command the user must be the owner of the procedure(s). The argument types to the procedure(s) usually must be specified, since several different procedures can exist with the same name and different argument lists.

What will happen if you drop view?

Remarks. When you drop a view, the definition of the view and other information about the view is deleted from the system catalog. All permissions for the view are also deleted. Any view on a table that is dropped by using DROP TABLE must be dropped explicitly by using DROP VIEW.

How do I drop a view in SQL Server?

The syntax for the SQL DROP VIEW Statement is: DROP VIEW view_name; view_name. The name of the view that you wish to drop.


2 Answers

your exists syntax is wrong and you should seperate DDL with go like below

if exists(select 1 from sys.views where name='tst' and type='v') drop view tst; go  create view tst as select * from test 

you also can check existence test, with object_id like below

if object_id('tst','v') is not null drop view tst; go  create view tst as select * from test 

In SQL 2016,you can use below syntax to drop

Drop view  if exists dbo.tst 

From SQL2016 CU1,you can do below

create or alter view vwTest as  select 1 as col; go 
like image 76
TheGameiswar Avatar answered Sep 21 '22 15:09

TheGameiswar


Regarding the error

'CREATE VIEW' must be the first statement in a query batch.

Microsoft SQL Server has a quirky reqirement that CREATE VIEW be the only statement in a batch. This is also true of a few other statements, such as CREATE FUNCTION. It is not true of CREATE TABLE, so go figure …

The solution is to send your script to the server in small batches. One way to do this is to select a single statement and execute it. This is clearly inconvenient.

The more convenient solution is to get the client to send the script in small isolated batches.

The GO keyword is not strictly an SQL command, which is why you can’t end it with a semicolon like real SQL commands. Instead it is an instruction to the client to break the script at this point and to send the portion as a batch.

As a result, you end up writing something like:

DROP VIEW IF EXISTS … ; GO CREATE VIEW … AS … ; GO 

None of the other database servers I have encountered (PostgreSQL, MySQL, Oracle, SQLite) have this quirk, so the requirement appears to be Microsoft Only.

like image 45
Manngo Avatar answered Sep 19 '22 15:09

Manngo