(100分)高手何在、急求SQL语句,达到如下效果、、

来源:百度知道 编辑:UC知道 时间:2024/05/28 15:00:14
表A:
code date year
1 5 2007
2 3 2008
查询结果如下:
code date year
1 1 2007
1 1 2007
1 1 2007
1 1 2007
1 1 2007
2 1 2008
2 1 2008
2 1 2008
急!!!谁帮我写出上述效果的SQL语句,马上给分!
多谢顶过!急需建设性的代码!!
各位哥哥姐姐、、我要的是代码哇!!

我用T-sql给你写了个。
运行成功有两个前提:
1:你date字段的数据不能小于0
2:需要建一个与表A结构相同的辅助表(我这里使用的辅助表是AB)
--------------脚本是下面的语句---------------------------

select * into AB from A where 1<>1 ---- 复制表A表结构
declare @code int ,@year int ,@date int ---不清楚你表结构的数据类型,所以都是使用的int 型
declare cur_depart cursor
for select code,date,year from A
open cur_depart
fetch from cur_depart into @code,@date,@year
while @@fetch_status=0
begin
while(@date>=1)
begin
insert into AB values (@code,'1',@year)
set @date=@date-1
end
fetch from cur_depart into @code,@date,@year
end
close cur_depart
deallocate cur_depart

先建一张和A结构一样的表t,我下面这种会修改到原来的那张表A,所以要使用前先备份下A表。可以实现你说的效果

declare @date int,@code int,@year int, @ind int

while exists (select * from A where date<>1)
begin
select @code=code,@date=date, @year=year from A where date<>1
set @ind=@date
w