Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Select all or select single integer

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-06-30 : 07:57:26
Abby writes "Hello,

I'm a bit new to t-sql in Sql Server. I am writing a vb.net application and am stumped on a how to. This is what I would like to do: have a drop down box with "All" and specific items. I could have 2 seperate queries to handle this, but I'm wondering if I could have one statement to handle both. For a specific item, the sql is pretty simple:

Select * where uid = @uid

Where uid is an integer. Since my data key is an integer, I don't know of a wildcard to select all integers or how else I could do this with one sql statement. Any suggestions?

Thanks,

Abby"

dsdeming

479 Posts

Posted - 2005-06-30 : 08:01:00
Select * where uid = @uid or @GetAll = 1

If you set @GetAll = 1 you'll get everything. If it = 0, you'll only get rows where uid = @uid.

Dennis
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-06-30 : 08:06:49
And if you switch the WHERE clause around:

Select * where @GetAll = 1 OR uid = @uid

SQL Server will perform a short-circuit evaulation and process the query more quickly (this means that if the first condition is true, it won't even evaluate the second condition, since the entire clause is already true.)
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-06-30 : 08:45:02
so sql server does process the where conditions in the order they were written??
i thought it doesn't...

Go with the flow & have fun! Else fight the flow
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-06-30 : 19:34:51
There are special cases where the optimizer will recognize a short-circuit situation and will not generate a plan for conditions that do not have to be evaluated.
Go to Top of Page
   

- Advertisement -