2014-03-29 3 views
0

나는 아래의 쿼리를 사용하지만 오류가 발생합니다 :사용하는 경우, 다른 SQL 쿼리의 기능에

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 [email protected]) 
    ) END 

나는 두 아래 사용을하지만 말한다 : 함수가 데이터를 반환 할 수없는 내

선택 사항이 포함 당신의 의도는 함수가 주어진 customerCodePaymentLog 표에 가격의 합을 반환하거나 경우 0을 반환하는 것입니다 같은 클라이언트에

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 [email protected]) 
    ) END 
+1

무엇 당신이 말하는 오류 – Marc

+0

오류, 내 어린 파다완? –

+0

질문을 편집했습니다. – SajjadZare

답변

2

같습니다 customerCode 존재하지 않습니다. 이 맞다면 당신이 할 수 있습니다 :

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 

customerCodePaymentLog 테이블에 존재하지 않는 경우 SUM() 기능은 NULL을 반환하고 사용할 수 있습니다 중 하나 COALESCE 또는 ISNULL 기능과 NULL 값을 대체 0.

참조 MSDN : COALESCEISNULL

관련 문제