?
Solved

Looping code through tables in a SQl Server database

Posted on 2014-08-05
3
Medium Priority
?
259 Views
Last Modified: 2014-08-05
I developed code that lists the columns, types and attributes (below) and it lists what I need.
Capture3.PNGIn SysObjects, I can see xtype = 'u' as the selection criteria but how can I iterate through all tables in the database so that I will get a continuous listing of the columns, types and attributes?

select
    t.TABLE_NAME as 'Table', c.COLUMN_NAME as 'Column Name' ,c.DATA_TYPE as 'Type' ,c.CHARACTER_MAXIMUM_LENGTH as 'Length',c.NUMERIC_PRECISION as 'Numeric length',c.NUMERIC_SCALE as 'Decimals',c.DATETIME_PRECISION as 'Date/Time'
    from INFORMATION_SCHEMA.Tables t
        INNER JOIN INFORMATION_SCHEMA.Columns c ON t.TABLE_CATALOG=c.TABLE_CATALOG AND t.TABLE_SCHEMA=c.TABLE_SCHEMA AND t.TABLE_NAME=c.TABLE_NAME
    WHERE t.TABLE_NAME='assetprice'
    ORDER BY t.TABLE_NAME, c.COLUMN_NAME
0
Comment
Question by:Lenny Gray
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 40241681
You can use a cursor to loop over the set:
http://www.mssqltips.com/sqlservertip/1599/sql-server-cursor-example/

You would need to remove the table name in the where clause to get all of the tables.
0
 

Author Comment

by:Lenny Gray
ID: 40241754
Thank You !

Below is the code that finally worked!


use <yourdatabasename>

DECLARE @TheTableName VARCHAR(100) -- Table name  

DECLARE db_cursor CURSOR FOR  
SELECT name 
FROM [QRMAssistant].[sys].[sysobjects] 
Where xtype = 'u'

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @TheTableName   

WHILE @@FETCH_STATUS = 0   
	BEGIN   
	 
	SELECT t.TABLE_NAME as 'Table', c.COLUMN_NAME as 'Column Name' ,c.DATA_TYPE as 'Type' ,c.CHARACTER_MAXIMUM_LENGTH as 'Length',c.NUMERIC_PRECISION as 'Numeric length',c.NUMERIC_SCALE as 'Decimals',c.DATETIME_PRECISION as 'Date/Time' 
	FROM INFORMATION_SCHEMA.Tables t
	INNER JOIN INFORMATION_SCHEMA.Columns c ON t.TABLE_CATALOG=c.TABLE_CATALOG AND t.TABLE_SCHEMA=c.TABLE_SCHEMA AND t.TABLE_NAME=c.TABLE_NAME
	WHERE t.TABLE_NAME = @TheTableName
	ORDER BY t.TABLE_NAME, c.COLUMN_NAME


	FETCH NEXT FROM db_cursor INTO @TheTableName   

	END   

CLOSE db_cursor   
DEALLOCATE db_cursor

Open in new window

0
 

Author Closing Comment

by:Lenny Gray
ID: 40241757
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

839 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