SQL SERVER先判断视图是否存在然后再创建视图的语句

前端之家收集整理的这篇文章主要介绍了SQL SERVER先判断视图是否存在然后再创建视图的语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如果我们的语句为:

sql;"> IF NOT EXISTS(SELECT 1 FROM sys.views WHERE name='Report_IndividualTicket') BEGIN create view Report_IndividualTicket as SELECT Ticket.TicketNumber,Ticket.TicketID,GisProcess.StageName,Content.DtReceived,Content.ContentText FROM (GisProcess INNER JOIN Ticket ON GisProcess.TicketID=Ticket.TicketID) INNER JOIN Content ON Ticket.ContentID=Content.ContentID END

提示以下错误

Msg 156,Level 15,State 1,Line 4
Incorrect Syntax near the keyword 'view'.

出现这个错误的原因是:create view 这一句必须是批处理中的第一句。

所以可以将该语句修改为:

sql;"> IF EXISTS(SELECT 1 FROM sys.views WHERE name='Report_IndividualTicket') DROP VIEW Report_IndividualTicket GO create view Report_IndividualTicket as SELECT Ticket.TicketNumber,Content.ContentText FROM (GisProcess INNER JOIN Ticket ON GisProcess.TicketID=Ticket.TicketID) INNER JOIN Content ON Ticket.ContentID=Content.ContentID GO

猜你在找的MsSQL相关文章