2016-01-24 28 views
1

我想插入值到表中,但是當我這樣做時,值不會進入我期望他們來到表的底部的列。請看圖片。我想在空白欄添加行到另一個表插入語句

INSERT INTO storiacloud.schl_storia_school_status_try 
(no_of_orders) 
select count(otc_order_number) 
from storiacloud.vw_storia_oms_orders as a 
inner join 
storiacloud.schl_storia_school_status_try as b 
on a.school_ucn = b.ucn 
group by school_ucn; 

我也嘗試更新...但它給我的語法錯誤錯誤:在或接近「更新」 位置語法錯誤:188

更新牛逼 ^ 請幫助

;With tbl as 
(
Select Count(1) as cnt, b.ucn 
FROM 
storiacloud.schl_storia_school_status_try b 
INNER JOIN 
storiacloud.vw_storia_oms_orders a ON a.school_ucn = b.ucn 
Group By school_ucn 
) 
update t 
SET no_of_orders = tbl.Cnt 
From tbl 
join storiacloud.schl_storia_school_status_try as t on t.ucn = tbl.ucn 

enter image description here

結果數據的圖像enter image description here

+1

您必須使用Update命令而不是Insert!插入將會添加新的列。你想要的是更新現有的數據! **如果我理解正確** –

+0

K。。我也嘗試更新......但不知道放在哪裏了...... UPDATE storiacloud.schl_storia_school_status_try SET no_of_orders FROM storiacloud.schl_storia_school_status_try INNER JOIN storiacloud.vw_storia_oms_orders ON a.school_ucn = b.ucn WHERE計數(otc_order_number) a.school_ucn = b.ucn 組由school_ucn; –

+0

在你的問題上添加你的結果數據應該是什麼樣子。 –

回答

0

希望這是你在找什麼。 此代碼解析正常但未測試

;With tbl as 
(
Select Count(1) as cnt, b.ucn 
FROM 
storiacloud.schl_storia_school_status_try b 
INNER JOIN 
storiacloud.vw_storia_oms_orders a ON a.school_ucn = b.ucn 
Group By school_ucn 
) 
update t 
SET no_of_orders = tbl.Cnt 
From tbl 
join storiacloud.schl_storia_school_status_try as t on t.ucn = tbl.ucn 
+0

ERROR:在或接近 「更新」 位置語法錯誤:194 更新噸 ^ –

+0

請更新您的代碼 – Hiten004

+0

感謝hiten ...您的幫助非常感謝 –

相關問題