Solved

Parameter passing to a function

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

706 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

19 Experts available now in Live!

Get 1:1 Help Now