简体   繁体   English

我正在尝试根据与另一个表的匹配来更新表,但出现多部分标识符错误

[英]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.

相关问题 为什么在SQL中收到多部分标识符错误? - Why am I getting a multi-part identifier error in SQL? 我正在尝试编写附加的查询,但收到错误“无法绑定多部分标识符” - I am trying to write the query attached, but getting the error “multi-part identifier could not be bound” 我正在使用Asp.net更新SQL表中的“图像”字段。 它给了我一个多部分的标识符绑定错误 - I am updating Image fields in my SQL table using Asp.net. It give me a multi-part Identifier bound error 更新表-多部分标识符无法绑定 - update table - multi-part identifier could not be bound 在多部件标识符上更新日期转换失败的表 - Update table with date conversion failing on Multi-Part Identifier sql update table set - 无法绑定多部分标识符 - sql update table set - The multi-part identifier could not be bound 为什么会出现“无法绑定多部分标识符”错误? - Why am I getting a “Multi-part Identifier could not be bound” error? 为什么会出现此错误以及如何解决该错误:不能绑定多部分标识符“ SEQ.nextval” - Why am I getting this error and how to resolve it : The multi-part identifier “SEQ.nextval” could not be bound 派生表错误:“无法绑定多部分标识符” - Derived Table Error: “The multi-part identifier could not be bound” SQL临时表中的多部分标识符 - Multi-Part Identifier in SQL Temp Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM