[英]I am trying to update a table based on matches to another table, getting a multi-part identifier error
I am having an issue with trying to update a table with values from another table when a match doesn't exist. 当不存在匹配项时,尝试用另一个表中的值更新表时遇到问题。 Here is the scenario, I have a participation table that has about 250K records. 在这种情况下,我有一个包含约250K记录的参与表。 I need to find the legislative districts associated with the records based on a zip + 4 code. 我需要根据邮政编码+ 4代码找到与记录相关的立法地区。 My participation table has the zip and the zip4 codes. 我的参与表包含邮政编码和zip4代码。 Sometimes I have both, sometimes neither. 有时我两者都有,有时两者都没有。 Gotta love data sets like this. 一定喜欢这样的数据集。 I have a second table which contains all of the legislative districts by 9 digit zip codes. 我还有第二张表,其中包含所有由9位邮政编码组成的立法区。 I have updated the data set and included the individual zip and zip4 codes. 我已经更新了数据集,并包括了各自的zip和zip4代码。 Here is the code that I have: 这是我的代码:
update
[ComEd].[dbo].[PP_FFR_BU]
set
[Zip4] =
(
CASE
when
[Tim].[dbo].[PP_FFR_BU].[Zip] + [Tim].[dbo].[PP_FFR_BU].[Zip4] not in (select zip from [TimCIS].[dbo].[Legis_Districts])
then
CASE
when [Tim].[dbo].[PP_FFR_BU].[Zip] = [TimCIS].[dbo].[Legis_Districts].[Zip5] and [Tim].[dbo].[PP_FFR_BU].[Zip4] <> [TimCIS].[dbo].[Legis_Districts].[Zip4]
then (select top 1 [ComEdCIS].[dbo].[Legis_Districts].[Zip4] from [TimCIS].[dbo].[Legis_Districts] where [Tim].[dbo].[PP_FFR_BU].[Zip] = [TimCIS].[dbo].[Legis_Districts].[Zip5])
when len([Tim].[dbo].[PP_FFR_BU].[Zip])<5
then '0000'
end
end
)
where
[Tim].[dbo].[PP_FFR_BU].[Zip] + [Tim].[dbo].[PP_FFR_BU].[Zip4] not in (select zip from [TimCIS].[dbo].[Legis_Districts])
Now I am getting an error: 现在我得到一个错误:
Msg 4104, Level 16, State 1, Line 11 The multi-part identifier "TimCIS.dbo.Legis_Districts.Zip4" could not be bound. 消息4104,级别16,状态1,行11无法绑定多部分标识符“ TimCIS.dbo.Legis_Districts.Zip4”。 Which equates to the when clause in the second CASE statement. 它等于第二条CASE语句中的when子句。
If the zip and zip4 in the original table are in the legislative district table then I need to move to the next record. 如果原始表中的zip和zip4位于立法区表中,那么我需要移至下一条记录。 If the zip is in the table and the zip4 doesn't match, I need the first value that matches. 如果zip在表中,而zip4不匹配,则需要第一个匹配的值。 If the zip doesn't exist in the legislative table then I want to put a '0000' in the zip4 of the participation table. 如果该立法机构表中不存在该邮政编码,那么我想在参与表的zip4中放入一个“ 0000”。 Any insight would be greatly appreciated. 任何见识将不胜感激。
In the case statement you have this query: 在case语句中,您具有以下查询:
then (select top 1 [ComEdCIS].[dbo].[Legis_Districts].[Zip4] from [TimCIS].[dbo].[Legis_Districts] where [Tim].[dbo].[PP_FFR_BU].[Zip] = [TimCIS].[dbo].[Legis_Districts].[Zip5])
But this part doesn't make sense: 但是这部分没有意义:
select top 1 [ComEdCIS].[dbo].[Legis_Districts].[Zip4] from [TimCIS].[dbo].[Legis_Districts]
You are selecting a column from a table that isn't in your from clause. 您正在从表中选择不在from子句中的列。 They are in two different databases. 它们位于两个不同的数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.