创建函数时出错

时间:2017-03-14 15:35:05

标签: sql-server function if-statement sql-server-2016

这是从我之前的问题开始的,但我已经采用了不同的方式,因为我发现我试图做的事情,我不能在视图中做,它需要是一个功能。

但是我已将代码附加到此线程的底部,但我收到了这些错误:

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的联系人,如果他们还没有获得这些关系代码ID,则选择具有main_group_contact_bt标志的联系人1。

谢谢Dan

1 个答案:

答案 0 :(得分: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);