我想更新在其他表上进行联接的表中的列,例如:
UPDATE table1 a
INNER JOIN table2 b ON a.commonfield = b.[common field]
SET a.CalculatedColumn= b.[Calculated Column]
WHERE
b.[common field]= a.commonfield
AND a.BatchNO = '110'
但它在抱怨:
消息170,第15级,州1,第2行
第2行:“ a”附近的语法不正确。
怎么了
#1楼
我发现将UPDATE转换为SELECT以获取要更新的行作为测试之前的测试很有用。 如果可以选择所需的确切行,则可以仅更新要更新的行。
DECLARE @expense_report_id AS INT
SET @expense_report_id = 1027
--UPDATE expense_report_detail_distribution
--SET service_bill_id = 9
SELECT *
FROM expense_report_detail_distribution erdd
INNER JOIN expense_report_detail erd
INNER JOIN expense_report er
ON er.expense_report_id = erd.expense_report_id
ON erdd.expense_report_detail_id = erd.expense_report_detail_id
WHERE er.expense_report_id = @expense_report_id
#2楼
尝试:
UPDATE table1
SET CalculatedColumn = ( SELECT [Calculated Column]
FROM table2
WHERE table1.commonfield = [common field])
WHERE BatchNO = '110'
#3楼
像这样尝试:
begin tran
UPDATE a
SET a.CalculatedColumn= b.[Calculated Column]
FROM table1 a INNER JOIN table2 b ON a.commonfield = b.[common field]
WHERE a.BatchNO = '110'
commit tran
#4楼
您不太了解SQL Server专有的UPDATE FROM
语法。 同样也不确定为什么您需要加入CommonField
并随后对其进行过滤。 尝试这个:
UPDATE t1
SET t1.CalculatedColumn = t2.[Calculated Column]
FROM dbo.Table1 AS t1
INNER JOIN dbo.Table2 AS t2
ON t1.CommonField = t2.[Common Field]
WHERE t1.BatchNo = '110';
如果您做的事情确实很愚蠢(例如不断尝试将一列的值设置为另一列的集合(这违反了避免存储冗余数据的原则),则可以使用CTE(公用表表达式)-请参见此处并在此处获取更多详细信息:
;WITH t2 AS
(
SELECT [key], CalculatedColumn = SUM(some_column)
FROM dbo.table2
GROUP BY [key]
)
UPDATE t1
SET t1.CalculatedColumn = t2.CalculatedColumn
FROM dbo.table1 AS t1
INNER JOIN t2
ON t1.[key] = t2.[key];
这确实很愚蠢的原因是,每当table2
任何行发生更改时,您都必须重新运行整个更新。 SUM
是您始终可以在运行时计算的内容,因此不必担心结果过时。
#5楼
亚伦在上面给出的答案是完美的:
UPDATE a
SET a.CalculatedColumn = b.[Calculated Column]
FROM Table1 AS a
INNER JOIN Table2 AS b
ON a.CommonField = b.[Common Field]
WHERE a.BatchNo = '110';
只想补充一下为什么当我们尝试在更新表时使用表别名时,SQL Server中会出现此问题的原因,下面提到的语法将始终给出错误:
update tableName t
set t.name = 'books new'
where t.id = 1
如果您要更新单个表或在使用连接时进行更新,则可以是任何情况。
尽管上面的查询在PL / SQL中可以正常工作,但在SQL Server中却不能。
在SQL Server中使用表别名时更新表的正确方法是:
update t
set t.name = 'books new'
from tableName t
where t.id = 1
希望它能对大家有所帮助,为什么会出现错误。
#6楼
另一种方法是使用MERGE
;WITH cteTable1(CalculatedColumn, CommonField)
AS
(
select CalculatedColumn, CommonField from Table1 Where BatchNo = '110'
)
MERGE cteTable1 AS target
USING (select "Calculated Column", "Common Field" FROM dbo.Table2) AS source ("Calculated Column", "Common Field")
ON (target.CommonField = source."Common Field")
WHEN MATCHED THEN
UPDATE SET target.CalculatedColumn = source."Calculated Column";
-合并是SQL标准的一部分
-同样,我非常确定内部联接更新不确定。 html
#7楼
似乎SQL Server 2012也可以处理Teradata的旧更新语法:
UPDATE a
SET a.CalculatedColumn= b.[Calculated Column]
FROM table1 a, table2 b
WHERE
b.[common field]= a.commonfield
AND a.BatchNO = '110'
如果我没记错的话,当我尝试类似的查询时,2008R2给出了错误。
#8楼
UPDATE mytable
SET myfield = CASE other_field
WHEN 1 THEN 'value'
WHEN 2 THEN 'value'
WHEN 3 THEN 'value'
END
From mytable
Join otherTable on otherTable.id = mytable.id
Where othertable.somecolumn = '1234'
此处提供更多替代方法: http : //www.karlrixon.co.uk/writing/update-multiple-rows-with-different-values-and-a-single-sql-query/
#9楼
MERGE table1 T
USING table2 S
ON T.CommonField = S."Common Field"
AND T.BatchNo = '110'
WHEN MATCHED THEN
UPDATE
SET CalculatedColumn = S."Calculated Column";
#10楼
我有同样的问题..并且您不需要添加物理列..因为现在您必须维护它。.您可以做的是在选择查询中添加一个通用列:
例如:
select tb1.col1, tb1.col2, tb1.col3 ,
(
select 'Match' from table2 as tbl2
where tbl1.col1 = tbl2.col1 and tab1.col2 = tbl2.col2
)
from myTable as tbl1
#11楼
亚伦的上述方法对我来说非常有效。 我的更新语句稍有不同,因为我需要基于一个表中串联的两个字段进行联接以匹配另一个表中的字段。
--update clients table cell field from custom table containing mobile numbers
update clients
set cell = m.Phone
from clients as c
inner join [dbo].[COSStaffMobileNumbers] as m
on c.Last_Name + c.First_Name = m.Name