日期:2014-05-18  浏览次数:20500 次

比对数据库问题--在线等
我有一个基础库还有一个更新包,现在都在同一个库中,基础库BZML,更新包BXB,
两个表中的字段几乎相同,基础库包含更新包
现在想把BXB中的内容与BZML进行比对,A100为比对字段,如果A100相同则更新BZML里的A107、A108、A109、A110等字段,
如果BXB里的A100表BZML里面没有,就把BXB中A100这组数据里的A100、A107、A108、A109、A110等写入BZML

------解决方案--------------------
顶一下
------解决方案--------------------
一个表一个表的写吧,没什么好办法

如果表名的命名有规则,可以用动态语句就简单一些

SQL code
update a set a107=b.a107,a108=b.a108,....,a200=b.a200
    from bzml_tb1 a
inner join bxb_tb1 b
    on a.a100=b.a100
insert bzml_tb1 (a100,a107,a108,...,a200)
select b.a100,b.a107,...,b.a200
    from bxb_tb1 b
left join bzml_tb1 a 
    on a.a100=b.a100
where a.a100 is null

update a set a107=b.a107,a108=b.a108,....,a200=b.a200
    from bzml_tb2 a
inner join bxb_tb2 b
    on a.a100=b.a100
insert bzml_tb2 (a100,a107,a108,...,a200)
select b.a100,b.a107,...,b.a200
    from bxb_tb2 b
left join bzm_tbN a 
    on a.a100=b.a100
where a.a100 is null

.....
update a set a107=b.a107,a108=b.a108,....,a200=b.a200
    from bzml_tbN a
inner join bxb_tbN b
    on a.a100=b.a100
insert bzml_tbN (a100,a107,a108,...,a200)
select b.a100,b.a107,...,b.a200
    from bxb_tbN b
left join bzml_tbN a 
    on a.a100=b.a100
where a.a100 is null

------解决方案--------------------
insert into xz_bzml 
select * --不行的话改为字段名A100、A107、A108、A109、A110
from bxb
where a100 not in (select distinct a100 from xz_bzml)
------解决方案--------------------
fcuandy 的写法里已经包含插入了,应该没问题

------解决方案--------------------
insert 没有into 能写入吗

t-sql 不需要into也可插
jetsql必须要