sql创建语句的问题,求解
现在有两个表,我想从表1中取出一个id,在表2中插入20条包含这个id的记录,如此循环,把表1的id都插1遍,有没有高人指点下呢
[解决办法]
。。。为何要20次,有啥规律?
declare @i int
set @i = 1
while @i <= 20
begin
insert into b select id from a
set @i = @i + 1
end
[解决办法]
insert 表2select a.IDfrom 表1 as a,(select top 20 ID from syscolumns) as bwhere a.ID=a
[解决办法]
双循环
declare @id varchar(10),@n int=1 ,@i int=1while exists(select 1 from (select *,ROW_NUMBER() over(order by getdate()) rid from tb)a where rid=@n)begin select @id=id from tb where rid=@n while @i<=20 begin insert into b select id from a set @i = @i + 1 end set @n=@n+1end
[解决办法]
declare @id
declare @cursor_Insert cursor for
select Id from table1
open @cursor_Insert
fetch next from @cursor_Insert into @id
while @@FETCH_STATUS=0
begin
..插入20行数据.
fetch next from @cursor_Insert into @id
end
close @cursor_Insert
deallocate @cursor_Insert
[解决办法]