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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL help

Status
Not open for further replies.

eheidrich

Programmer
Feb 2, 2005
7
BR
I am having trouble to remember the solution of a simples problem. There is a condition you put on a select to bring only one specific tuple from a table or the whole table. Ex:

select * from user
where (ID_user = :iduser or BLABLABLA)

The BLABLABLA part is the one I don't remember! The thing is that if I pass a invalid id, like a negative number, it will return all the tuples of the table. In the other hand, if a pass a actual id it will return me only the specific tuple.

Does any one have seen it before! I have used it a year ago, but I can't remember!

Thanks!
 
I think the sql statement you need is a little bit more complicated than you remember! The following should do the trick:

select * from user
where id = :iduser
union
select * from user
where not exists
(select * from user
where id = :iduser)

Steve
 
Hey there
My version of what you need:
Code:
SELECT * FROM USER
WHERE (ID_USER = :IDUSER)OR(:ID_USER = -1)
if you pass -1 as a query param then you'll get the whole table in your slect otherwise it will be zero or one record.

HTH
--- McMerfy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top