1

This query

SELECT name
FROM sysobjects
WHERE id IN (SELECT id FROM syscolumns WHERE name LIKE 'TICKER')

produces the list of tables that contain a column called TICKER.

Now I want to see all the data from each table.

Select * 
from (the result from the above query)

Example

Select * from POS   
Select * from SEC   
Select * from BROKER

Any ideas?

1
  • You probably need Dynamic-SQL for this Commented Sep 18, 2015 at 18:47

2 Answers 2

1

Set based\no cursors.

Final edit - a combo of both solutions

Declare @Sql NVarchar(Max) = N'';

Select @Sql += N'Select * From ' + QuoteName(Schema_Name(Schema_id)), '[') + N'.' + QuoteName(Tables.Name, '[') + N';'
From sys.tables
Join sys.Columns
On tables.object_id = Columns.object_id
Where columns.Name = 'Ticker';

Exec (@Sql);
Sign up to request clarification or add additional context in comments.

5 Comments

This is still a dangerous thing to do, makes no differentiation nor care on weather it has permissions or anything else. It is however exactly what you asked for!
The temp table also isn't really required at all (see answer posted above)
No, but a combination of the two will suffice. Mine, whilst uses extraneous temp tables does also take into consideration schemas! It's not an arms race, it's an attempt to make sure the OP has everything they need to answer their question. Between everything they do,
Note that with temp tables, you need to declare any string columns with COLLATE DATABASE_DEFAULT, or you'll get errors if the local database collation doesn't match the SQL Server installation collation (i.e. tempdb uses a different default collation than the current database).
Combined both solutions into one butterfly!
0

You should use an INNER JOIN on the two tables, and you should also use the sys.objects and sys.columns system views instead of the deprecated sysobjects and syscolumns. Also, if you don't have a wild-card, you should use equals instead of 'like'.

The result is something like this:

SELECT * 
  FROM sys.objects o 
       INNER JOIN sys.columns c ON o.object_id = c.object_id
 WHERE c.name = 'TICKER'

Okay, you changed the question on me while I was answering it... so with your new requirements (and this is horribly dangerous... think about what would happen if it returned a thousand tables with a given column name), you can use dynamic SQL like this:

DECLARE @sql varchar(MAX);
DECLARE @tableName sysname;
DECLARE theCursor CURSOR FAST_FORWARD FOR
    SELECT o.name AS TableName 
      FROM sys.objects o 
           INNER JOIN sys.columns c ON o.object_id = c.object_id
     WHERE c.name = 'TICKER';

OPEN theCursor;
FETCH NEXT FROM theCursor INTO @tableName;

WHILE @@FETCH_STATUS = 0 
BEGIN 
    SET @sql = 'SELECT * FROM ' + QUOTENAME(@tableName);
    EXEC (@sql);
    FETCH NEXT FROM theCursor INTO @tableName;
END

CLOSE theCursor;
DEALLOCATE theCursor;

Or, if you want to avoid the cursor, do it like this, no temp tables required:

DECLARE @sql varchar(MAX);
SET @sql = '';
SELECT @sql += 'SELECT * FROM ' + QUOTENAME(o.name) + '; '
  FROM sys.objects o 
       INNER JOIN sys.columns c ON o.object_id = c.object_id
 WHERE c.name = 'TICKER';
EXEC (@sql);

3 Comments

Updated the answer to reflect the changed question.
This works but a cursor here is not the most efficient way to do this.
Added a non-cursor solution. The various solutions should give some good over-view as to various ways to approach the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.