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
如果my_item_no列在#my_temp_table那麼你SELECT DISTINCT子句,因爲FROM子句僅指定一個表叫contact_connections您不能引用它。爲所有表格別名並始終使用別名是一個好主意,以避免此類問題。 – 2015-03-13 16:11:37
問題已解決,我設法將IN(select ...)語句轉換爲JOINS。所以現在我可以毫無問題地使用my_item_no。 – 2015-03-17 12:03:02