2017-03-14 4 views
0

Diese Art von folgt auf meine vorherige Frage, aber ich habe es anders gegangen, wie ich herausgefunden habe, was ich versuchte zu tun, kann ich nicht in einer Ansicht, es muss ein sein Funktion.Fehler beim Erstellen der Funktion

Allerdings habe ich den Code bekam an der Unterseite dieses Themas angebracht, aber ich erhalte diese Fehler:

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. 

Mein Code ist unten. Ich habe mein Bestes versucht, bin aber völlig festgefahren. Was mache ich falsch?

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 

Was ich versuche, ist nur ausgewählte Kontakte zu tun, die diese Beziehung Code-IDs haben, wenn sie nicht jene Beziehung Code-IDs dann wählen Sie den Kontakt, der die main_group_contact_bt Flagge als 1. hat bekommen haben

dank Dan

+0

Tag die dbms Sie verwende es. Dieser Code ist produktspezifisch. – jarlh

+0

Warum haben Sie diese Zeile in der Mitte Ihrer Funktion? 'return @ count' Ihre Funktion möchte anhalten, nachdem sie etwas zurückgegeben hat. – SqlZim

+0

@SqlZim - Ich habe ein Beispiel online verfolgt, das mir Hinweise geben könnte. Ich wusste nicht, ob ich sollte oder nicht. Funktioniert nicht, wenn ich es trotzdem herausnehme – dgoodwin

Antwort

0

Dies ist nur ein Schuss im Dunkeln nach zu Ihrer vorherigen Frage suchen, aber vielleicht suchen Sie etwas wie folgt aus:

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

Hallo, danke dafür, ich habe versucht, dies zu tun, aber es kam mit Fehlern, auch ich änderte ein paar der Parameter - in Zeile 1, ich änderte (at) group_id zu (at) contact_id, wie ich berichten möchte in der contact_id, nicht in der group_id. Auch in Zeile 11 änderte ich in grp.contact_id = (at) contact_id ... das schafft die Funktion korrekt, aber ich bekomme diesen Fehler: Msg 207, Level 16, State 1, Line 1 Ungültiger Spaltenname 'contact_id'. – dgoodwin

+0

@dgoodwin aktualisieren Sie Ihre Frage mit Ihren Tabellendefinitionen und Fremdschlüsseln – SqlZim