ARRRGH!!!! Stop using Dynamic SQL ;-)!!!!
-----Original Message----- From: Christian Watt [mailto:[EMAIL PROTECTED] Sent: 24 February 2005 20:09 To: SQL Subject: RE: How do you do an IF condition in where statement --------CfPage-------- <cfstoredproc procedure="ProcName" datasource="dbSource"> <cfprocparam dbvarname="firstname" cfsqltype="CF_SQL_VARCHAR" value="#form.firstname#" type="In"> <cfprocparam dbvarname="lastname " cfsqltype="CF_SQL_VARCHAR" value="#form.lastname #" type="In"> <cfprocparam dbvarname="phone " cfsqltype="CF_SQL_VARCHAR" value="#form.phone #" type="In"> <cfprocresult name="ResultName"> </cfstoredproc> ---------------TSql------------ CREATE PROCEDURE ProcName @firstname varchar(20), @lastname varchar(25), @phone varchar(15) Declare @sqlStatement nvarchar(150) Select @sqlStatement = "select * from profiles where 0=0" If (len(@firstname) = 0) begin Select @sqlStatement = @sqlStatement + " and firstname= @firstname" end If (len(@lastname) = 0) begin Select @sqlStatement = @sqlStatement + " and lastname = @lastname" End If (len(@phone) = 0) begin Select @sqlStatement = @sqlStatement + " and phone = @phone" End exec sp_executesql @sqlStatement , N'@firstname varchar(20),@lastname varchar(25),@phone varchar(15)',@firstname,@lastname,@phone -----Original Message----- From: Venable, John [mailto:[EMAIL PROTECTED] Sent: Thursday, February 24, 2005 1:25 PM To: SQL Subject: RE: How do you do an IF condition in where statement You need single quotes around your values. i.e. lastname='#form.lastname#' John Venable > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Sent: Thursday, February 24, 2005 2:26 PM > To: SQL > Subject: How do you do an IF condition in where statement > > How would you write this regular Cold Fusion query in TSQL for a > stored > procedure: > > select * > from profiles > where 0=0 > <cfif form.firstname neq ''>and firstname=#form.firstname#</cfif> > <cfif form.lastname neq ''>and lastname=#form.lastname#</cfif> <cfif > form.phone neq ''>and phone=#form.phone#</cfif> > > I tried a few times, I can't quite get the hang of IF statements in > TSQL for some reason, even tho. in CFMX they're juz a piece of cake. > > Thanks, > Pardeep. > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Purchase Studio MX with Flash Pro from House of Fusion, a Macromedia Authorized Affiliate and support the CF community. http://www.houseoffusion.com/banners/view.cfm?bannerid=51 Message: http://www.houseoffusion.com/lists.cfm/link=i:6:2179 Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/6 Subscription: http://www.houseoffusion.com/lists.cfm/link=s:6 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.6 Donations & Support: http://www.houseoffusion.com/tiny.cfm/54
