I'm in the process of converting some of my ColdFusion Component SQL Queries to Stored Procedures. I have a SELECT statement with multiple IF Statements in the WHERE clause.
The ColdFusion SELECT Statement is as follows.
SELECT *
FROM Employees a, Departments b
WHERE a.EmployeeID=a.EmployeeID AND a.DepartmentID=b.ID
<CFIF #FirstName# IS NOT "">
AND FirstName LIKE '#FirstName#%'
</CFIF>
<CFIF #LastName# IS NOT "">
AND LastName LIKE '#LastName#%'
</CFIF>
<CFIF #Extension# IS NOT "">
AND Extension LIKE '#Extension#%'
</CFIF>
<CFIF #DepartmentID# IS NOT "">
AND DepartmentID = #DepartmentID#
</CFIF>
ORDER BY LastName, FirstName
The above text tells the query to run the additional AND's within the WHERE clause if the variable being passed is not null.
I've been struggling with a way to accomplish this within a Stored Procedure. Any help is greatly appreciated.
The ColdFusion SELECT Statement is as follows.
SELECT *
FROM Employees a, Departments b
WHERE a.EmployeeID=a.EmployeeID AND a.DepartmentID=b.ID
<CFIF #FirstName# IS NOT "">
AND FirstName LIKE '#FirstName#%'
</CFIF>
<CFIF #LastName# IS NOT "">
AND LastName LIKE '#LastName#%'
</CFIF>
<CFIF #Extension# IS NOT "">
AND Extension LIKE '#Extension#%'
</CFIF>
<CFIF #DepartmentID# IS NOT "">
AND DepartmentID = #DepartmentID#
</CFIF>
ORDER BY LastName, FirstName
The above text tells the query to run the additional AND's within the WHERE clause if the variable being passed is not null.
I've been struggling with a way to accomplish this within a Stored Procedure. Any help is greatly appreciated.