简体   繁体   English

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

[英]Invalid column name from temporary table in stored procedure subquery

Below is the SQL database procedure responsible for adding permission_rows based on input from a temporary table. 下面是SQL数据库过程,负责根据临时表中的输入添加Permission_rows。 The issue is, I receive 'Invalid column name 'my_item_no' when I run this. 问题是,运行此程序时,我收到“无效的列名” my_item_no”。 The starting point of the search for who to give permissions to is the 'my_item_no' from the '#my_temp_table', which is nested twice. 从“ #my_temp_table”中搜索“ my_item_no”,这是嵌套两次的搜索的起点。 So SQL doesn't know the column at the top. 因此,SQL不知道顶部的列。

I tried rewriting it partially with joins, but failed as I need the 'IN', because a document can be connected to multiple handlings and a handling can be connected to multiple meetings. 我尝试使用连接部分重写它,但是由于需要“ IN”而失败,因为文档可以连接到多个处理,并且一个处理可以连接到多个会议。

I tried using a variable @my_item, settings it's value to 'my_item_no' right after the 'begin', and using that in the select line, but that only set permissions on the first document found. 我尝试使用变量@my_item,在“开始”之后立即将其值设置为“ my_item_no”,并在选择行中使用该变量,但这仅对找到的第一个文档设置权限。

Suggestions are welcome :) 欢迎提出建议:)

/* 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM