2017-03-14 62 views
0

這種情況跟我上一個問題有關,但我已經用了另一種方式,因爲我發現我正在嘗試做的事,我不能在視圖中做,它需要成爲一個功能。創建函數時的錯誤

但是我有連接到該線程的底部的代碼,但我得到這些錯誤:

Msg 4104, Level 16, State 1, Procedure fn_COT_TEST, Line 27 [Batch Start Line 0] 
The multi-part identifier "Contact_Group_Contacts_T.Relationship_Code_ID" could not be bound. 
Msg 4104, Level 16, State 1, Procedure fn_COT_TEST, Line 27 [Batch Start Line 0] 
The multi-part identifier "Contact_Group_Contacts_T.Relationship_Code_ID" could not be bound. 
Msg 4104, Level 16, State 1, Procedure fn_COT_TEST, Line 27 [Batch Start Line 0] 
The multi-part identifier "Contact_Group_Contacts_T.Relationship_Code_ID" could not be bound. 
Msg 4104, Level 16, State 1, Procedure fn_COT_TEST, Line 27 [Batch Start Line 0] 
The multi-part identifier "Contact_Group_Contacts_T.Relationship_Code_ID" could not be bound. 
Msg 4104, Level 16, State 1, Procedure fn_COT_TEST, Line 27 [Batch Start Line 0] 
The multi-part identifier "Contact_Group_Contacts_T.Relationship_Code_ID" could not be bound. 
Msg 4104, Level 16, State 1, Procedure fn_COT_TEST, Line 27 [Batch Start Line 0] 
The multi-part identifier "Contact_Group_Contacts_T.Relationship_Code_ID" could not be bound. 
Msg 4104, Level 16, State 1, Procedure fn_COT_TEST, Line 27 [Batch Start Line 0] 
The multi-part identifier "Contact_Group_Contacts_T.Relationship_Code_ID" could not be bound. 
Msg 444, Level 16, State 2, Procedure fn_COT_TEST, Line 27 [Batch Start Line 0] 
Select statements included within a function cannot return data to a client. 
Msg 4104, Level 16, State 1, Procedure fn_COT_TEST, Line 29 [Batch Start Line 0] 
The multi-part identifier "Contact_Group_Contacts_T.Main_Group_Contact_BT" could not be bound. 
Msg 444, Level 16, State 2, Procedure fn_COT_TEST, Line 29 [Batch Start Line 0] 
Select statements included within a function cannot return data to a client. 
Msg 455, Level 16, State 2, Procedure fn_COT_TEST, Line 29 [Batch Start Line 0] 
The last statement included within a function must be a return statement. 

我的代碼如下。我盡我所能,但完全卡住了。我究竟做錯了什麼?

CREATE FUNCTION [dbo].[fn_COT_POA] (@CONTACT_ID INT) 
RETURNS VARCHAR 
AS 
BEGIN 

DECLARE @COUNT INT; 
SET @COUNT = 
(select COUNT(CONTACT_ID) 
from Contact_Group_Contacts_T 
where CONTACT_ID = @CONTACT_ID) 
RETURN @COUNT 

IF @COUNT > 1 
    select @CONTACT_ID where Contact_Group_Contacts_T.Relationship_Code_ID in (2801,2802,2803,2804,2805,2806,2807) 
    ELSE 
    select @CONTACT_ID where Contact_Group_Contacts_T.Main_Group_Contact_BT = 1 

END 
GO 

我所要做的僅僅是有這些關係的代碼標識的,如果他們沒有得到這些關係碼的ID,然後選擇具有main_group_contact_bt標誌爲1

聯繫人中選擇聯繫人感謝丹

+0

標籤的DBMS你正在使用。該代碼是特定於產品的。 – jarlh

+0

爲什麼你的功能中間有這條線? 'return @ count'你的函數在返回之後想要停止。 – SqlZim

+0

@SqlZim - 我在網上關注一個示例,可能會給我提示。我不知道我是否應該。無論如何,如果我把它取出不起作用 – dgoodwin

回答

0

這只是瞎猜看着你前面的問題之後,但也許你正在尋找的東西是這樣的:

create function dbo.udf_cot_poa (@group_id int) 
returns table 
as return 
select top 1 /* getting just the first one*/ 
    Contact.* 
from Contact_Group_Contacts_T as Grp 
    inner join contact_contacts_t as Contact 
    on grp.contact_id = contact.contact_id 
    and contact.current_status_id = 65 
    and contact.deceased_date_dt is null 
where grp.group_id = @contact_id 
    and grp.Removed_BT = 0 
order by case 
    when grp.Relationship_Code_ID in (2801,2802,2803,2804,2805,2806,2807) /* related first */ 
    then 0 
    when grp.Main_Group_Contact_BT = 1 /* this one second */ 
    then 1 
    else 2 /* if neither of those exist then whatever does */ 
    end 
go 

select * from dbo.udf_cot_poa(group_id); 
+0

嗨,謝謝你,我試圖做到這一點,但它提出了錯誤,也改變了一些參數 - 在第1行,我改變(在)group_id到(在)contact_id,因爲我想報告在contact_id上,而不是group_id。同樣在第11行,我更改爲grp.contact_id =(at)contact_id ...這會正確創建函數,但我收到此錯誤:消息207,級別16,狀態1,行1 無效的列名稱'contact_id'。 – dgoodwin

+0

@dgoodwin用你的表定義和外鍵更新你的問題 – SqlZim