Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
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 January 19th, 2005, 03:26 PM
Authorized User
Join Date: Oct 2003
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default multiple selects -single where clause


I have a sp with about 6 if statements and each one has the same where condition such as the following:
if @param=1
select country
from countries
where lang=@lang
if @param=2
select name
from customers
where lang=@lang

Is it possible to write a query without having to repeat the where condition each time but only once?


Old January 19th, 2005, 10:02 PM
Friend of Wrox
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc

You may consider using case as the examples from books online

    WHEN state = 'KS' THEN 'In Kansas'
    WHEN city = 'New York' THEN 'East I think'
    ELSE 'Click your heels'
FROM pubs.dbo.authors
Jaime E. Maccou
Old January 20th, 2005, 06:41 AM
Ben Ben is offline
Authorized User
Join Date: Jun 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts

If I understand your question correctly, i don't believe it can be done in a SP.
However [As you probably realise] you could dynamically build the query in the application.

Example in VB:

Dim sSQL as String, sWhere as string

sWhere = "where lang=@lang"

sSQL = "if @param=1 begin select country from countries " & sWhere & " end if @param=2 begin select name from customers " & sWhere 'etc etc
Old January 21st, 2005, 01:27 AM
Registered User
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

you can use this query in stored procedure

declare @str varchar(1000)
declare @where varchar(100)
select @where='where lang='+@lang
select @str='if @param=1 begin select country from countries
             end if @param=2 begin select name from customers
             end ' + @where

Similar Threads
Thread Thread Starter Forum Replies Last Post
Put together 2 selects results in a single one romulo.petri SQL Language 3 March 6th, 2008 03:43 AM
multiple values in "IN" clause - Please help! billkrieger ASP.NET 1.0 and 1.1 Professional 5 October 22nd, 2007 05:23 PM
trying to get data from multiple selects ceevk1 VB Databases Basics 1 April 3rd, 2007 12:05 PM
Multiple Selects in SQL QueryStrings doonman BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 6 January 30th, 2006 07:01 AM
Single or Multiple application. Gibs_poovath General .NET 7 May 25th, 2004 07:54 AM

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