Solved

Parameter passing to a function

Posted on 2014-09-04
4
134 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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

773 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