473,414 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Selecting from different tables

Using SQL2000. How do I format my select statement to choose one out
of 24 different tables? Each table is slightly different and I was
hoping I could use one select statement and format it on-the-fly
instead of using 24 different ones. I had in mind using a case
statement, something like this:

select * from
case when <input parameter> = 'something1' then tblSomething1
case when <input parameter> = 'something2' then tblSomething2
...and so on...

Thanks for any help.

Jul 23 '05 #1
4 2342
(ma**********@hotmail.com) writes:
Using SQL2000. How do I format my select statement to choose one out
of 24 different tables? Each table is slightly different and I was
hoping I could use one select statement and format it on-the-fly
instead of using 24 different ones. I had in mind using a case
statement, something like this:

select * from
case when <input parameter> = 'something1' then tblSomething1
case when <input parameter> = 'something2' then tblSomething2
...and so on...


You can use dynamic SQL, but it's not sure that is a good idea.

One has to understand that a table definition is a little different from
a record or struct definition in a traditional language. A table comes
with indexes and statistics. Two table can look identical, yet the
optimizer may choose two different ways to compute:

SELECT typecol, COUNT(*)
FROM tbl1
WHERE grouptype = 'ABC'
GROUP BY typecol

SELECT typecol, COUNT(*)
FROM tbl2
WHERE grouptype = 'ABC'
GROUP BY typecol

Say both have a non-clustered index on grouptype and typecol is not
in the index. But in tbl1 only 3 values (of 4000) has grouptype = 'ABC'
whereas in tbl2 there are 3000 (of 4000) with that value. The optimizer
will pick different plans for these.

This is why there is no built-in construct in the language for choosing
one of many similar tables. When it comes to building the query plan,
each table is unique.

As I said, you could use dynamic SQL, and I have longer article on this
topic on my web site. http://www.sommarskog.se/dynamic_sql.html. The
section that is likely apply directly to your situation is
http://www.sommarskog.se/dynamic_sql.html#Dyn_table and the following
section.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Thanks for your reply. I think I'll stick with separate select
statements for all 24 tables.

Erland Sommarskog wrote:
(ma**********@hotmail.com) writes:
Using SQL2000. How do I format my select statement to choose one out
of 24 different tables? Each table is slightly different and I was
hoping I could use one select statement and format it on-the-fly
instead of using 24 different ones. I had in mind using a case
statement, something like this:

select * from
case when <input parameter> = 'something1' then tblSomething1
case when <input parameter> = 'something2' then tblSomething2
...and so on...


You can use dynamic SQL, but it's not sure that is a good idea.

One has to understand that a table definition is a little different from
a record or struct definition in a traditional language. A table comes
with indexes and statistics. Two table can look identical, yet the
optimizer may choose two different ways to compute:

SELECT typecol, COUNT(*)
FROM tbl1
WHERE grouptype = 'ABC'
GROUP BY typecol

SELECT typecol, COUNT(*)
FROM tbl2
WHERE grouptype = 'ABC'
GROUP BY typecol

Say both have a non-clustered index on grouptype and typecol is not
in the index. But in tbl1 only 3 values (of 4000) has grouptype = 'ABC'
whereas in tbl2 there are 3000 (of 4000) with that value. The optimizer
will pick different plans for these.

This is why there is no built-in construct in the language for choosing
one of many similar tables. When it comes to building the query plan,
each table is unique.

As I said, you could use dynamic SQL, and I have longer article on this
topic on my web site. http://www.sommarskog.se/dynamic_sql.html. The
section that is likely apply directly to your situation is
http://www.sommarskog.se/dynamic_sql.html#Dyn_table and the following
section.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


Jul 23 '05 #3
let's say I have an SP like this:
if <input parameter> = 'something1' then
SELECT typecol, COUNT(*)
FROM tbl1
WHERE grouptype = 'ABC'
GROUP BY typecol
else
if <input parameter> = 'something2' then
SELECT typecol, COUNT(*)
FROM tbl2
WHERE grouptype = 'ABC'
GROUP BY typecol

in the body of the stored procedure. How could I look up the execution
plans for these 2 selects?

TIA

Jul 23 '05 #4
Ford Desperado (fo************@yahoo.com) writes:
let's say I have an SP like this:
if <input parameter> = 'something1' then
SELECT typecol, COUNT(*)
FROM tbl1
WHERE grouptype = 'ABC'
GROUP BY typecol
else
if <input parameter> = 'something2' then
SELECT typecol, COUNT(*)
FROM tbl2
WHERE grouptype = 'ABC'
GROUP BY typecol

in the body of the stored procedure. How could I look up the execution
plans for these 2 selects?


The simplest is to run the procedure from Query Analyzer, and prior to
that press CTRL/K. That will give you a tab with the execution plans.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John | last post by:
Hello. I am just wondering if someone can help me with my PHP/MySql code as I am not too proficient with it yet. What I am wanting to do, is display data from two tables. The first table is...
2
by: Robert | last post by:
Hi All, I'm trying to solve this for a few days now and I just can't figure it out... I have three tables set up, I'll simplify them for this question: Table 1: HOTELS Columns: HOTEL_ID,...
6
by: aaj | last post by:
Hi all I use a data adapter to read numerous tables in to a dataset. The dataset holds tables which in turn holds full details of the records i.e. keys, extra colums etc.. In some cases I...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
11
by: Ron L | last post by:
I have a data table that lists a series of items in my database. In my user form, I want the user to be able to filter by a number of criteria (e.g. location, contract, date modified, etc). Other...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
16
by: Y.G. | last post by:
I am making a script which displays an RSS feed on my website. To do this I would like to take all the <div class="blabla"><img src="bla.jpg" alt="bla"> </div> tags and put them in front of my...
1
by: regikutty | last post by:
im a begginer,im using sql server 2005 i have 3 different tables Customer table intcid Companyname ProjectTable
5
by: kwartz | last post by:
Can somebody please help me with a select statement for selecting duplicate records with one different field . I have 3 tables: A, B and C A has 2 fields I ---- primary key m B
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.