2017-03-14 9 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의 다음 1.

로 main_group_contact_bt 플래그가 연락처를 선택, 그 관계 코드 ID의의가있는 경우에만 연락처 선택이다 감사합니다 댄

+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에서 (at) group_id (at) contact_id에보고하고 싶습니다 변경 group_id가 아니라 contact_id에 있습니다. 또한 줄 11, grp.contact_id = (at) contact_id 변경 ...이 올바르게 함수를 만듭니다 있지만이 오류가 발생합니다. msg 207, 수준 16, 상태 1, 줄 1 잘못된 열 이름 'contact_id'. – dgoodwin

+0

@dgoodwin 테이블 정의와 외래 키로 질문을 업데이트하십시오. – SqlZim

관련 문제