select userid from f201011 union all
select userid from f201012 union all
select userid from f201101 union all
select userid from f201102 union all
select userid from f201103 union all
select userid from f201104 union all
select userid from f201105 union all
select userid from f201106 union all
select userid from f201107 union all
select userid from f201108 union all
select userid from f201109 union all
select userid from f201110
查询用户一年的信息,表每个月增加,该怎么做?
select userid from f201012 union all
select userid from f201101 union all
select userid from f201102 union all
select userid from f201103 union all
select userid from f201104 union all
select userid from f201105 union all
select userid from f201106 union all
select userid from f201107 union all
select userid from f201108 union all
select userid from f201109 union all
select userid from f201110
查询用户一年的信息,表每个月增加,该怎么做?
CREATE PROCEDURE P1
(
@year nvarchar(4)
)
ASDECLARE @s NVARCHAR(4000),@i INT
SELECT @i=1WHILE @i<12
SELECT @s=ISNULL(@s+' union all select ',' select ')+' userid from f'+@year+RIGHT(100+@i,2),@i=@i+1
EXEC(@s)
在显示生成语句用 EXEC(@s) 改为 print @s
可以看到生成格式
(
@year nvarchar(4)
)
ASDECLARE @s NVARCHAR(4000),@i INT,@j int
SELECT @i=1,@j=12
if YEAR(GETDATE())=@year
set @j=MONTH(GETDATE())WHILE @i<@j
SELECT @s=ISNULL(@s+' union all select ',' select ')+' userid from f'+@year+RIGHT(100+@i,2),@i=@i+1EXEC(@s)