Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select from all tables

Author  Topic 

jhaney
Starting Member

33 Posts

Posted - 2012-01-12 : 15:10:39
I need to figure out how I can sample all of the tables in a database that I inherited. Unfortunately there is not data dictionary or documentation avaiable. Basically I woiuld like to select the top 10 or 20 rows from every table and or view in the database.

Thanks for your help with this.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-01-12 : 15:27:27
This will generate the code, copy/paste the output into a new query window and run it:

select 'select top 20 * from ' + table_name
from information_schema.tables


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-12 : 15:30:47


SELECT 'SELECT TOP 20 ' + '''' + TABLE_NAME + '''' + ' AS TBNAME, * FROM ' + TABLE_NAME
FROM INFORMATION_SCHEMA.Tables
WHERE TABLE_TYPE = 'BASE TABLE'

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-01-12 : 18:26:32
same from sys.tables

select 'select top 20 * from ' + name
from sys.tables
Go to Top of Page

jhaney
Starting Member

33 Posts

Posted - 2012-01-13 : 07:25:08
Do I need to type out all of the table neames? Or will this select all of them?

Thanks
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-01-13 : 08:08:21
sys.tables does not include views does it? I know that information_schema.tables does...

If you run the code that tara gave you, it should give you what you wanted.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -