日期:2014-05-18  浏览次数:20612 次

【求助】如何对返回的结果做操作?
SQL code
select   *   from   sysobjects   where   id   in   (select   id   from   syscolumns   where   name= 'approvedate') 


这样我会得到很多表名如何将这些表依次执行下面的删除操作?

SQL code
declare @k datetime 
declare @j datetime 
set @k='2009-04-20 10:10:11.000' 
set @j='2009-04-23 19:53:11.000'

delete FROM RegisterReport
WHERE approveDate
BETWEEN @k AND @j;


------解决方案--------------------
少改一个位置,a.Name=表名
SQL code
declare @k datetime 
declare @j datetime 
set @k='2009-04-20 10:10:11.000' 
set @j='2009-04-23 19:53:11.000'

--delete FROM RegisterReport
--WHERE approveDate
--BETWEEN @k AND @j;

--生成语句直接执行
select ' delete 'a.+name+'  where  approvedate between '''+CONVERT(varchar(23),@k,120)+''' and '''+CONVERT(varchar(23),@k,120)+''' ;'
from sysobjects as a,syscolumns as b where a.id=b.id and b.name='approvedate'

--或用变量传参一次执行
declare @s nvarchar(max)
select @s=@s+' delete '+a.name+'  where  approvedate between '''+CONVERT(varchar(23),@k,120)+''' and '''+CONVERT(varchar(23),@k,120)+''' ;'
from sysobjects as a,syscolumns as b where a.id=b.id and b.name='approvedate'

exec(@s)