Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 下拉视图(如果存在)_Sql_Sql Server_View_Create View - Fatal编程技术网

Sql 下拉视图(如果存在)

Sql 下拉视图(如果存在),sql,sql-server,view,create-view,Sql,Sql Server,View,Create View,我有一个脚本,我想首先删除视图,然后创建它。 我知道如何放下桌子: IF EXISTS (SELECT * FROM sys.tables WHERE name = 'table1' AND type = 'U') DROP TABLE table1; 因此,我对观点也采取了同样的做法: IF EXISTS (SELECT * FROM sys.views WHERE name = 'view1' AND type = 'U') DROP VIEW view1; create view1 as

我有一个脚本,我想首先删除视图,然后创建它。 我知道如何放下桌子:

IF EXISTS (SELECT * FROM sys.tables WHERE name = 'table1' AND type = 'U') DROP TABLE table1;
因此,我对观点也采取了同样的做法:

IF EXISTS (SELECT * FROM sys.views WHERE name = 'view1' AND type = 'U') DROP VIEW view1;
create view1 as(......)
然后我得到了一个错误:

“创建视图”必须是查询批处理中的第一条语句


您的exists语法错误,应该将DDL与go分开,如下所示

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

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

create view tst
as
select * from test
您还可以使用如下对象id检查存在性测试

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

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

create view tst
as
select * from test
在SQL 2016中,您可以使用以下语法删除

Drop view  if exists dbo.tst
从SQL2016 CU1,您可以执行以下操作

create or alter view vwTest
as
 select 1 as col;
go

为了适应模式,请在SQL 2014中使用此格式

if exists(select 1 from sys.views V inner join sys.[schemas] S on  v.schema_id = s.schema_id where s.name='dbo' and v.name = 'someviewname' and v.type = 'v')
  drop view [dbo].[someviewname];
go
把它扔出去,做存储过程,因为我也需要它:

if exists(select 1
          from sys.procedures p
          inner join sys.[schemas] S on p.schema_id = s.schema_id
          where
              s.name='dbo' and p.name = 'someprocname'
          and p.type in ('p', 'pc')
  drop procedure [dbo].[someprocname];
go
关于错误

“创建视图”必须是查询批处理中的第一条语句。

Microsoft SQL Server有一个奇怪的要求,
CREATE VIEW
是批处理中的唯一语句。其他一些语句也是如此,例如
CREATE FUNCTION
。“创建表”的情况并非如此,所以请参考图

解决方案是将脚本以小批量发送到服务器。一种方法是选择一条语句并执行它。这显然不方便

更方便的解决方案是让客户端以小的独立批发送脚本

GO
关键字并不是严格意义上的SQL命令,这就是为什么不能像真正的SQL命令那样以分号结尾。相反,它是一条指示客户机在此时中断脚本并将部分作为批发送的指令

因此,您最终会写下以下内容:

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

我遇到的其他数据库服务器(PostgreSQL、MySQL、Oracle、SQLite)都没有这种怪癖,因此,该要求似乎仅适用于Microsoft。

在这些命令之间放置一个
GO
,我将其放在create:GO create…等之前,但随后得到:数据库中已经有一个名为“TSB”的对象。对象类型错误-使用“V”而不是“U”。是的,改变了,它开始工作了。把U改成V,现在开始工作了!谢谢你的帮助小更正-DROP VIEW dbo.tst IF EXISTS应该读DROP VIEW IF EXISTS dbo.tst hanks,我实际上在go after DROP后面有一个分号,它不是这样的。删除了分号,效果很好。如何将架构添加到检查中?
DROP VIEW if exists {ViewName}
Go
CREATE View {ViewName} AS 
SELECT * from {TableName}  
Go