简体   繁体   English

如何在SQL Server 2008 r2中的函数中返回记录

[英]How to return a record in a function in SQL Server 2008 r2

I want to write a function that returns in INT which is the number of records that match a certain criteria. 我想写一个在INT中返回的函数,它是符合特定条件的记录数。

I tried this, and it runs... but it keeps quiet - it doesn't return anything. 我尝试了这个,它运行...但它保持安静 - 它不会返回任何东西。 I guess it just runs select in the background. 我猜它只是在后台运行select。

ALTER FUNCTION [dbo].[f_InboundCallsPerUser](@p_User varchar(50), @p_sd datetime, @p_ed datetime)
RETURNS INT
WITH EXECUTE AS CALLER
AS
BEGIN
Return(SELECT  COUNT(*) 
FROM CallDetail
WHERE LocalUserID = @p_user
AND InitiatedDate BETWEEN @p_sd AND @p_ed
AND UPPER(CallDirection) = 'OUTBOUND'
AND LineDurationSeconds > 0);
END;

How do you do that? 你是怎样做的?

Use 采用

SELECT [dbo].[f_InboundCallsPerUser](@p_User, @p_sd , @p_ed)

I guess you are probably EXEC -ing it. 我想你可能正在EXEC它。 in which case you need to do 在这种情况下你需要做

DECLARE @Result INT
EXEC @Result =[dbo].[f_InboundCallsPerUser] @p_User, @p_sd , @p_ed 
SELECT @Result

to see the returned value. 查看返回的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM