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

x
?
Solved

Parameter passing to a function

Posted on 2014-09-04
4
Medium Priority
?
152 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
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.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

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