Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

unable to return value when executing the function

Posted on 2011-09-02
8
Medium Priority
?
373 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 1000 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 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

722 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