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
 General SQL Server Forums
 New to SQL Server Programming
 Select statments using IF

Author  Topic 

richardlaw
Yak Posting Veteran

68 Posts

Posted - 2012-08-02 : 15:31:18
Hi,

I've tried to create a SELECT that changes the WHERE depending on the mode. The IF statements seem OK, but I'm having issues adding the variable WHERE. Any ideas?

USE [CB_Gymnastics]
GO
/****** Object: StoredProcedure [dbo].[usp_Members_SelectByFirstNameAndLastName] Script Date: 08/02/2012 20:21:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_Members_SelectByFirstNameAndLastName]
@i_params varchar(1000)

as

declare @sClubID varchar(50)
declare @Member_FirstName varchar(20)
declare @Member_LastName varchar(20)
declare @Member_IsGymnast bit
declare @Member_IsOfficial bit
declare @Mode varchar(50)
declare @AndWhere varchar(1000) = ''


select @sClubID = value from dbo.fnSplit(@i_params, '|') where idx = 2
select @Member_FirstName = value from dbo.fnSplit(@i_params, '|') where idx = 3
select @Member_LastName = value from dbo.fnSplit(@i_params, '|') where idx = 4
select @Member_IsGymnast = cast(value as bit) from dbo.fnSplit(@i_params, '|') where idx = 5
select @Member_IsOfficial = cast(value as bit) from dbo.fnSplit(@i_params, '|') where idx = 6

IF @Mode = 'Gymnasts'
SET @AndWhere = @AndWhere + 'AND @Member_IsGymnast = 1'
IF @Mode = 'Officials'
SET @AndWhere = @AndWhere + 'AND @Member_IsOfficial = 1'

select
MemberID, Member_FirstName, Member_LastName, Member_Gender, Member_DOB, Member_BgMembershipNumber, Member_BgMembershipWith
from
dbo.tbl_Members
where
Member_ClubID = @sClubID AND Member_IsGymnast = @Member_IsGymnast AND Member_IsOfficial = @Member_IsOfficial
AND Member_FirstName like '%' + @Member_FirstName + '%'
AND Member_LastName like '%' + @Member_LastName + '%'
@AndWhere


Thanks as alwasy

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-02 : 15:39:38
T-SQL syntax does not let you specify part of the WHERE clause as a variable as you are attempting to do. What you will need to do is something like this:
where
Member_ClubID = @sClubID AND Member_IsGymnast = @Member_IsGymnast AND Member_IsOfficial = @Member_IsOfficial
AND Member_FirstName like '%' + @Member_FirstName + '%'
AND Member_LastName like '%' + @Member_LastName + '%'
AND
(
(@Mode = 'Gymnasts' AND @Member_IsGymnast = 1)
OR
(@Mode = 'Officials' AND @Member_IsOfficial = 1)
)
But this may have some performance implications - see Gail Shaw's article here for an option that would perform better: http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-02 : 16:02:20
Isnt this problem solved yet?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=177402

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

Go to Top of Page

richardlaw
Yak Posting Veteran

68 Posts

Posted - 2012-08-02 : 18:54:08
Hi visakh16

Sort of. I followed the link you provided and saw the IF statement method and tried to follow that.

Thank you both - I'll study both sets of feedback and try to get it resolved.

Thanks again

quote:
Originally posted by visakh16

Isnt this problem solved yet?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=177402

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



Go to Top of Page

richardlaw
Yak Posting Veteran

68 Posts

Posted - 2012-08-06 : 17:22:40
Hi

Can someone help here - how will this work? I don't really understand the OR section??

Thanks

quote:
Originally posted by sunitabeck

T-SQL syntax does not let you specify part of the WHERE clause as a variable as you are attempting to do. What you will need to do is something like this:
where
Member_ClubID = @sClubID AND Member_IsGymnast = @Member_IsGymnast AND Member_IsOfficial = @Member_IsOfficial
AND Member_FirstName like '%' + @Member_FirstName + '%'
AND Member_LastName like '%' + @Member_LastName + '%'
AND
(
(@Mode = 'Gymnasts' AND @Member_IsGymnast = 1)
OR
(@Mode = 'Officials' AND @Member_IsOfficial = 1)
)
But this may have some performance implications - see Gail Shaw's article here for an option that would perform better: http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-06 : 18:36:24
thats boolean logic. either of two condition separated by OR has to be true. so either it has to be @Mode='Gymnasts' AND @Member_IsGymnast = 1

or @Mode = 'Officials' AND @Member_IsOfficial = 1

so whe you pass Gymnasts as value it will only return cases where @Member_IsGymnast = 1 and similarly for Officials

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

Go to Top of Page
   

- Advertisement -