if object_id('createview') is not null drop proc createview
go
CREATE proc createView
@viewName varchar(100),
@condition varchar(2000),
@judge varchar(10)
as
set nocount on
if(@judge = 'true')
begin 
exec('if object_id('''+@viewName+''')is not null drop view '+ @viewName)
exec('create view '+@viewName+' as '+@condition)
end
else
exec('if object_id('''+@viewName+''')is not null drop view '+ @viewName)
set nocount offGO----执行存储过程
exec createview '22345234','select  * from category','true'

解决方案 »

  1.   

    可以的 
    你只需要加上 [] 即可,名稱中有空格時就必須加上create view [2] as select 1 as [City Hunter]
      

  2.   

    if object_id('createview') is not null drop proc createview
    go
    CREATE proc createView
    @viewName varchar(100),
    @condition varchar(2000),
    @judge varchar(10)
    asdeclare @sql varchar(400)
    set nocount on
    if(@judge = 'true')
    begin 
    set @sql='if exists (select * from dbo.sysobjects where id = object_id(N'+''''+'[dbo].['+@viewName+']'''+') and OBJECTPROPERTY(id, N'+''''+'IsView'''+') = 1)'
    set @sql=@sql+' drop view [dbo].['+@viewName+']'

    print @sql
    exec(@sql) set @sql=' create view '+@viewName+' as '+@condition print @sql
    exec(@sql)
    end
    else
    begin
    set @sql='if exists (select * from dbo.sysobjects where id = object_id(N'+''''+'[dbo].['+@viewName+']'''+') and OBJECTPROPERTY(id, N'+''''+'IsView'''+') = 1)'
    set @sql=@sql+' drop view [dbo].['+@viewName+']'

    print @sql
    exec(@sql)
    end
    set nocount offGO