Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

unable to return value when executing the function

Posted on 2011-09-02
8
Medium Priority
?
382 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

916 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