SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


stored procedure parameters question


stored procedure parameters question

Author
Message
Mh-397891
Mh-397891
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2550 Visits: 1514
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!
Sanjay-300840
Sanjay-300840
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 124
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
Florian Reischl
Florian Reischl
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13873 Visits: 3934
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:

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)




Correct me if I'm wrong.

Greets
Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Siddhesh
Siddhesh
Old Hand
Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)

Group: General Forum Members
Points: 315 Visits: 165
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,
ѕι๋∂∂нєѕн
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search