Solved

unable to return value when executing the function

Posted on 2011-09-02
8
367 Views
Last Modified: 2012-05-12
Hi experts, for the below function I am unable to return values even after passing valid parameter values, not undestanding why

CREATEFUNCTION [dbo].[UDF_GetInvoiceAmt]
(
      @ProjectNo            VARCHAR(100),
      @FALocation            VARCHAR(100)
      --@ProgramType      VARCHAR(100)
      --@Listed0719            VARCHAR(10)
)
RETURNS MONEY
AS
BEGIN
      -- Declare the return variable here
      DECLARE @Amt MONEY
      SET @Amt = 0
      
                  SELECT @Amt =SUM(ISNULL(I.[Invoice Line Amount],0))
                        FROM DBO.[InvoiceLineItems] (NOLOCK) I      
                        WHERE ((I.[Project Number] LIKE '2%' AND I.[Project Number] = @ProjectNo AND I.FAID = @FALocation)
                              OR (I.[Project Number] LIKE '3%' AND I.[Project Number] = @ProjectNo))
                        GROUP BY [Project Number],FAID

      RETURN @Amt

      
END

GO
0
Comment
Question by:sqlcurious
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 9

Expert Comment

by:jerrypd
ID: 36475809
you are not putting any values in FALocation and ProjectNo?
0
 

Author Comment

by:sqlcurious
ID: 36475823
when I am executing it I am giving the values
exec [dbo].[UDF_GetInvoiceAmt] '310739', '10035848'
Am I doing it wrong?

0
 

Author Comment

by:sqlcurious
ID: 36475831
when I run just the query:

SELECT SUM(ISNULL(I.[Invoice Line Amount],0))
                        FROM DBO.[InvoiceLineItems] (NOLOCK) I      
                        WHERE ((I.[Project Number] LIKE '2%' AND I.[Project Number] = @ProjectNo AND I.FAID = @FALocation)
                              OR (I.[Project Number] LIKE '3%' AND I.[Project Number] = @ProjectNo))
                        GROUP BY [Project Number],FAID


with @ProjectNo ='310739',  @FALocation= '10035848', I get the result but not when executing the function
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 36475861
How about:

SELECT [dbo].[UDF_GetInvoiceAmt]('310739', '10035848') AS Result
0
 
LVL 40

Expert Comment

by:lcohan
ID: 36475876
I suggest to check data type in the InvoiceLineItems table and make sure the parameters passed match the data type and the values in the table. You use VARCHAR(100) in the function and SQL implicit conversion is good up to one point...
I think the easy way to see if the issue it's due to data converions to try do a LIKE instead of the = and maybe only thing that buggers the = is leading/trailing spaces in your table columns easy to fix by LTRIM/RTRIM
0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 250 total points
ID: 36475913
yeah...mathew is right - I didn't even looked at that, sorry you must run

SELECT [dbo].[UDF_GetInvoiceAmt] ('310739', '10035848')

and not

exec [dbo].[UDF_GetInvoiceAmt] '310739', '10035848'

for some odd reason SQL does not throw an error but just "Command(s) completed successfully." for the exec statement.
0
 

Author Closing Comment

by:sqlcurious
ID: 36475963
thanks
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36476204
try this
CREATEFUNCTION [dbo].[UDF_GetInvoiceAmt]
(
      @ProjectNo            VARCHAR(100),
      @FALocation            VARCHAR(100)
      --@ProgramType      VARCHAR(100)
      --@Listed0719            VARCHAR(10)
)
RETURNS MONEY
AS
BEGIN
      -- Declare the return variable here
      DECLARE @Amt MONEY
      
      if @projectno like '2%' or @projectno like '3%'
      begin
       SELECT @Amt =SUM(case when (@ProjectNo LIKE '2%' AND I.FAID = @FALocation )
                               or @ProjectNo LIKE '3%'
                             then I.[Invoice Line Amount]
                             end)
         FROM DBO.[InvoiceLineItems] (NOLOCK) I      
        WHERE I.[Project Number] = @ProjectNo
      end
                 
      RETURN coalesce(@Amt,0)
      
END

GO

Open in new window

0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question