Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Parameters Expand / Collapse
Author
Message
Posted Tuesday, April 8, 2014 10:18 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 8:52 AM
Points: 253, Visits: 761
I am actually working on a report where I need to pass parameters.
I need to pass the parameters HoursStated which could be 5, 10,15 . So when I pass 5 it will give me the details of everything where the Hours worked (which is a integer column) is > HoursStated (Can be 5 , 10 , or 15).
But at the same time I can pass "*" and I should get all the hours which are greater than All ( 5,10, 15)

The Query is

Select * from Production
where
HourWorked > @HoursStated

The above works perfectly fine for individuals .. Don't know how should I add for all three..

Post #1559567
Posted Tuesday, April 8, 2014 11:14 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, November 7, 2014 2:00 PM
Points: 979, Visits: 3,364
Sharon
I would suggest never using an asterisk like that. You will only have difficulties. Also, always name your columns, never use an asterisk in your query. What happens to the front-end if a column is added to the tables, or removed?

So back to your question:
In your stored procedure, simply use an IF statement
IF @HourStated = 0
BEGIN
SELECT
<ColumnName1>
, <ColumnName2>
, <ColumnNameN>
FROM
Production
WHERE
HourWorked > 15
END

ELSE

BEGIN
SELECT
<ColumnName1>
, <ColumnName2>
, <ColumnNameN>
FROM
Production
WHERE
HourWorked >= @HoursStated
END

Andrew SQLDBA
Post #1559595
Posted Tuesday, April 8, 2014 12:34 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:50 PM
Points: 13,325, Visits: 12,811
You could make this even simpler by providing a default to HoursStarted.

create procedure GetSomeData
(
@HoursStarted int = 15
) as
Select [Columns]
from Production
where HourWorked > @HoursStated

So now if the user wants "All" just pass in NULL for @HoursStarted.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1559649
Posted Tuesday, April 8, 2014 1:04 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, November 7, 2014 2:00 PM
Points: 979, Visits: 3,364
Sean
That is very nice, I completely forgot about that way. That is very clean and efficient.

Andrew SQLDBA
Post #1559668
Posted Wednesday, April 9, 2014 4:58 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, November 20, 2014 10:51 PM
Points: 71, Visits: 423

then try this one if u want to use *--

create procedure startrred
@hourstarted varchar(10)
as
begin
if(@hourstarted!='*')
begin

Select *
from Production where hourworked>@hourstarted

END
else
begin
select * from Production where hourworked>15
end
end

Post #1559871
Posted Wednesday, April 9, 2014 7:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:50 PM
Points: 13,325, Visits: 12,811
super48 (4/9/2014)

then try this one if u want to use *--

create procedure startrred
@hourstarted varchar(10)
as
begin
if(@hourstarted!='*')
begin

Select *
from Production where hourworked>@hourstarted

END
else
begin
select * from Production where hourworked>15
end
end



I would NEVER recommend passing in a character that will be implicitly converted to an int like this. One of the issues with this type of thing is what happens when you pass a character string that cannot be converted to an int?

However, if you go with this path keep it simple. There is no need to have separate select statements. This procedure can be greatly simplified to:

create procedure startrred
(
@hourstarted varchar(10)
)as
if @hourstarted = '*' set @hourstarted = '15'

Select *
from Production where hourworked>@hourstarted



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1559928
Posted Wednesday, April 9, 2014 10:04 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 8:52 AM
Points: 253, Visits: 761
Great. Thank y'all.
Post #1560047
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse