在SQL里面有一个循环取数据的,想把循环的Union all 成一个数据集,有什么办法没有?
例declare @t table(oid int identity ,d int,m varchar(20),startdate datetime , enddate datetime)
declare @oid int
set @oid = 1
while @oid <= 12
begin
select * from a where a.datetime>= (select startdate from @t where oid=@oid)
end
例declare @t table(oid int identity ,d int,m varchar(20),startdate datetime , enddate datetime)
declare @oid int
set @oid = 1
while @oid <= 12
begin
select * from a where a.datetime>= (select startdate from @t where oid=@oid)
end
解决方案 »
- password出于安全原因,已用该注释替换此文本
- 简单的查询,多条记录在一个字段里面显示
- sql语句错误 求助????
- SQL语句求助
- 哎,今天第三个问题了,还是语法~~~~
- 怎么选择虚拟主机?
- 如何用sql语句来删除字段中的全角空格
- 当用户删除记录时激活触发器,我如何在触发器中知道删除了哪条记录呢
- 再问:为什么我在使用SQLDMO进行异地备份数据库时,总提示出错(我使用的)。在线等待,急!
- 关于分组(Group by)以后,求各个和(sum)的最大值(max)的问题
- 那位高手知道,为什么在asp.net 2.0中添加不上Microsoft.AnalysisServices.DLL引用(SQL2005),而在winfrm中却可以
- 一个查询条件冲突问题!
然后在从表变量里面选。
declare @t table(oid int identity ,d int,m varchar(20),startdate datetime , enddate datetime)
select * from a where a.datetime>= (select min(startdate) from @t where oid between 1 and 12)
declare @t table(oid int identity ,d int,m varchar(20),startdate datetime , enddate datetime)
declare @oid int
set @oid = 1
declare @sql varchar(1000)
set @sql=''
while @oid <= 12 begin
set @sql=@sql+' ######################+' union all '
endset @sql=substring(@sql,1,len(@sql)-10)exec (@sql)