Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 15th, 2007, 08:36 AM
Registered User
Join Date: Mar 2007
Location: Greer, SC, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to jdziggy Send a message via Yahoo to jdziggy
Default If statement in Where Clause

Hey all,
I am trying to convert the following code into a stored procedure. I wonder if any of you could help me. It appears that it is saying if a string startswith "z_" then replace "z_" with "" and if Count is = 0 then use the first Where clause with the proceeding SQL statement else the second where clause.

For each item in Request.QueryString
                    if Left(item,2)="z_" Then
                        intCurrentVar = Replace(item, "z_", "")
                        if iCount = 0 Then
                            strWhereClause = " WHERE tblPCRDdata.Option_ID=" & intCurrentVar

                            strWhereClause = strWhereClause & " or tblPCRDdata.Option_ID=" & intCurrentVar
                        end if

                        End if

                    iCount = iCount + 1

               strReportSQL = "SELECT tblMain.*, tblAgency.Name AS Agency " _
                                & " FROM tblMain INNER JOIN " _
                                & " tblPCRDdata ON tblMain.ID = tblPCRDdata.Report_ID INNER JOIN " _
                              & " tblAgency ON tblMain.HHSAgency = tblAgency.ID " & strWhereClause

Code?  We don't need no stinkin' code!

Code?  We don't need no stinkin' code!
  #2 (permalink)  
Old March 16th, 2007, 01:38 AM
Friend of Wrox
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post

I would say this post better fits in an ASP or .NET forum, not in an SQL server 2000 forum, to get quick and better response. It may go with no response for a long time here.

I too wonder, if of us could help you at this forum.


- Vijay G
Strive for Perfection
  #3 (permalink)  
Old March 16th, 2007, 08:08 AM
Authorized User
Join Date: Dec 2006
Location: Lake Wylie, SC, USA.
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts

You could put this in a stored procedure with a little rewriting using dynamic SQL. Since the execution plan will have to be generated each time anyway, executing from client or server code will be the same.

Alternatively, since there are only two paths, you could write two stored procedures. This would avoid the execution plan generation on compile.

From the code above, there is something not right. The only difference appears to be the logical operator 'OR'. In path 1, the statement would filter on tblPCRDdata.Option_ID. In path 2, the statement would optionally filter, tblMain.HHSAgency = tblAgency.ID OR tblPCRDdata.Option_ID. That doesn't make sense to me. I may be missing the business logic completely.

Adam Gossage
Lake Wylie, SC, USA

Similar Threads
Thread Thread Starter Forum Replies Last Post
Case Statement In Where Clause Logic dwj119 SQL Server 2000 4 October 27th, 2011 03:14 PM
If statement in WHERE clause frakey Classic ASP Basics 30 July 8th, 2008 03:35 PM
Problems using case statement in where clause vghiya SQL Server 2000 3 May 28th, 2007 05:12 AM
Select statement with where clause with 2 conditon Yasho VB.NET 2002/2003 Basics 7 May 16th, 2007 01:40 PM
USING SQL IF Statement in WHERE clause rit01 SQL Server 2000 17 March 15th, 2007 08:33 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.