Wrox Programmer Forums
|
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
 
Old March 8th, 2005, 12:34 AM
Authorized User
 
Join Date: Mar 2005
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Query

I need to write a stored proceed that has 15 parameters that returns a recordset. Any one of these parameters may contain values.

EX: @Lname = ''
@Phone = '1234567890'
@Fname = 'JANE'
@City = 'LA'
@State = ''

The main part of the proc is a dynamically created SELECT statement where the parameters are used in the WHERE clause. EX: @SQL = 'SELECT * FROM Table WHERE '. Only parameters with values must be included in the WHERE clause. And any parameter after the first one should have 'AND'. So the query should look like this:

@SQL = 'SELECT * FROM Table WHERE '
@SQL = @SQL + ' phone = ' + @phone
@SQL = @SQL + ' AND Fname = ' + @Fname

How can I figure out which is the first parameter that contains a value so not to include an AND condition and then add the AND for the rest of the parameters?

Thanks,
Ninel
 
Old March 8th, 2005, 01:18 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default


IF @Var <> "" then
   @SQL = @SQL + ' AND <column>= ' + @Var
END IF





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Table Name in SQL Query drani C# 2 November 6th, 2007 05:47 PM
Dynamic Query Sum Colonel Angus SQL Server 2000 4 March 30th, 2005 11:08 AM
Dynamic Query zaman1111 Javascript 4 August 4th, 2004 02:16 PM
Dynamic query problem drex10 SQL Server 2000 2 April 6th, 2004 11:55 PM
ADO Dynamic Query - Please help rudidoku Access 1 July 23rd, 2003 06:50 PM





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