0
我正在使用ms-sql服務器。我有table
,我想update
從select statement
。例如,我想更新的表格是Table_A
,其中有2 rows
。我想更新Table_A的更新語句返回10 rows
。所以我想要update Table_A 10 times
。問題是Table_A是updated 2 times
(the count of rows in Table_A
)。從選擇更新表
例子:
CREATE TABLE #tmp
(
AccountID INT,
Inflow DECIMAL(10,2)
)
DECLARE @n INT = 0
WHILE (@n <10)
BEGIN
INSERT INTO #tmp SELECT 2, 10
SET @n += 1
END
UPDATE dbo.Table_A
SET Balance += sss.Inflow
FROM (SELECT t.AccountID ,
t.Inflow
FROM #tmp AS t
) AS sss
WHERE dbo.tAccount.AccountID = sss.AccountID;
-- Updates only 2 times
-- What I expected here is Table_A to be updated as many times as the count of the select statement which is 10, based on the insert before.
首先,我猜你正在使用基於語法的SQL Server。其次,你的期望是錯誤的。一行只更新一次。 –
是的,它只更新一次。我的期望是根據select語句的計數更新10次,語法如下。 – TheChampp
您只能更新表A中的內容。如果只有2條記錄,則只能更新這2條記錄。你需要插入其他8。 –