Solved

Parameter passing to a function

Posted on 2014-09-04
4
133 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:
ScottPletcher 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Copy Database Wizard 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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

939 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

3 Experts available now in Live!

Get 1:1 Help Now