0

I use the below query,but throws an error:

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

I Use below Two but says:

select statement included within a function cannot return data to client

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
2
  • Error you say, my young Padawan? Commented Mar 29, 2014 at 11:40
  • 1
    There is so much syntactical errors in you code that disallows to understand your intention. Can you explain in human language (not in SQL) your intention? Commented Mar 29, 2014 at 11:44

1 Answer 1

2

It looks like your intention is to have the function return the sum of price in the PaymentLog table for a given customerCode, or return 0 if the customerCode doesn't exist. If this is correct you could do this:

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

If thecustomerCodedoesn't exist in thePaymentLogtable theSUM()function will returnNULLand you can use either theCOALESCEor theISNULLfunction to replace the NULLvalue with 0.

See MSDN: COALESCE and ISNULL

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.