无法绑定多部分标识符“si.shi”

时间:2010-06-23 09:01:44

标签: sql sql-server sql-server-2005 tsql

我收到错误,如下所示:

无法绑定多部分标识符“si.shi”。

执行此SQL时:

;WITH myCTE AS
(Select mci.* from
view_name AS si
JOIN merch_ctlg_ipt_view_name AS mci
  ON mci.view_id = si.view_id
  AND mci.resolved_view_name_id = si.view_name_id
  AND mci.ctg_ipt_event_id = @ctg_ipt_event_id
  AND mci.accept_flag = 'y')

UPDATE view_name
SET name = (select mci.name from myCTE where myCTE.view_id = view_id)
,view_name_ctg_id = (select mci.resolved_view_name_ctg_id from myCTE where myCTE.view_id = view_id)
,xref_value = (select mci.xref_value from myCTE where myCTE.view_id = view_id)
,availability_start_date = (select mci.availability_start_date from myCTE where myCTE.view_id = view_id)
,availability_end_date = (select mci.availability_end_date from myCTE where myCTE.view_id = view_id)
,status_code = (select mci.status_code from myCTE where myCTE.view_id = view_id)
,last_modified_user_id = (select CASE WHEN mci.last_modified_user_id = 42 THEN @posting_user_id ELSE mci.last_modified_user_id END from myCTE where myCTE.view_id = view_id)
,last_modified_timestamp = CURRENT_TIMESTAMP
WHERE si.shi_flag = 'n'
and exists (select view_id from merch_ctlg_ipt_view_name AS mci
  Where mci.view_id = view_name.view_id
  AND mci.resolved_view_name_id = view_name.view_name_id
  AND mci.ctg_ipt_event_id = @ctg_ipt_event_id
  AND mci.accept_flag = 'y')

以下是视图和表格的结构:

CREATE TABLE [dbo].[Merch_ctlg_ipt_View_Name](
    [ctlg_ipt_event_id] [int] NOT NULL,
    [supplier_id] [int] NOT NULL,
    [View_Name_id] [int] NOT NULL,
    [name] [nvarchar](50) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NOT NULL,
    [resolved_View_Name_id] [int] NULL,
    [packaged_item_name] [nvarchar](50) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NULL,
    [packaged_item_id] [int] NULL,
    [View_Name_code] [nvarchar](50) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NOT NULL,
    [status_code] [nchar](1) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NOT NULL,
    [action_code] [nchar](1) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NOT NULL,
    [xref_value] [nvarchar](50) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NULL,
    [View_Name_ctg_name] [nvarchar](50) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NULL,
    [resolved_View_Name_ctg_id] [int] NULL,
    [packaged_unit_of_measure_name] [nvarchar](50) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NOT NULL,
    [packaged_unit_of_measure_factor] [numeric](14, 6) NULL,
    [resolved_packaged_unit_of_measure_id] [int] NULL,
    [package_weight] [numeric](16, 8) NULL,
    [availability_start_date] [datetime] NULL,
    [availability_end_date] [datetime] NULL,
    [client_id] [int] NOT NULL,
    [last_mod_user_id] [int] NOT NULL,
    [weight_unit_of_measure_name] [nvarchar](50) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NULL,
    [last_mod_timestamp] [datetime] NOT NULL,
    [resolved_weight_unit_of_measure_id] [int] NULL,
    [accept_flag] [nchar](1) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NOT NULL DEFAULT ('y'),
    [View_Name_ctg_description] [nvarchar](255) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NULL,
    [changed_catch_weight_flag] [nchar](1) COLLATE SQL_AltDiction_Pref_CP850_CI_AS NOT NULL DEFAULT ('n'),
 CONSTRAINT [PK_Merch_ctlg_ipt_View_Name] PRIMARY KEY CLUSTERED 
(
    [ctlg_ipt_event_id] ASC,
    [supplier_id] ASC,
    [View_Name_id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


SELECT     l.supplier_id, l.View_Name_id, b.View_Name_ctg_id, b.name, b.description, b.brand_id, b.status_code, l.last_mod_user_id, 
                      l.last_mod_timestamp, l.client_id, b.manufacturer_id, b.mfr_id_edit_flag, b.usage, b.xref_value, b.ingredients, b.shi_flag, 
                      b.country_of_origin_type_code, b.availability_start_date, b.availability_end_date, b.country_of_origin_required_flag, b.exception_status_code, 
                      b.resolved_View_Name_id, b.maximum_order_quantity, b.item_id, b.item_hierarchy_id, l.first_order_multiplier, l.second_order_multiplier, 
                      l.first_order_round_rule, l.second_order_round_rule, b.original_supplier_id
FROM         dbo.View_Name_Su_List AS l LEFT OUTER JOIN
                      dbo.View_Name_Base AS b ON b.View_Name_id = l.View_Name_id

请帮忙

3 个答案:

答案 0 :(得分:1)

问题在于这一行:

WHERE si.shi_flag = 'n'

更新查询中未定义si别名。

应该是:

WHERE shi_flag = 'n'

更新(以下评论)

您还应该更改此行:

SET name = (select mci.name from myCTE where myCTE.view_id = view_id)

由于子查询引用了一个未声明的别名:

SET name = (select name from myCTE where myCTE.view_id = view_id)

答案 1 :(得分:0)

您的更新使用si.shi。

此别名仅在CTE中定义。从更新中删除别名应该可以解决错误。

;WITH myCTE AS
(Select mci.* from
view_name AS si
JOIN merch_ctlg_ipt_view_name AS mci
  ON mci.view_id = si.view_id
  AND mci.resolved_view_name_id = si.view_name_id
  AND mci.ctg_ipt_event_id = @ctg_ipt_event_id
  AND mci.accept_flag = 'y')

UPDATE view_name
SET name = (select mci.name from myCTE where myCTE.view_id = view_id)
,view_name_ctg_id = (select mci.resolved_view_name_ctg_id from myCTE where myCTE.view_id = view_id)
,xref_value = (select mci.xref_value from myCTE where myCTE.view_id = view_id)
,availability_start_date = (select mci.availability_start_date from myCTE where myCTE.view_id = view_id)
,availability_end_date = (select mci.availability_end_date from myCTE where myCTE.view_id = view_id)
,status_code = (select mci.status_code from myCTE where myCTE.view_id = view_id)
,last_modified_user_id = (select CASE WHEN mci.last_modified_user_id = 42 THEN @posting_user_id ELSE mci.last_modified_user_id END from myCTE where myCTE.view_id = view_id)
,last_modified_timestamp = CURRENT_TIMESTAMP
WHERE shi_flag = 'n'
and exists (select view_id from merch_ctlg_ipt_view_name AS mci
  Where mci.view_id = view_name.view_id
  AND mci.resolved_view_name_id = view_name.view_name_id
  AND mci.ctg_ipt_event_id = @ctg_ipt_event_id
  AND mci.accept_flag = 'y')

答案 2 :(得分:0)

为什么选择CTE?这会不会更清楚地达到同样的目的吗?

编辑:更新了更新语句的正确别名

UPDATE si
SET  name = mci.name
    ,view_name_ctg_id = mci.resolved_view_name_ctg_id 
    ,xref_value = mci.xref_value 
    ,availability_start_date = mci.availability_start_date 
    ,availability_end_date = mci.availability_end_date 
    ,status_code = mci.status_code 
    ,last_modified_user_id = CASE WHEN mci.last_modified_user_id = 42 THEN @posting_user_id ELSE mci.last_modified_user_id END 
    ,last_modified_timestamp = CURRENT_TIMESTAMP
FROM    view_name as si
        INNER JOIN
            merch_ctlg_ipt_view_name as mci
            ON mci.view_id = si.view_id
            AND mci.resolved_view_name_id = si.view_name_id
WHERE   mci.ctg_ipt_event_id = @ctg_ipt_event_id
AND     mci.accept_flag = 'y'
AND     si.shi_flag = 'n'