全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1684
推到 Plurk!
推到 Facebook!

請問下面資料,sql update的語法?

答題得分者是:l630521
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-11-24 10:23:00 IP:60.248.xxx.xxx 未訂閱
請教大家,我的資料結構如下
empno1 empno2 amount1     amount2     
------ ------ ----------- ----------- 
0010          0           0
0010   0011   0           10
0010   0012   0           15
0010   0011   0           20
我想整理資料成
empno1 empno2 amount1     amount2     
------ ------ ----------- ----------- 
0010          45          0
0010   0011   0           10
0010   0012   0           15
0010   0011   0           20
下面是我的實驗語法
declare @xtable table(empno1 char(4),empno2 char(4),amount1 integer,amount2 integer)    insert @xtable 
   select '0010','',0,0    insert @xtable 
   select '0010','0011',0,10    insert @xtable 
   select '0010','0012',0,15    insert @xtable 
   select '0010','0011',0,20    select * from  @xtable    
update @xtable set amount1=t.amount2
from (select empno1,Sum(amount2) as amount2 from @xtable group by empno1) as t
WHERE @xtable.empno1=t.empno1    
請問我的Update語法會出現 Server: Msg 137, Level 15, State 2, Line 19 Must declare the variable '@xtable'. 請教前輩,我要怎麼改,才能行得通? 謝謝您的熱心幫助... ps:ms-sqlserver2000 ..................... .楛耕傷稼,楛耘失歲. ..................... 發表人 - pedro 於 2005/11/24 10:25:35
l630521
資深會員


發表:15
回覆:349
積分:372
註冊:2003-02-17

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-11-24 11:04:19 IP:211.74.xxx.xxx 未訂閱
declare @xtable table(empno1 char(4),empno2 char(4),amount1 integer,amount2 integer)    insert @xtable     select '0010','',0,0    insert @xtable     select '0010','0011',0,10    insert @xtable     select '0010','0012',0,15    insert @xtable     select '0010','0011',0,20    select * from  @xtable    update @xtable set amount1=t.amount2 from @xtable as a, (select empno1,Sum(amount2) as amount2 from @xtable group by empno1) as t WHERE a.empno1=t.empno1 select * from @xtable Ian
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-11-24 11:26:51 IP:60.248.xxx.xxx 未訂閱
l630521大大,可以了,謝謝你 update @xtable set amount1=t.amount2 from @xtable as a, (select empno1,Sum(amount2) as amount2 from @xtable group by empno1) as t WHERE a.empno1=t.empno1 and rtrim(a.empno2)='' ..................... .楛耕傷稼,楛耘失歲. .....................
系統時間:2024-05-19 3:00:08
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!