2015-03-13 57 views
0

下面是SQL数据库过程,它负责根据临时表的输入添加permission_rows。问题是,当我运行这个时收到'无效的列名'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 
+0

如果my_item_no列在#my_temp_table那么你SELECT DISTINCT子句,因为FROM子句仅指定一个表叫contact_connections您不能引用它。为所有表格别名并始终使用别名是一个好主意,以避免此类问题。 – 2015-03-13 16:11:37

+0

问题已解决,我设法将IN(select ...)语句转换为JOINS。所以现在我可以毫无问题地使用my_item_no。 – 2015-03-17 12:03:02

回答

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