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!

"or else" filter 1

Status
Not open for further replies.

ncirkel

IS-IT--Management
Sep 28, 2003
10
0
0
NL
Hi there,

I have a table that contains, among others, the columns idItem and idOwner. The records in this table could look like this:

idItem idOwner other_columns
1 1 this is data for user 1
1 <null> this is default data in idItem = 1 (for other users)
2 <null> there may be only default data for an idItem

I'm writing a stored procedure that accepts variables @Item and @User. I want it to return all records in the table where:
- if a record exisits with idItem = @Item and idOwner = @User, then return that record
- if that record does not exist, return record where idItem = @Item and idOwner = <null> (return default data)

Sounds fairly simple but I can't get it done!

Thanks for your help,
Nander :)
 
Code:
CREATE PROC apGetItem
  @item int,
  @user int
AS

IF EXISTS (
  SELECT 1 FROM tbl
  WHERE iditem = @item AND idowner = @user
)
  SELECT * FROM tbl
  WHERE iditem = @item AND idowner = @user
ELSE
  SELECT * FROM tbl
  WHERE iditem = @item AND idowner IS NULL
GO

--James
 
That was a fast one, and it works! Thanks a lot,
Nander.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top