表a
aID,aYear,aTitle
表b
bID,aID,bTitle
表c
cID,bID,cTitle表a里aYear是唯一的
aID,bID,cID都是自增的,可为每个表的主键a,b,c三个表是主从关系.a表中aYear表示年份,
我现在要做一个功能根据年份来复制的功能.2006年数据已有了,我想把2006的数据复制一份成为2007年的.如何写存储过程,谢谢!
aID,aYear,aTitle
表b
bID,aID,bTitle
表c
cID,bID,cTitle表a里aYear是唯一的
aID,bID,cID都是自增的,可为每个表的主键a,b,c三个表是主从关系.a表中aYear表示年份,
我现在要做一个功能根据年份来复制的功能.2006年数据已有了,我想把2006的数据复制一份成为2007年的.如何写存储过程,谢谢!
-------------------------------------------------------------------------------------
declare @aID int,@bID int
declare @in int
declare @aTitle varchar(200),@bTitle varchar(200),@cTitle varchar(200)--类型要对应
declare cur_a cursor for
select aID,aTitle from a where a.aYear='2006'fetch next from cur_a into @aID,@aTitlewhile(@@fetch_status=0)
begin /*insert a****************************************************************************/
insert a (aYear,aTitle) select '2007',@aTitle
select @in = @@identity
/* */
declare cur_b cursor for
select bID,bTitle from b where aID=@aID
open cur_b
fetch next from cur_b into @bID,@bTitle
while(@@fetch_status=0)
begin
/*insert b ==================================================*/
insert b (aID,bTitle) select @in ,@bTitle
select @in=@@identity
/* insert c -------------------------------------*/
declare cur_c cursor for
select cTitle from c where bID=@bID
open cur_c
fetch next from cur_c into @cTitle
while(@@fetch_status=0)
begin
insert c(bID,cTitle) select @in,@cTitle
fetch next from cur_c into @cTitle
end
close cur_c
deallocate cur_c
/*--------------------------------------------------*/
fetch next from cur_b into @bID,@bTitle
end
/*===========================================================*/
fetch next from cur_a into @aID,@aTitle
end /****************************************************************************************/close cur_a
deallocate cur_a
-- 1 准备数据
create table a(
aID int identity(1,1) primary key,
aYear varchar(10),
aTitle varchar(10))create table b(
bID int identity(1,1) primary key,
aID int,
bTitle varchar(10))create table c(
cID int identity(1,1) primary key,
bID int,
cTitle varchar(10))alter table b add constraint fk_b_b foreign key (aID) references a (aID)
alter table c add constraint fk_b_c foreign key (bID) references b (bID)
insert a (aYear,aTitle)
select '2006','tom'insert b (aID,bTitle)
select 1,'b_xxx'
union
select 1,'b_yyy'insert c (bID,cTitle)
select 1,'c_xxx'
union all
select 1,'c_yyy'
union all
select 2,'c_xxx'
union all
select 2,'c_yyy'
/* 显示复制前的数据 */
select * from a
select * from b
select * from c-- =========================================================================================================
-- 2 复制数据
declare @aID int,@bID int
declare @in_a int,@in_b int
declare @aTitle varchar(200),@bTitle varchar(200),@cTitle varchar(200)--类型要对应
declare cur_a cursor for
select aID,aTitle from a where a.aYear='2006'open cur_afetch next from cur_a into @aID,@aTitlewhile(@@fetch_status=0)
begin /*insert a****************************************************************************/
insert a (aYear,aTitle) select '2007',@aTitle
select @in_a = @@identity
/* */
declare cur_b cursor for
select bID,bTitle from b where aID=@aID
open cur_b
fetch next from cur_b into @bID,@bTitle
while(@@fetch_status=0)
begin
/*insert b ==================================================*/
insert b (aID,bTitle) select @in_a ,@bTitle
select @in_b=@@identity
/* insert c -------------------------------------*/
declare cur_c cursor for
select cTitle from c where bID=@bID
open cur_c
fetch next from cur_c into @cTitle
while(@@fetch_status=0)
begin
insert c(bID,cTitle) select @in_b,@cTitle
fetch next from cur_c into @cTitle
end
close cur_c
deallocate cur_c
/*--------------------------------------------------*/
fetch next from cur_b into @bID,@bTitle
end
close cur_b
deallocate cur_b
/*===========================================================*/
fetch next from cur_a into @aID,@aTitle
end /****************************************************************************************/close cur_a
deallocate cur_a-- =========================================================================================================
/* 显示复制后的数据 */
select * from a
select * from b
select * from c
/* 删除表 */
drop table c
drop table b
drop table a