1.用SQL写一道程序
1,1,2,3,5,8,13.....
求出第30位的数字,并依次打印出来

解决方案 »

  1.   

    declare @i as int
    declare @num1 as int
    declare @num2 as int
    declare @t as int
    select @i =1
    select @num1=1,@num2=1
    while @i<=30
    begin
    select @t=@num2
    select @num2=@num1+@num2
    select @num1=@t
    print @num1 select @i=@i+1
    end 
      

  2.   

    --1,1,2,3,5,8,13..... 
    create table test(ID INT IDENTITY(1,1), n int)
    insert test values(1)
    declare @n int,@k int,@s varchar(200)
    set @n=1
    set @k=1
    set @s='1'
    while @n<=30
    begin 
    if(@n=1)
    set @k=1;
    ELSE
    SET @k=(select SUM(n) from test where ID in (@n,@n-1));
    insert test values(@k)
        SET @s=@s+','+RTRIM(@k)
    set @n=@n+1 
    end
    print @s
    /*
    1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269*/
      

  3.   

    declare @i as int
    declare @num1 as int
    declare @num2 as int
    declare @t as int
    select @i =1
    select @num1=1,@num2=1
    while @i<=30
    begin
    select @t=@num2
    select @num2=@num1+@num2
    select @num1=@t
    print '--'+CAST(@I AS CHAR(2))+'~'+LTRIM(@num1) select @i=@i+1
    end--1 ~1
    --2 ~2
    --3 ~3
    --4 ~5
    --5 ~8
    --6 ~13
    --7 ~21
    --8 ~34
    --9 ~55
    --10~89
    --11~144
    --12~233
    --13~377
    --14~610
    --15~987
    --16~1597
    --17~2584
    --18~4181
    --19~6765
    --20~10946
    --21~17711
    --22~28657
    --23~46368
    --24~75025
    --25~121393
    --26~196418
    --27~317811
    --28~514229
    --29~832040
    --30~1346269
      

  4.   


    declare @i int
    declare @j int
    declare @k int
    declare @l intset @i = 1
    set @j = 1
    set @k = 1
    set @l = 1
    while @j<30
    begin
    print cast(@k as nvarchar(10))
    set @k = @i
    set @i = @l +@k
    set @l = @k
    set @j = @j +1
    end/*
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    144
    233
    377
    610
    987
    1597
    2584
    4181
    6765
    10946
    17711
    28657
    46368
    75025
    121393
    196418
    317811
    514229