PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > Select statement
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Select statement

Réponse
 
LinkBack Outils de la discussion
Vieux 20/09/2007, 17h47   #1
krusz10@hotmail.co.uk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Select statement

I have a table (tableA) that lists a number of other tables on the
same database. I want to search each table listed in tableA for an
existence of an known entry and then display that entry and the table
its in
I can do it in other scripting languages but I'm new to SQL. Can you


I sort of want to do the following
Select * from everything_in(select tablename from tableA) where entry
= 'fred'

  Réponse avec citation
Vieux 20/09/2007, 23h31   #2
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Select statement

(krusz10@hotmail.co.uk) writes:
> I have a table (tableA) that lists a number of other tables on the
> same database. I want to search each table listed in tableA for an
> existence of an known entry and then display that entry and the table
> its in
> I can do it in other scripting languages but I'm new to SQL. Can you
>
>
> I sort of want to do the following
> Select * from everything_in(select tablename from tableA) where entry
>= 'fred'


Sounds like you have a bad database design. Normally, each table should
describe a unique entity, why a query like your would not be meaningful
in most cases. (The one exception I can think of is auditing columns that
could appear in all tables.)

The query to write is:

WITH all_tables AS (
SELECT ...
FROM tblA
UNION ALL
SELECT ...
FROM tblB
...
)
SELECT ... FROM all_tables WHERE col = 'fred'

If you want to work from yor table of table names, you would need to
generate the query dynamically.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Vieux 21/09/2007, 12h57   #3
krusz10@hotmail.co.uk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Select statement

On 20 Sep, 22:31, Erland Sommarskog <esq...@sommarskog.se> wrote:
> (krus...@hotmail.co.uk) writes:
> > I have a table (tableA) that lists a number of other tables on the
> > same database. I want to search each table listed in tableA for an
> > existence of an known entry and then display that entry and the table
> > its in
> > I can do it in other scripting languages but I'm new to SQL. Can you
> >

>
> > I sort of want to do the following
> > Select * from everything_in(select tablename from tableA) where entry
> >= 'fred'

>
> Sounds like you have a bad database design. Normally, each table should
> describe a unique entity, why a query like your would not be meaningful
> in most cases. (The one exception I can think of is auditing columns that
> could appear in all tables.)
>
> The query to write is:
>
> WITH all_tables AS (
> SELECT ...
> FROM tblA
> UNION ALL
> SELECT ...
> FROM tblB
> ...
> )
> SELECT ... FROM all_tables WHERE col = 'fred'
>
> If you want to work from yor table of table names, you would need to
> generate the query dynamically.
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx


----------------------------------------------------------------------------------------------------------------------------------------------

Thanks for the prompt reply. Not sure it would unless I could
probably use variables in some way.


The database is a back end DB to a Microsoft system (SMS). There are a
number of collection tables that perform different tasks against there
contents. Listed in the DB are unique objects that are linked by a
primary key across the tables. These unique objects can exist in any
number of the tables depending on what task is required.

What I was hoping to get is a list of tables (tasks) that a unique
object is listed in, and the list of tables I want to search in (which
change daily) are found in tblA.

Hope this makes things clearer.
Can it be done in a single query?

In simple terms a script would look something like this:

Foreach $tblname in ('select tablename from tblA')

do

If true (Select objectname from $tblname where objectname = 'FRED')
Print $tblname






  Réponse avec citation
Vieux 21/09/2007, 23h23   #4
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Select statement

(krusz10@hotmail.co.uk) writes:
> The database is a back end DB to a Microsoft system (SMS). There are a
> number of collection tables that perform different tasks against there
> contents. Listed in the DB are unique objects that are linked by a
> primary key across the tables. These unique objects can exist in any
> number of the tables depending on what task is required.
>
> What I was hoping to get is a list of tables (tasks) that a unique
> object is listed in, and the list of tables I want to search in (which
> change daily) are found in tblA.
>
> Hope this makes things clearer.
> Can it be done in a single query?


No. A query always exactly define set of tables and columns. If you don't
know which tables that you want to access, you will need to build the
query dynamically along the pattern in my first post. You could build
that query in T-SQL or in a client language, whatever your preference.

But once you know which table to query, you can query all at once if you
like. (Running a loop and querying one table at a time is not likely to
have much extra cost.)

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h41.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10730 seconds with 12 queries