Solved

unable to return value when executing the function

Posted on 2011-09-02
8
334 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
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
 
LVL 92

Accepted Solution

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

SELECT [dbo].[UDF_GetInvoiceAmt]('310739', '10035848') AS Result
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 39

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 39

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now