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



下面是负责根据临时表的输入添加permission_rows的SQL数据库过程。问题是,当我运行它时,我收到"无效的列名'my_item_no'。搜索向谁授予权限的起点是嵌套两次的"#my_temp_table"中的"my_item_no"。所以 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
    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

最新更新