SQL Server first determines whether a view exists and then creates a statement for the view

Source: Internet
Author: User
Tags ticket

If our statement is:

?
12345678910111213 IF NOT EXISTS(SELECT 1 FROM sys.views WHERE name=‘Report_IndividualTicket‘) BEGINcreate view Report_IndividualTicket asSELECT 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 asSELECT 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.