存储过程子查询中临时表的列名无效

时间:2015-03-13 15:16:37

标签: sql-server stored-procedures

以下是SQL数据库过程,负责根据临时表的输入添加permission_rows。问题是,我收到了无效的列名称' my_item_no'当我跑这个。搜索权限的起点是' my_item_no'来自' #my_temp_table',它嵌套了两次。所以SQL并不知道顶部的列。

我尝试使用连接部分重写它,但因为我需要' IN'而失败,因为文档可以连接到多个处理,并且处理可以连接到多个会议。

我尝试使用变量@my_item,将其值设置为' my_item_no'在'开始'之后,并在选择行中使用它,但仅在找到的第一个文档上设置权限。

欢迎提出建议:)

/* The procedure is given unique numbers of documents in the DB, from a temporary table '#my_temp_table' */
/* The procedure grant permissions to participants on a meeting by adding rows in a permission table by using the numbers from the #my_temp_table */
/* To find out which contacts to grant permissions to, it needs to check a couple of things */
/* - We start with the document number from the #my_temp_table, and find out to which meeting handlings it's attached */
/* - Then we check to which meetings all found handlings are connected */
/* - Then we check which contacts are registered as attendees on all found meetings and we add the permissions_rows */

CREATE procedure [dbo].[sp_CUST_members_bulk]
 (@entity varchar(2000), @access int, @rights varchar(2000), @token int)
as 
begin
    set nocount on;
    if @entity = 'Document' begin
        insert into dbo.permissionstable (contact, entity, document, access, rights, token) 
        select DISTINCT(contact_contactperson_no * -1), 'Activity', my_item_no, @access, @rights, @token
        from    contact_connections 
        where   contact_activities_no IN
            (select T2.activity_no from dbo.activities T1
                left outer join dbo.activity_connection T3 on T1.activity_no = T3.activity_connectto
                left outer join dbo.activities T2 on T3.activity_connectfrom = T2.activity_no
                and T2.activity_activity_type = 10 /* type 10 = meeting */
                where T1.activity_no IN
                    (select activity_connectfrom from dbo.activity_connection T4 
                    join #my_temp_table on T4.activity_connectto = my_item_no
                    and T1.activity_activity_type = 8) /* type 8 = meeting handling */
            and T1.activity_status = 16) /* status 16 = 'document is on the agenda' */
        and contact_no <> 0
        and contact_contactperson_no <> 0
    end; 
end;
GO

1 个答案:

答案 0 :(得分:0)

    select DISTINCT(contact_contactperson_no * -1), 'Activity', my_item_no, @access, @rights, @token
    from    activities T1
    left outer join dbo.activity_connection T3 on T1.activity_no = T3.activity_connectto andactivity_connect_role_no = 5
    left outer join dbo.activities T2 on T3.activity_connectfrom = T2.activity_no
    join contact_connections on contact_activities_no = T2.activity_no
    left outer join activity_status T5 on T1.activity_status = T5.activity_status_no and T5.Activity_status_language = N'ENU'
    join dbo.activity_connection T6 on T1.activity_no = T6.activity_connectfrom
    inner join #my_temp_table on T6.activity_connectto = my_item_no
    where T1.activity_activity_type = 8
    and T2.activity_activity_type = 10
    and T1.activity_status = 16
    and contact_role_no IN (3, 10)
    and contact_no <> 0
    and contact_contactperson_no <> 0