ALTER proc ReturnN(@n int)
as
begin
declare @sql varchar(8000)
declare @b int
set @b=0
set @sql=''
while @b<@n
begin
set @sql=@sql+'select * from bb where AID='+cast(@n as varchar(10))+' union all '
set @b =@b+1
end
set @sql=substring(@sql,0,len(@sql)-len(' union all ' ))
print @sql
exec(@sql)
end
as
begin
declare @sql varchar(8000)
declare @b int
set @b=0
set @sql=''
while @b<@n
begin
set @sql=@sql+'select * from bb where AID='+cast(@n as varchar(10))+' union all '
set @b =@b+1
end
set @sql=substring(@sql,0,len(@sql)-len(' union all ' ))
print @sql
exec(@sql)
end
set @i = 3
select * from table1 where AID=@i
declare @i int
while (@i<=5)
begin
insert into #t1
select * from t1 where AID=1
set i=i+1
end
这句是复制它的表结构
select * into #t1 from t1 where 1<>1
declare @table1 table(AID int, ANAME varchar(10), ACOM varchar(10))insert into @table1 select 1, 'a', 'd'
insert into @table1 select 2, 'b', 'c'
insert into @table1 select 3, 'k', 's'--解决方法
select a.* from @table1 a cross join
(
SELECT b2.i + b1.i + b0.i x
FROM (SELECT 0 i UNION ALL SELECT 1) b0
CROSS JOIN (SELECT 0 i UNION ALL SELECT 2) b1
CROSS JOIN (SELECT 0 i UNION ALL SELECT 4) b2
)b
where aid=1 and b.x<3