--裴波纳契级数
declare @table1 table( col1 int ,col2 int)
insert @table1 select 1 ,0
union select 2,0
union select 3,0
union select 5,0
union select 8,0
declare @col2 int
select @col2 = 1
select * from @table1
--最后想得到的结果
--1 , 2 @col2+ 1 = 2, select @col2 = 2
--2 , 3 @col2+ 1 = 3, select @col2 = 3
--3 , 5 @col2+ 1 = 5, select @col2 = 5
--5 , 8 @col2+ 1 = 8, select @col2 = 8
--8 , 13 @col2+ 1 = 13, select @col2 = 13最近碰到了裴波纳契级数类似的问题,请问如果使用sql 求解裴波纳契级数问题,最好将col2列的最后结果13也能赋值给变量@col2
declare @table1 table(col1 int,col2 int)
insert into @table select 1,2
declare @a int ,@c int
set @c=0
my_loop:
insert into @table1 select * from @table
set @c=@c+1
select @a=col1 from @table
update @table set col1=col2
update @table set col2=@a+col1
if @c<10
begin
goto my_loop
end
select * from @table1借用楼上的算法,做出来的结果如下:
col1 col2
1 2
2 3
3 5
5 8
8 13
13 21
21 34
34 55
55 89
89 144