﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Help in creating T-SQL where clause / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 26 May 2013 01:24:42 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Help in creating T-SQL where clause</title><link>http://www.sqlservercentral.com/Forums/Topic1421893-392-1.aspx</link><description>I attended a training by Itzik Ben Gan and we found that this technique worked the best for our data set.-- Stored Procedure GetOrders,-- Using Dynamic SQLALTER PROC dbo.GetOrders  @orderid   AS INT      = NULL,  @custid    AS INT      = NULL,  @empid     AS INT      = NULL,  @orderdate AS DATETIME = NULLASDECLARE @sql AS NVARCHAR(1000);SET @sql =     N'SELECT orderid, custid, empid, orderdate, filler /* 27702431-107C-478C-8157-6DFCECC148DD */'  + N' FROM dbo.Orders'  + N' WHERE 1 = 1'  + CASE WHEN @orderid IS NOT NULL THEN      N' AND orderid = @oid' ELSE N'' END  + CASE WHEN @custid IS NOT NULL THEN      N' AND custid = @cid' ELSE N'' END  + CASE WHEN @empid IS NOT NULL THEN      N' AND empid = @eid' ELSE N'' END  + CASE WHEN @orderdate IS NOT NULL THEN      N' AND orderdate = @dt' ELSE N'' END;EXEC sp_executesql  @stmt = @sql,  @params = N'@oid AS INT, @cid AS INT, @eid AS INT, @dt AS DATETIME',  @oid = @orderid,  @cid = @custid,  @eid = @empid,  @dt  = @orderdate;</description><pubDate>Tue, 19 Feb 2013 19:21:11 GMT</pubDate><dc:creator>brad.mason5</dc:creator></item><item><title>RE: Help in creating T-SQL where clause</title><link>http://www.sqlservercentral.com/Forums/Topic1421893-392-1.aspx</link><description>Coalesce is something which doesn't give me the desired results....All i am trying to do is, if the parameter is not passed from UI i need to remove the parameter from where clause of sql Stored procedure ....I guess probably the only way to handle this is to separate out the query and where clause(in Stored procedure) and use sp_executesql finally(which i hate doing it as i have to check everytime where the parameter is not null)Anyother suggestions or thoughts???</description><pubDate>Tue, 19 Feb 2013 18:37:29 GMT</pubDate><dc:creator>PradeepVallabh</dc:creator></item><item><title>RE: Help in creating T-SQL where clause</title><link>http://www.sqlservercentral.com/Forums/Topic1421893-392-1.aspx</link><description>sure here's the link, I dont use the whole thing I just found the use of the Coalesce statement interesting as i normally use where (columnName is null) or ( columnName =@columnName )[url=http://www.codeproject.com/Articles/21234/Implementing-Dynamic-WHERE-Clause-in-Static-SQL]Code project Article Link[/url]</description><pubDate>Tue, 19 Feb 2013 17:35:44 GMT</pubDate><dc:creator>Kevin.roberts25</dc:creator></item><item><title>RE: Help in creating T-SQL where clause</title><link>http://www.sqlservercentral.com/Forums/Topic1421893-392-1.aspx</link><description>[quote][b]Kevin.roberts25 (2/19/2013)[/b][hr]Try this which I got from code project, its a great way to do a query from a form with multiple parameters[code="sql"]Create Procedure sp_EmployeeSelect_Coalesce    @EmployeeName NVarchar(100),    @Department NVarchar(50),    @Designation NVarchar(50),    @StartDate DateTime,    @EndDate DateTime,    @Salary Decimal(10,2)        AS      Set NoCount ON     Select * From tblEmployees     where EmployeeName = Coalesce(@EmployeeName, EmployeeName) AND        Department = Coalesce(@Department, Department ) AND        Designation = Coalesce(@Designation, Designation) AND        JoiningDate &amp;gt;= Coalesce(@StartDate, JoiningDate) AND         JoiningDate &amp;lt;= Coalesce(@EndDate, JoiningDate) AND        Salary &amp;gt;= Coalesce(@Salary, Salary)    If @@ERROR &amp;lt;&amp;gt; 0 GoTo ErrorHandler    Set NoCount OFF    Return(0)  ErrorHandler:    Return(@@ERROR)GO[/code][/quote]Kevin,First, I know this isn't your fault and I'm not slamming you.  You found what you think is a good article or post.Have you got a link for that reference?  I ask because I'd like to go there and set those folks straight on how bad this form of "Catch All" query can be since INDEX SCANS are virtually the only thing guaranteed to happen here.The correct way to do this is with properly written, SQL-Injection-proof code.  Please see the following article on one method for how easily this is accomplished. [url]http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/[/url]Also, the use of GOTO has fallen out of favor with most people.  TRY/CATCH seems to be the way to go now for most things.</description><pubDate>Tue, 19 Feb 2013 17:25:19 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Help in creating T-SQL where clause</title><link>http://www.sqlservercentral.com/Forums/Topic1421893-392-1.aspx</link><description>Try this which I got from code project, its a great way to do a query from a form with multiple parameters[code="sql"]Create Procedure sp_EmployeeSelect_Coalesce    @EmployeeName NVarchar(100),    @Department NVarchar(50),    @Designation NVarchar(50),    @StartDate DateTime,    @EndDate DateTime,    @Salary Decimal(10,2)        AS      Set NoCount ON     Select * From tblEmployees     where EmployeeName = Coalesce(@EmployeeName, EmployeeName) AND        Department = Coalesce(@Department, Department ) AND        Designation = Coalesce(@Designation, Designation) AND        JoiningDate &amp;gt;= Coalesce(@StartDate, JoiningDate) AND         JoiningDate &amp;lt;= Coalesce(@EndDate, JoiningDate) AND        Salary &amp;gt;= Coalesce(@Salary, Salary)    If @@ERROR &amp;lt;&amp;gt; 0 GoTo ErrorHandler    Set NoCount OFF    Return(0)  ErrorHandler:    Return(@@ERROR)GO[/code]</description><pubDate>Tue, 19 Feb 2013 17:03:02 GMT</pubDate><dc:creator>Kevin.roberts25</dc:creator></item><item><title>Help in creating T-SQL where clause</title><link>http://www.sqlservercentral.com/Forums/Topic1421893-392-1.aspx</link><description>My front end aspx web page has a form with some text boxes, dropdown, radiobuttons, Datagrid and searchbutton. I need to populate Datagrid based upon the result set from searchbutton click event....Now the problem is not all the textboxes, dropdown's and radiobutton are required fields...Query for the searchbutton click event depends upon the search parameters provided by the user...I am trying to figure out where condition for the query, but my query is becoming more and more complex as i have to check everytime whether each and every search parameter exists and write sql accordinglyPS: I have 8 different parameters for the form and only 2 parameters are required fieldsIs there any better way of handling this issue?</description><pubDate>Tue, 19 Feb 2013 16:52:04 GMT</pubDate><dc:creator>PradeepVallabh</dc:creator></item></channel></rss>