我想写一个存储过程,来删除表。存储过程包含一个参数就是表的名字。我删除sail表时,我就把表名字传给这个存储过程,存储过程就判断这个表是否存在,存在就删除,不存在不做任何处理~~~~~~~

解决方案 »

  1.   


    if object_id('pro_test')is not null
    drop proc pro_test
    go
    create proc pro_test @tblname varchar(20)
    as
    declare @str varchar(200)
    set @str=
    'if object_id('+quotename(@tblname,'''')+') is not null
     drop table '+@tblname
    print @str
    exec(@str)--TRY
      

  2.   

    if OBJECT_ID('test','P')is not null
    drop proc test
    go
    create proc test

       @table_name   varchar(max)
    )
    as
    declare @sql varchar(max)
    set @sql='' 
    select @sql=@sql+'if object_id('+''''+@table_name+''''+') is not null drop table +'+@table_name+'+'+''exec (@sql)
      

  3.   

    if OBJECT_ID('test','P')is not null
    drop proc test
    go
    create proc test

       @table_name   varchar(max)
    )
    as
    declare @sql varchar(max)
    set @sql='' 
    select @sql=@sql+'if object_id('+''''+@table_name+''''+') is not null drop table +'+@table_name+'+'+''exec (@sql)
      

  4.   

    对头,但是quotename是什么意思呢?难道我的错了就是因为没有加那个关键字吗