Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How can I loop sp_help through for all tables or objects? Expand / Collapse
Author
Message
Posted Monday, January 05, 2009 3:02 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, May 28, 2010 9:56 AM
Points: 77, Visits: 248
I want to create a text file summary of the schematics for a database. Essentially, I want to have what is output for sp_help objectname, but for every object in the database. I can generate the script for the entire database, and that might work, but I don't really want a SQL script, just a summary of the schema (table/object names, field and column names, and datatypes and lengths. Keys and indexes would be nice, too).

A diagram isn't really what I'm after, either, as I think that would be unwieldy, plus I don't really want to have to drag and arrange every single table image in the diagram to get it to look right. I'm thinking a text file would be best.

I thought maybe I could create a cursor and loop through the objects or something, but don't really know what/how the output of running sp_help objectname would work - can I output to text somehow?

Maybe there's another tool or script that would do what I want? Thanks for any info or help!
Post #630166
Posted Monday, January 05, 2009 5:05 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855, Visits: 9,374
This should do it:
Declare @sql Nvarchar(MAX)
Set @sql = N''

Select @sql = @sql + N'
EXEC sp_help ''['+TABLE_SCHEMA+N'].['+TABLE_NAME+N']'';'
From INFORMATION_SCHEMA.TABLES

Print N'Executing:
'+@sql+'
'
EXEC(@sql)



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #630227
Posted Monday, January 05, 2009 10:20 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, August 31, 2012 6:08 AM
Points: 322, Visits: 459
Pete T (1/5/2009)
I want to create a text file summary of the schematics for a database. Essentially, I want to have what is output for sp_help objectname, but for every object in the database. I can generate the script for the entire database, and that might work, but I don't really want a SQL script, just a summary of the schema (table/object names, field and column names, and datatypes and lengths. Keys and indexes would be nice, too).

A diagram isn't really what I'm after, either, as I think that would be unwieldy, plus I don't really want to have to drag and arrange every single table image in the diagram to get it to look right. I'm thinking a text file would be best.

I thought maybe I could create a cursor and loop through the objects or something, but don't really know what/how the output of running sp_help objectname would work - can I output to text somehow?

Maybe there's another tool or script that would do what I want? Thanks for any info or help!



use master
go
select * into table_help from information_schema.columns
GO

DECLARE @sql varchar(8000)
DECLARE @BAK_PATH VARCHAR(4000)
declare c_bak cursor FAST_FORWARD FOR
select name from sys.databases where name <> 'master'
OPEN c_bak
FETCH NEXT FROM c_bak INTO @BAK_PATH
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'use '+@BAK_PATH+';
insert into table_help select * from information_schema.tables;'
PRINT (@SQL)
EXEC(@SQL)
FETCH NEXT FROM c_bak INTO @BAK_PATH
END
CLOSE C_BAK
DEALLOCATE C_BAK

select * from table_help
GO


_____________________________________________________________________________________________________________
Paresh Prajapati
+919924626601
http://paresh-sqldba.blogspot.com/
LinkedIn | Tweet Me | FaceBook | Brijj
Post #630306
Posted Monday, January 05, 2009 10:36 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855, Visits: 9,374
Yeah, but that uses a cursor. Better to avoid them.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #630320
Posted Monday, January 05, 2009 10:43 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, April 24, 2013 5:02 AM
Points: 2,365, Visits: 1,825
RBarryYoung (1/5/2009)
This should do it:
Declare @sql Nvarchar(MAX)
Set @sql = N''

Select @sql = @sql + N'
EXEC sp_help ''['+TABLE_SCHEMA+N'].['+TABLE_NAME+N']'';'
From INFORMATION_SCHEMA.TABLES

Print N'Executing:
'+@sql+'
'
EXEC(@sql)



:)


"Keep Trying"
Post #630322
Posted Monday, January 05, 2009 11:00 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, February 15, 2013 4:42 AM
Points: 1,798, Visits: 177
Make use of Un Documented
sp_MSForEachDB
and
sp_MSForEachTable

EXEC sp_MSForeachdb 'SELECT * FROM ?.INFORMATION_SCHEMA.TABLES' 

The ? is a way to represent the name of the database. This returns multiple result sets, but is the best you are going to get out of the box. Mainly, there is no consolidated metadata for the server. Each database hosts its own metadata.



Post #630326
Posted Monday, January 05, 2009 11:05 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855, Visits: 9,374
The undocumented procedures also use cursors.

Also, please note that the request was to document a dsingle database, not every database on the server at once.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #630327
Posted Tuesday, January 06, 2009 7:44 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, May 28, 2010 9:56 AM
Points: 77, Visits: 248
Interesting - thanks for all the replies! I'll play with it some more today and see what happens!
Post #630581
Posted Wednesday, May 16, 2012 1:52 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 21, 2013 12:41 PM
Points: 174, Visits: 247
Is there anyway that you could capture the output of sp_help <table_name> to some table. You know what I mean ?
Post #1301334
Posted Wednesday, May 16, 2012 2:30 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855, Visits: 9,374
That's really a different question. Best to post it separately in its own thread.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #1301359
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse