SQL SERVER先判断视图是否存在然后再创建视图的语句
程序员文章站
2023-08-13 18:03:51
如果我们的语句为:
if not exists(select 1 from sys.views where name='report_individualtick...
如果我们的语句为:
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