使用 sql 中的选定记录更新现有 table
Update an existing table using selected records in tsql
我遇到了一个查询,我需要使用动态生成的记录集更新现有 table。我试着寻找答案,但没有从他们那里得到太多帮助。
这是我的查询:
with t as
(
SELECT R.Col1, R.Col2, O.Column2, row_number()
OVER (partition by R.Col3, R.Col4
ORDER BY
CASE WHEN @Criteria = 'NA' then R.Col1 END ASC,
CASE WHEN @Criteria = 'ND' then R.Col1 END DESC,
CASE WHEN @Criteria = 'CA' then R.Col2 END ASC,
CASE WHEN @Criteria = 'CD' then R.Col1 END DESC
) as NewOrder
From RecordsTable R innerjoin OtherTable O on R.Col2 = O.Column1
where R.Col3 = @Col5Val
)
Update RecordsTable Set RecordsTable.Ordering = t.NewOrder
where RecordsTable.Name = t.Col1
and RecordsTable.Address = t.Col2
我收到的错误是 The multi-part identifier "t.Col1" could not be bound
和 The multi-part identifier "t.Col2" could not be bound
我不确定查询是否正确,但我想这在某种程度上应该是正确的。
Table 结构
RecordsTable
Col1 | Col2 | Col3 | Col4 | Ordering
------------------------------------------------------------
ABC | 78945 | 8345 | XYZ | 1
OtherTable
Column1 | Column2 | Column3
-----------------------------------
FOO | BAR | 8345
注意
cases
需要对记录进行排序,因为由此生成的 NewOrder
需要更新 RecordsTable
您正在更新您的 CTE 中涉及的 table 之一,但您没有在更新期间将其链接到 CTE。因此,您需要在更新语句中使用内部联接将源 table 联接回 CTE,如下所示:
--CTE here
Update RecordsTable
Set RecordsTable.Ordering = t.NewOrder
from recordstable r
inner join t on
r.Name = t.Col1 and r.Address = t.Col2
或者,您可以在 CTE 中包含名称、地址和订购列,然后像这样直接更新 CTE:
with t as
(
SELECT R.Col1, R.Col2, O.Column2, R.Name, R.Address, R.Ordering, row_number()
OVER (partition by R.Col3, R.Col4
ORDER BY
CASE WHEN @Criteria = 'NA' then R.Col1 END ASC,
CASE WHEN @Criteria = 'ND' then R.Col1 END DESC,
CASE WHEN @Criteria = 'CA' then R.Col2 END ASC,
CASE WHEN @Criteria = 'CD' then R.Col1 END DESC
) as NewOrder
From RecordsTable R innerjoin OtherTable O on R.Col2 = O.Column1
where R.Col3 = @Col5Val
)
Update t
Set t.Ordering = t.NewOrder
where t.Name = t.Col1
and t.Address = t.Col2
我遇到了一个查询,我需要使用动态生成的记录集更新现有 table。我试着寻找答案,但没有从他们那里得到太多帮助。
这是我的查询:
with t as
(
SELECT R.Col1, R.Col2, O.Column2, row_number()
OVER (partition by R.Col3, R.Col4
ORDER BY
CASE WHEN @Criteria = 'NA' then R.Col1 END ASC,
CASE WHEN @Criteria = 'ND' then R.Col1 END DESC,
CASE WHEN @Criteria = 'CA' then R.Col2 END ASC,
CASE WHEN @Criteria = 'CD' then R.Col1 END DESC
) as NewOrder
From RecordsTable R innerjoin OtherTable O on R.Col2 = O.Column1
where R.Col3 = @Col5Val
)
Update RecordsTable Set RecordsTable.Ordering = t.NewOrder
where RecordsTable.Name = t.Col1
and RecordsTable.Address = t.Col2
我收到的错误是 The multi-part identifier "t.Col1" could not be bound
和 The multi-part identifier "t.Col2" could not be bound
我不确定查询是否正确,但我想这在某种程度上应该是正确的。
Table 结构
RecordsTable
Col1 | Col2 | Col3 | Col4 | Ordering
------------------------------------------------------------
ABC | 78945 | 8345 | XYZ | 1
OtherTable
Column1 | Column2 | Column3
-----------------------------------
FOO | BAR | 8345
注意
cases
需要对记录进行排序,因为由此生成的 NewOrder
需要更新 RecordsTable
您正在更新您的 CTE 中涉及的 table 之一,但您没有在更新期间将其链接到 CTE。因此,您需要在更新语句中使用内部联接将源 table 联接回 CTE,如下所示:
--CTE here
Update RecordsTable
Set RecordsTable.Ordering = t.NewOrder
from recordstable r
inner join t on
r.Name = t.Col1 and r.Address = t.Col2
或者,您可以在 CTE 中包含名称、地址和订购列,然后像这样直接更新 CTE:
with t as
(
SELECT R.Col1, R.Col2, O.Column2, R.Name, R.Address, R.Ordering, row_number()
OVER (partition by R.Col3, R.Col4
ORDER BY
CASE WHEN @Criteria = 'NA' then R.Col1 END ASC,
CASE WHEN @Criteria = 'ND' then R.Col1 END DESC,
CASE WHEN @Criteria = 'CA' then R.Col2 END ASC,
CASE WHEN @Criteria = 'CD' then R.Col1 END DESC
) as NewOrder
From RecordsTable R innerjoin OtherTable O on R.Col2 = O.Column1
where R.Col3 = @Col5Val
)
Update t
Set t.Ordering = t.NewOrder
where t.Name = t.Col1
and t.Address = t.Col2