Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old January 19th, 2005, 02:26 PM
Authorized User
Join Date: Oct 2003
Location: , , .
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?


Reply With Quote
  #2 (permalink)  
Old January 19th, 2005, 09:02 PM
Friend of Wrox
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
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
Reply With Quote
  #3 (permalink)  
Old January 20th, 2005, 05:41 AM
Ben Ben is offline
Authorized User
Join Date: Jun 2003
Location: , , United Kingdom.
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
Reply With Quote
  #4 (permalink)  
Old January 21st, 2005, 12:27 AM
Registered User
Join Date: Jan 2005
Location: , , .
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

Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02: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 06:01 AM
Single or Multiple application. Gibs_poovath General .NET 7 May 25th, 2004 07:54 AM

All times are GMT -4. The time now is 07:05 AM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.