当前位置: 代码迷 >> Sql Server >> 批删除时有有关问题
  详细解决方案

批删除时有有关问题

热度:83   发布时间:2016-04-27 15:29:42.0
批删除时有问题
批删除 
'@库表名' 是定义把所有数据库表名放到这个表里 Name是表名

declare @i int
set @i=0
declare @iCount int 
set @iCount=(select count(*) from @库表名)
while(@i<@iCount)

begin
 set @[email protected]+1
 declare @fTableName nvarchar(50)
 set @Name=(select Name from @库表名 where [email protected])
 delete [@Name]

end

但执行报对象名 [email protected]'无效。

------解决方案--------------------
需要動態sql
比如
exec('delete [email protected])
------解决方案--------------------
--删除表
exec('drop table [email protected])

--删除数据库
exec('drop database [email protected])
------解决方案--------------------
SQL code
declare @i int set @i=0 declare @iCount int  set @iCount=(select count(*) from @库表名)  declare @fTableName nvarchar(50) while(@i <@iCount) begin      set @[email protected]+1     delete @库表名 where [email protected]) end
------解决方案--------------------
[code=SQL][/code]


CREATE PROCEDURE s_Delete 

@库表名 table 

declare @i int
set @i=0
declare @iCount int
 set @icount= exec('select count(*) from '+@库表名)
while(@i <@iCount)

begin
 set @[email protected]+1
 declare @fTableName nvarchar(50)
 set @Name=exec('select Name from '+@库表名 +' where fID='+ltrim(@i))
 exec('delete from ' +[@Name])

end
  相关解决方案