首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

查询一张表的结果,在另一张表中做添加或修改解决办法

2012-03-19 
查询一张表的结果,在另一张表中做添加或修改查询一张表的结果,在另一张表中没有name做添加有做修改操作!A

查询一张表的结果,在另一张表中做添加或修改
查询一张表的结果,在另一张表中没有name做添加有做修改操作!

A表
name fraction status
张三 80 0 
李四 70 0
王五 58 1


B表
name fraction
张三 20
王五 58


要个结果是
B表
name fraction
张三 80  
李四 70  
王五 58

[解决办法]

SQL code
--更新update bset b.fraction = a.fractionfrom a join b on a.name = b.name--插入insert into bselect name,fractionfrom a twhere not exists (select 1 from b where name = t.name)
[解决办法]
加个存储过程的创建,把那段放进去就是了。

SQL code
create proc get_UpdateAndInsertasbegin--更新update bset b.fraction = a.fractionfrom a join b on a.name = b.name--插入insert into bselect name,fractionfrom a twhere not exists (select 1 from b where name = t.name)end
[解决办法]
SQL code
create table a(name varchar(10),fraction int,status bit)create table b(name varchar(10),fraction int)insert aselect '张三',80,0 union allselect '李四',70,0 union allselect '王五',58,1insert bselect '张三',20 union allselect '王五',58goinsert b(name,fraction) select a.name,a.fraction from a where not exists(select 1 from b where a.name=b.name)/*(所影响的行数为 1 行)*/goselect * from b/*name  fraction----   -----张三    20王五    58李四    70*/godrop table a,b 

热点排行