日期:2014-05-17  浏览次数:20551 次

请教一个关于存储过程动态调用的问题
已有一个存储过程,该存储过程的输入参数为某个表的id字段,然后根据这个id执行某些删除操作和数据清理。

现在我想要实现的就是:利用select按某些条件查询,查询出需要执行这个存储过程的id,如何通过查询出的id动态调用存储过程?谢谢!!!
存储过程?动态调用

------解决方案--------------------
写一个游标来读取id字段,然后在游标循环体中调用存储过程。

------解决方案--------------------
like this,

declare @id int,@tsql varchar(6000)
declare ap scroll cursor for
 select id from [表名] where [条件]

open ap
fetch first from ap into @id
while(@@fetch_status<>-1)
begin
  select @tsql='exec [存储过程名] @id='+rtrim(@id)
  exec(@tsql)
  fetch next from ap into @id
end

close ap
deallocate ap