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 2008 Forums
 Transact-SQL (2008)
 where

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-10-17 : 08:17:20
Hi,
In the stored proc below, i may or may not pass the two parameters.

It seems what I have does not return the correct data.
How should I change the where clause in the end select statement please?

Thanks

create procedure myProc

@Name varchar(5) = null,
@Code char(4) = null

as

...

declare @ClassID tinyint
select @ClassID = ClassID from tblMain where Name = @Name

select
...
where
(
a.ClassID = @ClassID or @ClassID =0
and c.Code = @Code or @Code is null
)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-17 : 08:27:10
it should be

...
where
(
(a.ClassID = @ClassID or @ClassID is null)
and (c.Code = @Code or @Code is null)
)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-10-17 : 08:31:06
Hi,
please note that I should have said, I may pass one or two or no parameters.
The where clause still does not return the correct data.
Any thoughts please?
Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-17 : 08:35:16
as per your current proc, if you do not pass a parameter it will assume null value and since @ClassID is assigned value based on @Name it will be null for @Name = null and so query should work fine with where clause provided.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-10-17 : 08:41:42
Many thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-17 : 09:02:42
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -