------解决方案-------------------- with tb as
(
select b.*, MAX(a.变更版本号) + 1 as aaa from b
inner join A on a.单别 = b.单别
and a.单号 = b.单号
group by b.单别, b.单号
)
insert into a
select * from tb ------解决方案--------------------
INSERT TABLEA(单别,单号,变更版本号)
SELECT b.单别,b.单号,isnull(max(a.变更版本号),0)+1
FROM TABLEB b left join TABLEA a
on a.单别 = b.单别
and a.单号 = b.单号
------解决方案--------------------
insert into tableA select B.单别,B.单号,max(A.变更号)+1 from tableA A join tableB B on A.单别=B.单别 and A.单号=B.单号 group by B.单别,B.单号
------解决方案--------------------
呵呵,改了一下,试试:
INSERT TABLEA(单别,单号,变更版本号)
SELECT b.单别,b.单号,
right('000'+cast(isnull(max(a.变更版本号),0)+1 as varchar),3)
FROM TABLEB b left join TABLEA a
on a.单别 = b.单别
and a.单号 = b.单号
GROUP BY b.单别,b.单号