SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select statments using IF
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

richardlaw
Yak Posting Veteran

United Kingdom
66 Posts

Posted - 08/02/2012 :  15:31:18  Show Profile  Visit richardlaw's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/02/2012 :  15:39:38  Show Profile  Reply with Quote
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

India
48027 Posts

Posted - 08/02/2012 :  16:02:20  Show Profile  Reply with Quote
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

United Kingdom
66 Posts

Posted - 08/02/2012 :  18:54:08  Show Profile  Visit richardlaw's Homepage  Reply with Quote
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

United Kingdom
66 Posts

Posted - 08/06/2012 :  17:22:40  Show Profile  Visit richardlaw's Homepage  Reply with Quote
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

India
48027 Posts

Posted - 08/06/2012 :  18:36:24  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000