請問下面資料,sql update的語法? |
答題得分者是:l630521
|
pedro
尊榮會員 發表:152 回覆:1187 積分:892 註冊:2002-06-12 發送簡訊給我 |
請教大家,我的資料結構如下
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 發送簡訊給我 |
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 發送簡訊給我 |
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)='' .....................
.楛耕傷稼,楛耘失歲.
.....................
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |