Solved

Parameter passing to a function

Posted on 2014-09-04
4
135 Views
Last Modified: 2014-09-04
i HAVE DEFINE THE FOLLOWING FUNCTION WITH THREE PARAMETERS.


ALTER FUNCTION [dbo].[fn_threeyearsalessolo](
      @clientid varchar(10),
        @stdate datetime,
        @edate datetime
      )
RETURNS money
AS
-- Returns Sales for past three years for solo practitioners
BEGIN
    DECLARE @ret money;
    SELECT @ret = SUM(total_charges)
    FROM orders o
    WHERE o.bt_id = @clientid and (o.order_date >= dateadd("YYYY",-3,@stdate) and o.order_date <= @edate
        and o.status not like 'C%' and balance = 0;
     IF (@ret IS NULL)
        SET @ret = 0;
    RETURN @ret;
END;



Yet when I try to call the function using the following code

Update BM_OnlinePass_SubscriberandSales      
set threeyearsales = dbo.fn_threeyearsalessolo(clientid,@STDATE,@EDATE)  
where company_record = 0

  SQL says function call has too many arguments????

Am I defining the correct type of function?   Scalar
0
Comment
Question by:Jeff_Kingston
4 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40303834
There appears to be some incorrect bracketing:

WHERE o.bt_id = @clientid and (o.order_date >= dateadd("YYYY",-3,@stdate) and o.order_date <= @edate

should be

WHERE o.bt_id = @clientid and o.order_date >= dateadd("YYYY",-3,@stdate) and o.order_date <= @edate

Try that? Doubt that's it. I assume a copy and paste error.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40303851
you are missing a parenthesis here before 'and o.order_date '

 WHERE o.bt_id = @clientid and (o.order_date >= dateadd("YYYY",-3,@stdate)      )      and o.order_date <= @edate
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40303873
The date part, the first parameter, in DATEADD is not a literal but a keyword, i.e., no quotes around YEAR.  Since all connectors are "AND", you don't parentheses anyway.  

The current error occurred because the previous ALTER failed, therefore you didn't replace the existing function with one that had 3 parameters.


ALTER FUNCTION [dbo].[fn_threeyearsalessolo](
       @clientid varchar(10),
       @stdate datetime,
       @edate datetime
       )
RETURNS money
AS
-- Returns Sales for past three years for solo practitioners
BEGIN
RETURN ISNULL((
     SELECT SUM(total_charges)
     FROM orders o
     WHERE
        o.bt_id = @clientid and
        o.order_date >= dateadd(YEAR,-3,@stdate) and
        o.order_date <= @edate and
        o.status not like 'C%' and
        balance = 0
), 0)
END --FUNCTION
0
 

Author Closing Comment

by:Jeff_Kingston
ID: 40303966
Kudos for quick and accurate analysis.. date utils in Crysta/SQL always spin me silly

Corrected the issue and made my deadline..

Thanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tools to scan a SQL server's problem 14 28
SQL Group By Question 4 21
Setting variables in a stored procedure 5 24
SQL - Rewrite statement 4 27
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

821 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