我使用以下查询,但引发错误:
CREATE FUNCTION getCustomerPaymentFunc (@customerCode bigint )
RETURNS bigint AS BEGIN
RETURN
( if(select coun from getCustomerPaymentCount (@customerCode))=0)
select 0 as price
else
(select SUM(price) as code
from PaymentLog
where customerCode=@customerCode)
) END
我使用以下两个,但说:
函数中包含的select语句无法将数据返回给客户端
CREATE FUNCTION getCustomerPaymentFunc (@customerCode bigint )
RETURNS bigint AS BEGIN
RETURN
(
SELECT CASE WHEN (select coun from getCustomerPaymentCount(@customerCode))=0
THEN 0 ELSE 1 END as
(select SUM(price) as code from PaymentLog
where customerCode=@customerCode)
) END
看来您的意图是让该函数返回PaymentLog
给定表中价格的总和,customerCode
如果customerCode
不存在,则返回0 。如果正确,则可以执行以下操作:
CREATE FUNCTION getCustomerPaymentFunc (@customerCode bigint) RETURNS bigint AS
BEGIN
DECLARE @result bigint
SET @result= (SELECT ISNULL(SUM(price), 0) FROM PaymentLog WHERE customerCode = @customerCode)
RETURN @result
END
如果customerCode
不在存在PaymentLog
表中的SUM()
函数将返回NULL
,你可以使用的COALESCE
或者ISNULL
功能,以取代NULL
与价值0
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句