If our statement is:
?
12345678910111213 |
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
|
The following error is prompted:
MSG 156, Level A, State 1, line 4
Incorrect syntax near the keyword ' view '.
The reason for this error is that the phrase "CREATE VIEW" must be the first sentence in the batch process.
So you can modify the statement to:
?
1234567891011121314 |
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
|
SQL Server first determines whether a view exists and then creates a statement for the view