请教如何用代码创建SQL数据库视图?  
  先谢谢!!我是想用代码:
if(name这个视图存在)
{
  退出;
}
else
{
  建立name视图;
}想用代码执行一段数据库脚本,不知如何写,请教老师呀!

解决方案 »

  1.   


    declare @cou int
    select @cou=count(*) from sysobjects where xtype = 'v' and name ='视图名'
       if @cou >0
    print '已存在视图'
       else 
    create view as 
    select * from tab
    where ...
      

  2.   


    这个查询写法select @cou=count(*) from sysobjects where xtype = 'v' and name ='视图名'是2005的,如果是2000,则是select @cou=count(*) from sysobjects where type = 'v' and name ='视图名'
      

  3.   

    先谢谢,
    我试了下,在SQL2005查询分析器中运行不成呀。说是view附近有错误!
      

  4.   


    if object_id('dbo.v_name','v') is not null
     return;
    create view dbo.v_name 
    as
    select ... ...
      

  5.   

    谢谢还是不行。
    消息 111,级别 15,状态 1,第 3 行
    'CREATE VIEW' 必须是查询批次中的第一个语句。
      

  6.   


    if object_id('dbo.v_name','v') is not null
     return;
    go
    create view dbo.v_name 
    as
    select ... ...
    go
      

  7.   

    if(name这个视图存在)
    {
      退出;
    }
    else
    {
      exec ('建立name视图的SQL')
    }