SQL server first checks whether a view exists and then creates a view statement. SQL SERVER
If our statement is:
IF NOT EXISTS(SELECT 1 FROM sys.views WHERE name='Report_IndividualTicket')BEGINcreate view Report_IndividualTicketasSELECT Ticket.TicketNumber, Ticket.TicketID,GisProcess.StageName,Content.DtReceived, Content.ContentTextFROM(GisProcess INNER JOIN TicketON GisProcess.TicketID=Ticket.TicketID)INNER JOIN ContentON Ticket.ContentID=Content.ContentIDEND
The following error is prompted:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'view '.
The cause of this error is that the "create view" statement must be the first one in batch processing.
Therefore, you can modify the statement:
IF EXISTS(SELECT 1 FROM sys.views WHERE name='Report_IndividualTicket')DROP VIEW Report_IndividualTicketGOcreate view Report_IndividualTicketasSELECT Ticket.TicketNumber, Ticket.TicketID,GisProcess.StageName,Content.DtReceived, Content.ContentTextFROM(GisProcess INNER JOIN TicketON GisProcess.TicketID=Ticket.TicketID)INNER JOIN ContentON Ticket.ContentID=Content.ContentIDGO
How does SQL server determine whether a view exists?
SELECT count (*) as cut FROM sysviews WHERE object_id = '[dbo]. [view name]'
If fieldByName ('cut ')> 0 then
// Exists
Else
// Does not exist
SQL server determines whether a view exists
First, judge whether the following statements return results, and then judge.
SELECT * from dbo. sysobjects B where B. XTYPE = 'V' and B. NAME = 'A'