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


Parameters


Parameters

Author
Message
sharonsql2013
sharonsql2013
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1324 Visits: 1265
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..
AndrewSQLDBA
AndrewSQLDBA
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4784 Visits: 3427
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
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65384 Visits: 17980
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 Modens 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)
AndrewSQLDBA
AndrewSQLDBA
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4784 Visits: 3427
Sean
That is very nice, I completely forgot about that way. That is very clean and efficient.

Andrew SQLDBA
super48
super48
Mr or Mrs. 500
Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)

Group: General Forum Members
Points: 527 Visits: 530
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
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65384 Visits: 17980
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 Modens 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)
sharonsql2013
sharonsql2013
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1324 Visits: 1265
Great. Thank y'all.
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