If Based SQL Statement

Posted on 2006-10-23
Medium Priority
Last Modified: 2013-12-24
Alright guys my code is below. The problem is that I would like this component to work reguardless of how many arguments are passed even if there are none.  I cannot say how many arguments are going to be passed and even if any will be. So the problem is where i would place my WHERE SQL Statement. I think you will get what i am saying if you look at my code. The problem with how it currently runs it that the code read "WHERE AND". I mean I guess i could put something in there that will be a definite for all rows in the table but I think there must be a better solution than that.

<cfquery datasource="#Request.DSN#" name="GetTickets">
SELECT #Arguments.TicketType#.*, Customers.*
FROM #Arguments.TicketType#
INNER JOIN Customers ON #Arguments.TicketType#.CustomerPK = Customers.CustomerPK
<cfif #Arguments.TicketStatus# neq "Undefined">AND #Arguments.TicketType#.TicketStatus = '#Arguments.TicketStatus#'</cfif>
<cfif #Arguments.ServiceStatus# neq "Undefined">AND #Arguments.TicketType#.ServiceStatus = '#Arguments.ServiceStatus#'</cfif>
<cfif #Arguments.TicketAssignedTo# neq "Undefined">AND #Arguments.TicketType#.TicketAssignedTo = '#Arguments.TicketAssignedTo#'</cfif>
<cfif #Arguments.CustomerName# neq "Undefined">AND #Arguments.TicketType#.TicketStatus = '#Arguments.CustomerName#'</cfif>
<cfif #Arguments.TicketID# neq "Undefined">AND #Arguments.TicketType#.TicketID = '#Arguments.TicketID#'</cfif>
<cfif #Arguments.PhoneNumber# neq "Undefined">AND Customers.PrimaryPhone = '#Arguments.PhoneNumber#' OR Customers.SecondaryPhone = '#Arguments.PhoneNumber#'</cfif>
Question by:rjhunter
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Accepted Solution

tim_cs earned 1500 total points
ID: 17794157
The easiest way, and the most commonly used way, is to use "Where 1=1" for AND statements or "Where 0=1" if you had OR statements.

Expert Comment

ID: 17798900
Agree with Tim we use the 1=1 all the time.

Author Comment

ID: 17798965
lol awesome. I was hoping for a way that u didnt have to compare 2 values but this works all the same and will be more effiecient then comparing a value in the table. Thanks dude.

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question