A to B
表A
1 2
1 3
2 1
2 2
3 1
表B
id pid
1 1,2,3
2 1,2
3 1
创建一个合并的函数
create function fmerg(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+cast(pid as varchar) from 表A where id=@id set @str=right(@str,len(@str)-1)
return(@str)
End
go
第二问
if exists(select * from sysobjects where xtype='u' and name='A_mcn') drop table A_mcn
create table A_mcn (in_detail_id char(10),scheduler_code char(6),pmat_id char(10),Jan float,Feb float,Mar float,Apr float)
declare @in_detail_id char(10)
declare @scheduler_code char(6)
declare @scheduler_month char(10)
declare @pmat_id char(10)
declare @aty_need float
declare mycur cursor for
select in_detail_id,scheduler_code,scheduler_month,pmat_id,aty_need from A_mc order by in_detail_id
open mycur
fetch next from mycur
into @in_detail_id,@scheduler_code,@scheduler_month,@pmat_id,@aty_need
while @@FETCH_STATUS = 0
begin
if @scheduler_month='1'
begin
insert into A_mcn values(@in_detail_id,@scheduler_code,@pmat_id,@aty_need,'','','')
end
if @scheduler_month='2'
begin
insert into A_mcn values(@in_detail_id,@scheduler_code,@pmat_id,'',@aty_need,'','')
end
if @scheduler_month='3'
begin
insert into A_mcn values(@in_detail_id,@scheduler_code,@pmat_id,'','',@aty_need,'')
end
if @scheduler_month='4'
begin
insert into A_mcn values(@in_detail_id,@scheduler_code,@pmat_id,'','','',@aty_need)
end
fetch next from mycur
into @in_detail_id,@scheduler_code,@scheduler_month,@pmat_id,@aty_need
end
close mycur
deallocate mycur
建了个新表,名字改一下,由于用了float,没有数据的地方全是0,改成varchar应该可以了,月份增加的话自己加相应的代码。。。
[此贴子已经被作者于2007-6-7 17:41:47编辑过]