Solved

Select the values from the first column in all tables in my database

Posted on 2009-05-19
10
225 Views
Last Modified: 2012-06-21
Hi,

I have a database with a number of tables, I want to return the values of the first column in each table.

To illustrate, if there are, say, 3 tables in the database MyDB called Table1, Table2, Table3 with columns as follows:

Table1 has columns LastName, FirstName, Title
Table2 has columns Street, City, Country
Table3 has columns Age, Ethnicity, Gender, AccommodationType

Then I want the equivalent of doing:

Select LastName from Table1
Select Street from Table2
Select Age from Table3
etc.

Of course, in the real database there are about fifty tables and each has its own ideosyncrasies and column names are different in each and of differing types. Can I automate this using schema objects like 'Column_Name' etc in a loop of some sort for each table in the DB?

Thanks
Iain
0
Comment
Question by:iaing1000
  • 4
  • 3
  • 3
10 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 24421187
you can run this query to get them..

select column_name from information_schema.columns
where table_name in('table1', 'table2', 'table3')
and ordinal_position = 1
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24421272
Use the query below as the Select query for your cursor and execute the statement for each and every values:

Hope this helps
SELECT 'SELECT ' + CO.COLUMN_NAME + ' FROM ' + TA.TABLE_SCHEMA + '.' + TA.TABLE_NAME + ';'

FROM INFORMATION_SCHEMA.TABLES TA, INFORMATION_SCHEMA.COLUMNS CO

WHERE TA.TABLE_NAME = CO.TABLE_NAME

AND TA.TABLE_SCHEMA = CO.TABLE_SCHEMA

AND CO.ORDINAL_POSITION = 1

Open in new window

0
 

Author Comment

by:iaing1000
ID: 24421276
Hi,

Thanks for the fast response.

This only returns the actual names of the columns themselves. If you can imagine the result of the select staements I've used to illustrate the question, that is what I'm after.

Also, I won't have time to explicitly outline the names of the tables as you've outlined, since there will be a lot of them. Possibly increasing from 50 to 900 ,so it all needs to be automated with no explicit reference to the tables or columns themselves

Thanks
Iain
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24421319
so, you want something to select ALL records from the "first" column of all of your tables?
0
 

Author Comment

by:iaing1000
ID: 24421321
Hi rrjegan,

I'll have a look at this, it looks useful.

It seems to return the actual SELECT statements themselves though. I'm after the result of running each of these resultant SELECTS one after the other, I'm trying to find a way of re-jigging your SQL to do this because it must be close.

 The result I'm after will be the same as pasting this...

Select LastName from Table1
Select Street from Table2
Select Age from Table3
etc.

...into the New Query area and clicking Execute

Thanks
Iain


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:iaing1000
ID: 24421329
Hi chapmandew,

Yes that's exactly right,

Thanks for being so quick too,

Iain
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 24421346
declare @sql nvarchar(4000), @tablename nvarchar(255), @columnname nvarchar(255)

DECLARE  CursorTemplate CURSOR
FAST_FORWARD FOR       
      
select table_name, column_name from INFORMATION_SCHEMA.COLUMNS
where ordinal_position = 1

OPEN CursorTemplate

FETCH NEXT FROM CursorTemplate
INTO      @tablename, @columnname

WHILE (@@FETCH_STATUS = 0)
BEGIN
      set @sql = 'select ' + @columnname + ' from ' + @tablename
      exec sp_executesql @sql

      FETCH NEXT FROM CursorTemplate
      INTO      @tablename, @columnname

END

CLOSE CursorTemplate
DEALLOCATE CursorTemplate
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24421352
Test this one out which works as you explained:


DECLARE @sql nvarchar(1000);

DECLARE select_cur CURSOR FOR 

SELECT 'SELECT ' + CO.COLUMN_NAME + ' FROM ' + TA.TABLE_SCHEMA + '.' + TA.TABLE_NAME + ';'

FROM INFORMATION_SCHEMA.TABLES TA, INFORMATION_SCHEMA.COLUMNS CO

WHERE TA.TABLE_NAME = CO.TABLE_NAME

AND TA.TABLE_SCHEMA = CO.TABLE_SCHEMA

AND CO.ORDINAL_POSITION = 1;
 

    OPEN select_cur;
 

    FETCH NEXT FROM select_cur

    INTO @sql;
 

    WHILE @@FETCH_STATUS = 0

       BEGIN

          EXEC sp_executesql @sql;

          

          FETCH NEXT FROM select_cur

          INTO @sql;

       END;

    CLOSE select_cur;

    DEALLOCATE select_cur;

Open in new window

0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 250 total points
ID: 24421373
A small change in SELECT query:
DECLARE @sql nvarchar(1000);

DECLARE select_cur CURSOR FOR 

SELECT 'SELECT ' + CO.COLUMN_NAME + ' FROM ' + TA.TABLE_SCHEMA + '.' + TA.TABLE_NAME + ';'

FROM INFORMATION_SCHEMA.TABLES TA, INFORMATION_SCHEMA.COLUMNS CO

WHERE TA.TABLE_NAME = CO.TABLE_NAME

AND TA.TABLE_SCHEMA = CO.TABLE_SCHEMA

AND CO.ORDINAL_POSITION = 1

AND TA.TABLE_TYPE = 'BASE TABLE';

 

    OPEN select_cur;

 

    FETCH NEXT FROM select_cur

    INTO @sql;

 

    WHILE @@FETCH_STATUS = 0

       BEGIN

          EXEC sp_executesql @sql;

          

          FETCH NEXT FROM select_cur

          INTO @sql;

       END;

    CLOSE select_cur;

    DEALLOCATE select_cur;

Open in new window

0
 

Author Comment

by:iaing1000
ID: 24421488
Hi,

Those both worked perfectly!

Thank you both ever so much,

Points split in a minute or two,

Thanks
Iain
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now