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

sql update 问题。。。纠结了好久了。。。大侠们帮帮忙。。
-----
select t2.fbillno,t1.fentryid_src,t2.fitemid
from t_sz_wkorderentry t1,t_sz_wkorder t2 ,seorderentry t3
where t1.fid = t2.fid and t2.fitemid=t3.fitemid and t1.fentryid_src=t3.fentryid
---这段代码执行结果:

fbillno fentryid fitemid
00000029 1 1907
00000036 1 1893
00000037 2 1905
00000038 3 1907
00000039 4 1909
00000040 5 1911
00000041 6 1915
00000042 1 1919

----另外一个表查询结果

select fentryid,fitemid, FEntrySelfS0168 from seorderentry

----结果:
fentryid fitemid FEntrySelfS0168
1 1907 
1 1893 
2 1905 
3 1907 
4 1909 
5 1911 
6 1915 
1 1919 99990
---我用这个语句执行更改时提示错误
update mm set FEntrySelfS0168= b.fbillno
from seorderentry mm inner join  
(select t2.fbillno,t1.fentryid_src,t2.fitemid
from t_sz_wkorderentry t1,t_sz_wkorder t2 ,seorderentry t3
where t1.fid = t2.fid and t2.fitemid=t3.fitemid and t1.fentryid_src=t3.fentryid ) b  
on mm.fentryid = b.fentryid_src and mm.fitemid = b.fitemid
-----错误提示:
消息 512,级别 16,状态 1,过程 bb,第 5 行
子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
语句已终止。


大侠们帮忙弄一下看。。。。具体错在什么地方了。
纠结了好久了。。。



------解决方案--------------------
没问题吧,应该能运行:
SQL code
create table t(fbillno varchar(10),fentryid_src int,fitemid int)
insert into t select '00000029',1,1907
insert into t select '00000036',1,1893
insert into t select '00000037',2,1905
insert into t select '00000038',3,1907
insert into t select '00000039',4,1909
insert into t select '00000040',5,1911
insert into t select '00000041',6,1915
insert into t select '00000042',1,1919
create table seorderentry(fentryid int,fitemid int,FEntrySelfS0168 int)
insert into seorderentry select 1,1907,null
insert into seorderentry select 1,1893,null
insert into seorderentry select 2,1905,null
insert into seorderentry select 3,1907,null
insert into seorderentry select 4,1909,null
insert into seorderentry select 5,1911,null
insert into seorderentry select 6,1915,null
insert into seorderentry select 1,1919,99990
go
update mm set FEntrySelfS0168= b.fbillno
from seorderentry mm inner join   
(select * from t) b   --用这个查询结果代替你的查询语句
on mm.fentryid = b.fentryid_src and mm.fitemid = b.fitemid
select * from seorderentry
/*
fentryid    fitemid     FEntrySelfS0168
----------- ----------- ---------------
1           1907        29
1           1893        36
2           1905        37
3           1907        38
4           1909        39
5           1911        40
6           1915        41
1           1919        42

(8 行受影响)

*/
go
drop table t,seorderentry