有2列A和B,想要在A中插入从1到10,在B中插入当A=1,B中插入10个数,等等。共一百行数据..要显示如下结果.A   B
1   1
1   2
1   3
1   4
1   5
1   6
1   7
1   8
1   9
1   10
2   1
2   2
......

解决方案 »

  1.   

    insert tablename (a,b)
    select a,b
    from (
    select 1 as a
    union all
    select 2 
    union all
    select 3 
    union all
    select 4 
    union all
    select 5 
    union all
    select 6 
    union all
    select 7 
    union all
    select 8 
    union all
    select 9 
    union all
    select 10
    ) as t1,(
    select 1 as b
    union all
    select 2 
    union all
    select 3 
    union all
    select 4 
    union all
    select 5 
    union all
    select 6 
    union all
    select 7 
    union all
    select 8 
    union all
    select 9 
    union all
    select 10
    ) as t2
     
      

  2.   

    if object_id('tempdb..#tmp') is not null
          drop table #tmp
    declare @t table(A int,B int)
    select top 10 id = identity(int,1,1) into #tmp from syscolumns
    insert @t select b.id ,a.id from #tmp a,#tmp b
    select * from @t
    drop table #tmp
      

  3.   

    declare @i int
    declare @j int
    set @i=1
    set @j=1while(@i<=10 and @j<=10)
    begin
        insert into table1 values(@i,@j)
        print @i
        if(@j=10)
           begin
              set @j=1
              set @i=@i + 1
           end
        else
           set @j=@j+1
    end
      

  4.   

    declare @i as int
    declare @j as int
    @i=1
    @j=1while @i
    begin
      while @j
      begin
       insert into [table_name] (a,b) values(i,j)
       if @j=10
         break
       else
         @j=@j+1
         continue
      end
      if @i=10
        break
      else
        @i=@i+1
        continue
    end
      

  5.   

    declare @i as int
    declare @j as int
    @i=1
    @j=1while @i
    begin
      while @j
      begin
       insert into [table_name] (a,b) values(i,j)
       if @j=10
         break
       else
         @j=@j+1
         continue
      end
      if @i=10
        break
      else
        @i=@i+1
        continue
    end有语法错误:服务器: 消息 156,级别 15,状态 1,行 11
    在关键字 'begin' 附近有语法错误。
    服务器: 消息 156,级别 15,状态 1,行 13
    在关键字 'begin' 附近有语法错误。
    服务器: 消息 170,级别 15,状态 1,行 19
    第 19 行: '@j' 附近有语法错误。
    服务器: 消息 170,级别 15,状态 1,行 25
    第 25 行: '@i' 附近有语法错误。
      

  6.   

    declare @i int
    declare @j int
    set @i=1
    set @j=1while(@i<=10 and @j<=10)
    begin
        insert into table1 values(@i,@j)
        print @i
        if(@j=10)
           begin
              set @j=1
              set @i=@i + 1
           end
        else
           set @j=@j+1
    end
    插不进入结果啊..
      

  7.   

    declare @i int
    declare @j int
    set @i=1while(@i<=10)
    begin
     set @j=1
     while(@j<=10)
       begin
         insert into table1 values(@i,@j)
         set @j=@j+1
       end
     set @i=@i+1
    end