stored procedure parameters question

  • I have created the following stored proc:

    Create procedure proc1

    @orderstatus VARCHAR(15),

    @orderdate DATETIME,

    @createdate DATETIME

    AS

    Select * from table1

    where ordercreatedt >= DATEADD(Day, DATEDIFF(Day, 0, GetDate()-1), 0) AND

    ordercreatedt < DATEADD(Day, DATEDIFF(Day, 0, GetDate()), 0)

    and @orderstatus = case when @orderstatus = 'ALL' then 'ALL' Else orderstatus End

    and isnull(@orderdate,'') = case when isnull(@orderdate,'') = '' then '' else orderdate end

    and isnull(@createdate,'') = case when isnull(@createdate,'') = '' then '' else convert(varchar(10),ordercreatedt,101) end

    The following is the requirement:

    1. This proc will be scheduled to run as a SQL job every day to pick up the previous day orders, so the proc runs fine and gets the previous days data if I give the following parameters to the proc:

    proc1 'ALL', null, null

    2. The proc can also be run adhocly, by passing in just the @orderdate or @createdate for the below parameters:

    proc1 'ALL', '03/01/2009', null --Should give the data for orderdate '03/01/2009'

    proc1 'ALL', null, '02/05/2009' -- should give the data for createdate '02/05/2009'

    I am having problems for the above 2nd requirement. Please let me know what changes should be done to the stored procedure so that both the requirements are met.

    Thanks!

  • Try this

    CREATE procedure proc1

    @orderstatus VARCHAR(15),

    @orderdate DATETIME,

    @createdate DATETIME

    AS

    IF @OrderDate is NULL and @Createdate is NULL

    Begin

    Select * from table1

    where ordercreatedt >= DATEADD(Day, DATEDIFF(Day, 0, GetDate()-1), 0) AND

    ordercreatedt < DATEADD(Day, DATEDIFF(Day, 0, GetDate()), 0)

    and @orderstatus = case when @orderstatus = 'ALL' then 'ALL' Else orderstatus End

    END

    ELSE

    BEGIN

    Select * from table1

    where @orderstatus = case when @orderstatus = 'ALL' then 'ALL' Else orderstatus End

    and isnull(@orderdate,'') = case when isnull(@orderdate,'') = '' then '' else orderdate end

    and isnull(@createdate,'') = case when isnull(@createdate,'') = '' then '' else convert(varchar(10),ordercreatedt,101) end

    END

  • Hello

    What I understood:

    * @orderstatus may be ALL or an specific status

    * @orderdate may be NULL or a specific date

    * @createdate may be NULL or a specific date, but the time has to be cutted off.

    So try this:

    [font="Courier New"]SELECT *

       FROM Table1

       WHERE ordercreatedt >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0)

          AND ordercreatedt < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)

          AND orderstatus = CASE WHEN @orderstatus = 'ALL' THEN orderstatus ELSE @orderstatus END

          AND orderdate = ISNULL(@orderdate, orderdate)

          AND ordercreatedt = ISNULL(CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, @createdate))), ordercreatedt)[/font]

    Correct me if I'm wrong.

    Greets

    Flo

  • Try This :

    Create procedure proc1

    @orderstatus VARCHAR(15),

    @orderdate DATETIME,

    @createdate DATETIME

    AS

    Select * from table1

    where ordercreatedt >= CAST(CONVERT(Varchar(10),GetDate()-1,120) AS DateTime)

    AND ordercreatedt < CAST(CONVERT(Varchar(10),GetDate(),120) AS DateTime)

    and orderstatus = case when @orderstatus = 'ALL' then orderstatus Else @orderstatus End

    and orderdate = ISNULL(@orderdate, orderdate)

    and ( @createdate Is Null Or convert(varchar(10),ordercreatedt,101) = convert(varchar(10),@createdate,101) )

    Go

    Regards,
    ѕι๋∂∂нєѕн

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply