首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

存储过程实现删除表,该如何解决

2012-06-09 
存储过程实现删除表我想写一个存储过程,来删除表。存储过程包含一个参数就是表的名字。我删除sail表时,我就

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

[解决办法]

SQL code
if object_id('pro_test')is not nulldrop proc pro_testgocreate proc pro_test @tblname varchar(20)asdeclare @str varchar(200)set @str='if object_id('+quotename(@tblname,'''')+') is not null drop table '+@tblnameprint @strexec(@str)--TRY
[解决办法]
SQL code
if OBJECT_ID('test','P')is not nulldrop proc testgocreate proc test(    @table_name   varchar(max))asdeclare @sql varchar(max)set @sql='' select @sql=@sql+'if object_id('+''''+@table_name+''''+') is not null drop table +'+@table_name+'+'+''exec (@sql) 

热点排行
Bad Request.