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.
| Author |
Topic |
|
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2006-12-20 : 09:23:34
|
| Hi,Assuming you have a stored procedure as follows:The userID may or may not be providedhow should the where clause be written to take into account the two scenarios?I do not want to have if or case statement.Thankscreate procedure usp_getUsers@UserID int = nullASselect UserID, UserLogin, UserName IsActivefrom Userswhere isnull(@UserID, UserID) |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-12-20 : 09:26:19
|
| [code]select UserID, UserLogin, UserName IsActivefrom Userswhere UserID = isnull(@UserID, UserID)[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-20 : 09:30:28
|
| create procedure usp_getUsers@UserID int = nullASselect UserID, UserLogin, UserName IsActivefrom Userswhere isnull(@UserID, UserID) = useridPeter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-20 : 09:30:46
|
Peter LarssonHelsingborg, Sweden |
 |
|
|
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2006-12-20 : 10:15:03
|
| Hi,This is what I have now:and to test it this is what I do:UsersGet --> returns one record only whereas it shoulod return all recordsUsersGet 1 --> returns one record onlyp.s. there are many records in Users TableALTER PROCEDURE [dbo].[UsersGet]@UserID bit = nullASselect UserID, UserLogin, UserName IsActivefrom Userswhere UserID = isnull(@UserID, UserID) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-20 : 10:23:21
|
| Change @UserID BIT = NULL to@UserID INT = NULLPeter LarssonHelsingborg, Sweden |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-12-20 : 10:49:02
|
quote: Originally posted by fmardani Hi,This is what I have now:and to test it this is what I do:UsersGet --> returns one record only whereas it shoulod return all recordsUsersGet 1 --> returns one record onlyp.s. there are many records in Users TableALTER PROCEDURE [dbo].[UsersGet]@UserID bit = nullASselect UserID, UserLogin, UserName IsActivefrom Userswhere UserID = isnull(@UserID, UserID)
1 In your question, INT is used, why did you change it to BIT?2 Note that all non zero values will become 1 if you assign it to BIT variableMadhivananFailing to plan is Planning to fail |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-12-20 : 12:17:19
|
| [code]create procedure usp_getUsers@UserID int = nullASselect UserID, UserLogin, UserName IsActivefrom Userswhere @UserID is null or UserID = @UserID[/code]CODO ERGO SUM |
 |
|
|
|
|
|
|
|