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

我还是喜欢你,像日月星辰,经久不息。仲夏时节,气候宜人,大地散发着诱人的清香。五彩斑斓的蝴蝶丰容靓饰,顾盼生姿,体态轻盈的蜜蜂乱欢飞舞,寻香弄绿,一派繁忙的景象。

如果我们的语句为:

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 这一句必须是批处理中的第一句。

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

IF EXISTS(SELECT 1 FROM sys.views WHERE name='Report_IndividualTicket')
DROP VIEW Report_IndividualTicket
GO
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
GO

本文SQLSERVER先判断视图是否存在然后再创建视图的语句到此结束。人生目标确定容易实现难,但是如果不行动,那连实现的可能也不会有。小编再次感谢大家对我们的支持!

标签: 然后再 存在