例如:mp_no itm prd_no name date(目标表取的字段) mp_no itm prd_no name date (源表要取的字段)
001 1 a001 000 0000 001 1 c001 000 0000
001 2 a002 000 0000 001 2 c002 000 0000
002 1 b001 000 0000 002 1 d001 000 000
002 2 b002 000 0000 002 2 d002 000 0000
002 3 d004 000 0000
最后我想达到的效果是触发源表
mp_no itm prd_no name date (目标表取的字段)
001 1 a001 000 0000
001 2 a002 000 0000
001 3 c001 000 0000
001 4 c002 000 0000
002 1 b001 000 0000
002 2 b002 000 0000
002 3 d002 000 0000
002 4 d004 000 0000
源表的data是条件 凡是为0000的 都要插入到目标表
5 个解决方案
#1
lz 排下版了.分不请两表的 字段....
#2
这个为什么要用触发器?
另外两个表的结构好像是一样的,,,
另外两个表的结构好像是一样的,,,
#3
下班了...
#4
使用触发器不太合适,可以考虑用存储过程,,,
create table tab1(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
create table tab2(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
insert tab1 select '001', 1, 'a001', '000', '0000'
union all select '001', 2, 'a002', '000', '0000'
union all select '002', 1, 'b001', '000', '0000'
union all select '002', 2, 'b002', '000', '0000'
insert tab2 select '001', 1, 'c001', '000', '0000'
union all select '001', 2, 'c002', '000', '0000'
union all select '002', 1, 'd001', '000', '000'
union all select '002', 2, 'd002', '000', '0000'
union all select '002', 3, 'd004', '000', '0000'
insert tab1(mp_no, prd_no, [name], date, itm) select mp_no, prd_no, [name], date
,itm=(select max(itm) from tab1 where mp_no=tab2.mp_no)
+(select count(1) from tab2 T where mp_no=tab2.mp_no and date='0000' and prd_no<=tab2.prd_no)
from tab2 where date='0000'
/*
mp_no itm prd_no name date
-------- ----------- -------- ---------------- --------
001 1 a001 000 0000
001 2 a002 000 0000
001 3 c001 000 0000
001 4 c002 000 0000
002 1 b001 000 0000
002 2 b002 000 0000
002 3 d002 000 0000
002 4 d004 000 0000
(8 row(s) affected)
*/
select * from tab1 order by mp_no,itm
drop table tab1,tab2
#5
用存储过程:
create table tab1(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
create table tab2(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
insert tab1 select '001', 1, 'a001', '000', '0000'
union all select '001', 2, 'a002', '000', '0000'
union all select '002', 1, 'b001', '000', '0000'
union all select '002', 2, 'b002', '000', '0000'
insert tab2 select '001', 1, 'c001', '000', '0000'
union all select '001', 2, 'c002', '000', '0000'
union all select '002', 1, 'd001', '000', '000'
union all select '002', 2, 'd002', '000', '0000'
union all select '002', 3, 'd004', '000', '0000'
go
create procedure sp_Test
as
begin
insert tab1(mp_no, prd_no, [name], date, itm) select mp_no, prd_no, [name], date
,itm=(select max(itm) from tab1 where mp_no=tab2.mp_no)
+(select count(1) from tab2 T where mp_no=tab2.mp_no and date='0000' and prd_no<=tab2.prd_no)
from tab2 where date='0000'
end
go
exec dbo.sp_Test
select * from tab1 order by mp_no,itm
/*
mp_no itm prd_no name date
-------- ----------- -------- ---------------- --------
001 1 a001 000 0000
001 2 a002 000 0000
001 3 c001 000 0000
001 4 c002 000 0000
002 1 b001 000 0000
002 2 b002 000 0000
002 3 d002 000 0000
002 4 d004 000 0000
(8 row(s) affected)
*/
drop procedure dbo.sp_Test
drop table tab1,tab2
#1
lz 排下版了.分不请两表的 字段....
#2
这个为什么要用触发器?
另外两个表的结构好像是一样的,,,
另外两个表的结构好像是一样的,,,
#3
下班了...
#4
使用触发器不太合适,可以考虑用存储过程,,,
create table tab1(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
create table tab2(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
insert tab1 select '001', 1, 'a001', '000', '0000'
union all select '001', 2, 'a002', '000', '0000'
union all select '002', 1, 'b001', '000', '0000'
union all select '002', 2, 'b002', '000', '0000'
insert tab2 select '001', 1, 'c001', '000', '0000'
union all select '001', 2, 'c002', '000', '0000'
union all select '002', 1, 'd001', '000', '000'
union all select '002', 2, 'd002', '000', '0000'
union all select '002', 3, 'd004', '000', '0000'
insert tab1(mp_no, prd_no, [name], date, itm) select mp_no, prd_no, [name], date
,itm=(select max(itm) from tab1 where mp_no=tab2.mp_no)
+(select count(1) from tab2 T where mp_no=tab2.mp_no and date='0000' and prd_no<=tab2.prd_no)
from tab2 where date='0000'
/*
mp_no itm prd_no name date
-------- ----------- -------- ---------------- --------
001 1 a001 000 0000
001 2 a002 000 0000
001 3 c001 000 0000
001 4 c002 000 0000
002 1 b001 000 0000
002 2 b002 000 0000
002 3 d002 000 0000
002 4 d004 000 0000
(8 row(s) affected)
*/
select * from tab1 order by mp_no,itm
drop table tab1,tab2
#5
用存储过程:
create table tab1(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
create table tab2(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
insert tab1 select '001', 1, 'a001', '000', '0000'
union all select '001', 2, 'a002', '000', '0000'
union all select '002', 1, 'b001', '000', '0000'
union all select '002', 2, 'b002', '000', '0000'
insert tab2 select '001', 1, 'c001', '000', '0000'
union all select '001', 2, 'c002', '000', '0000'
union all select '002', 1, 'd001', '000', '000'
union all select '002', 2, 'd002', '000', '0000'
union all select '002', 3, 'd004', '000', '0000'
go
create procedure sp_Test
as
begin
insert tab1(mp_no, prd_no, [name], date, itm) select mp_no, prd_no, [name], date
,itm=(select max(itm) from tab1 where mp_no=tab2.mp_no)
+(select count(1) from tab2 T where mp_no=tab2.mp_no and date='0000' and prd_no<=tab2.prd_no)
from tab2 where date='0000'
end
go
exec dbo.sp_Test
select * from tab1 order by mp_no,itm
/*
mp_no itm prd_no name date
-------- ----------- -------- ---------------- --------
001 1 a001 000 0000
001 2 a002 000 0000
001 3 c001 000 0000
001 4 c002 000 0000
002 1 b001 000 0000
002 2 b002 000 0000
002 3 d002 000 0000
002 4 d004 000 0000
(8 row(s) affected)
*/
drop procedure dbo.sp_Test
drop table tab1,tab2