您的位置:程序门 -> ms-sql server -> 基础类



求:更新语句,两个表


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求:更新语句,两个表[已结贴,结贴人:aaronyy2002]
发表于:2007-08-27 15:35:11 楼主
a表
id           name       tt
1               2             2
2               3             3
3               4             2

b表
id           tt
1             2
2
3
我现在需要由a表关联,更新b表tt列的数值
这样的sql语句怎么写,谢谢!
发表于:2007-08-27 15:40:311楼 得分:0

update   a
set   tt=(select   tt   from   b   where   a.id=b.id   )
发表于:2007-08-27 15:42:472楼 得分:0
update   a   from   b
set   tt   =   b.tt   where   a.id=b.id
发表于:2007-08-27 16:12:013楼 得分:0
这种写法只能是单条用,可是我这个是返回多条记录的
发表于:2007-08-27 16:43:354楼 得分:0
不明么意思
发表于:2007-08-27 16:49:295楼 得分:5
是这样把!!!!!

update   b
set   tt=   a.tt
from   a   ,b  
where   b.id   =a.id

发表于:2007-08-27 16:53:476楼 得分:5
--   看这个测试?是不是?

create       table       a   (id       int,name   varchar(10),tt   int   )      
    insert       a            
                          select           1   ,   '2 ',   2    
    union           all               select         2   ,   '3 ',   3  
    union           all               select           3   ,   '4 ',   2  
go
create       table       b   (id       int,tt   int   )      
    insert       b            
                          select           1   ,null
    union           all               select         2   ,null  
    union           all               select           3   ,null  


update   b
set   tt=   a.tt
from   a   ,b  
where   b.id   =a.id
/*

id             tt
-----   -----
1               2
2               3
3               2

*/

drop   table   a

drop   table   b
发表于:2007-08-27 17:43:157楼 得分:10
楼主的问题跟这个很相似,试试触发器
http://community.csdn.net/expert/topic/5729/5729842.xml?temp=.2835504


快速检索

最新资讯
热门点击