CREATE TABLE [test](
[a] int,
[b] int

GO我想把1~10插入到表中,生成所有可能产生的组成,并且不想要有重复的组合,想了半天没想到咋搞,求帮忙~

解决方案 »

  1.   

    只想到用笨办法做
    set@b=1~10
    运行10次~
    declare @a  int
    set @a=1
    declare @b  int
    set @b=1
    while @a<=10
    begin
    insert into [test] values
    (@a,@b)
    set @a+=1
    end
      

  2.   


    提供一种蛮办法,看看是否行:
    if db_id('testdb') is not null
    drop database testdb
    gocreate database testdb
    gouse testdb
    goif object_id('tb1')  is not null
    drop table tb1if object_id('tb2')  is not null
    drop table tb2create table tb1
    (a int)
    gocreate table tb2
    (b int)
    godeclare @a int
    set @a=1
    while @a<=10
    begin
           insert into tb1 values(@a)
        set @a=@a+1   
    enddeclare @a int
    set @b=1
    while @b<=10
    begin
           insert into tb2 values(@a)
        set @b=@b+1   
    endselect * into tb
    from (select *  from tb1 cross join  tb2)  as bselect * from tb/*
    1 1
    2 1
    3 1
    4 1
    5 1
    6 1
    7 1
    8 1
    9 1
    10 1
    1 2
    2 2
    3 2
    4 2
    5 2
    6 2
    7 2
    8 2
    9 2
    10 2
    1 3
    2 3
    3 3
    4 3
    5 3
    6 3
    7 3
    8 3
    9 3
    10 3
    1 4
    2 4
    3 4
    4 4
    5 4
    6 4
    7 4
    8 4
    9 4
    10 4
    1 5
    2 5
    3 5
    4 5
    5 5
    6 5
    7 5
    8 5
    9 5
    10 5
    1 6
    2 6
    3 6
    4 6
    5 6
    6 6
    7 6
    8 6
    9 6
    10 6
    1 7
    2 7
    3 7
    4 7
    5 7
    6 7
    7 7
    8 7
    9 7
    10 7
    1 8
    2 8
    3 8
    4 8
    5 8
    6 8
    7 8
    8 8
    9 8
    10 8
    1 9
    2 9
    3 9
    4 9
    5 9
    6 9
    7 9
    8 9
    9 9
    10 9
    1 10
    2 10
    3 10
    4 10
    5 10
    6 10
    7 10
    8 10
    9 10
    10 10*/
      

  3.   


    declare @a int
    此处没改过来
    应为:
    declare @b intinsert into tb2 values(@a)
    应为:
    insert into tb2 values(@b)
      

  4.   

    drop table test
    CREATE TABLE [test](
        [a] int,
        [b] int

    GO
    insert into test 
    select t1.number,t2.number from (select number from master.dbo.spt_values   where type='p' and number>=1 and number<=10)t1
    cross join
    (select number from master.dbo.spt_values   where type='p' and number>=1 and number<=10)t2--1 1
    --2 1
    --3 1
    --4 1
    --5 1
    --6 1
    --7 1
    --8 1
    --9 1
    --10 1
    --1 2
    --2 2
    --3 2
    --4 2
    --5 2
    --6 2
    --7 2
    --8 2
    --9 2
    --10 2
    --1 3
    --2 3
    --3 3
    --4 3
    --5 3
    --6 3
    --7 3
    --8 3
    --9 3
    --10 3
    --1 4
    --2 4
    --3 4
    --4 4
    --5 4
    --6 4
    --7 4
    --8 4
    --9 4
    --10 4
    --1 5
    --2 5
    --3 5
    --4 5
    --5 5
    --6 5
    --7 5
    --8 5
    --9 5
    --10 5
    --1 6
    --2 6
    --3 6
    --4 6
    --5 6
    --6 6
    --7 6
    --8 6
    --9 6
    --10 6
    --1 7
    --2 7
    --3 7
    --4 7
    --5 7
    --6 7
    --7 7
    --8 7
    --9 7
    --10 7
    --1 8
    --2 8
    --3 8
    --4 8
    --5 8
    --6 8
    --7 8
    --8 8
    --9 8
    --10 8
    --1 9
    --2 9
    --3 9
    --4 9
    --5 9
    --6 9
    --7 9
    --8 9
    --9 9
    --10 9
    --1 10
    --2 10
    --3 10
    --4 10
    --5 10
    --6 10
    --7 10
    --8 10
    --9 10
    --10 10
      

  5.   


    declare @a int
    set @a=1
    while @a<=10
    begin
    declare @b int
    set @b=1
    while @b<=10
    begin
    insert into test(a,b)values(@a,@b)
    set @b=@b+1   
    end
        set @a=@a+1
    end
     
    select * from test
      

  6.   

    ;with wang as (
    select number from master..spt_values
    where type='p' and number between 1 and 10),
    wang2 as
    (
    select num1=s.number,num2=t.number from wang s cross join wang t)select * from wang2 s 
    where not exists(select 1 from wang where s.num1 =num2 and s.num2 =num1)
      

  7.   


    -- 凑个人数。
    with m as 
    (select ROW_NUMBER() over(order by name) rn from sysobjects )
    select b.rn, a.rn from m a , m b
    where a.rn <= 10 and b.rn <= 10  and a.rn >= b.rn