Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I improve this query

Status
Not open for further replies.

habesha

Programmer
Oct 30, 2006
68
0
0
US
I have a table say

tbl
col1 col2
1 A
2 B
3 C
Null F
5 G
Null H

Here is what I want a parameter is passed to my stored proc
@col1 int = Null

I want to select a value from tbl1 according to @col1
if @col1 is null I will select all the records of tbl
otherwise I will select records wil @col1 = @col1

I wrote it like this

select * from tbl
where (col1 = isnull(@col1, col1) or ((@col1 is null) and (col1 is null)))
it is giving me the right answer but performance wise I am not satisfied

Is there other way to do that considering performance/

Thanks
 
Code:
select *
       from tbl
       WHERE (@col1 IS NULL OR col1 = @col1)

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top