?
Solved

Generate table owner list for all the tables in a database

Posted on 2011-02-21
10
Medium Priority
?
614 Views
Last Modified: 2013-11-24
I am using SQL 2008 please how do I write a query to generate table owner list and sizes for all the tables in a database. The query headings will be TABLENAME, OWNER, SIZE
0
Comment
Question by:agbelo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 34948274
Well, I think this can help you as a start point:

SELECT SCHEMA_NAME(T.[schema_id]) + '.' + T.name, USER_NAME(S.principal_id) AS Owner
FROM sys.tables T
INNER JOIN sys.schemas S
ON T.[schema_id] = S.[schema_id]
ORDER BY T.name

In SQL Server 2008 the owner is the one that own the schema where the object resides.
0
 
LVL 15

Accepted Solution

by:
jorge_toriz earned 2000 total points
ID: 34948282
Well, in the preceeding sentence, I must fix: "the owner (in tables) is the one that own the schema where the object resides"
0
 

Author Comment

by:agbelo
ID: 34952700
That was surely very helpful so what about if this was for a SQL 2000 server? How would the code look?
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Closing Comment

by:agbelo
ID: 34952757
Solution will be complete when my follow up question is answered
0
 

Author Comment

by:agbelo
ID: 34952789
And also include the table sizes column in your answer
0
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 34952794
Ups... I can't really remember anything about sql 2000, let me find the system views and verify if this information can be get
0
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 34952892
Well, you can get the estimated row size with this one:

SELECT SCHEMA_NAME(T.[schema_id]) + '.' + T.name, USER_NAME(S.principal_id) AS Owner, SUM(C.max_length) AS EstimatedRowSize
FROM sys.tables T
INNER JOIN sys.schemas S
ON T.[schema_id] = S.[schema_id]
INNER JOIN sys.columns C
ON T.[object_id] = C.[object_id]
GROUP BY T.[schema_id], T.name, S.principal_id
ORDER BY T.name
0
 

Author Comment

by:agbelo
ID: 34953077
Please I asked for the estimated table size for each table and make values return in MB
0
 

Author Comment

by:agbelo
ID: 34953155
I actuallly have a query that would do what I need I just need to re-engineer it to give me the object owners. Maybe you can help me here. Below is the query I am using and it works in both SQL 2000 and SQL 2008. Please help to modify it to return the object owners

SELECT so.[name], used AS "# of Pages", rows AS "# of Rows", (used * 8) /
1024 AS "# of MB"  
FROM sysobjects so JOIN sysindexes si ON so.id = si.id
WHERE so.xtype = 'U'
ORDER BY used DESC
0
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 34953375
You can use this query to get the table size:

SELECT T.name AS TableName, SUM(P.record_count * P.avg_record_size_in_bytes) AS TotalSize
FROM sys.tables T
CROSS APPLY sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'DETAILED') AS P
WHERE T.[object_id] = P.[object_id]
GROUP BY T.name
ORDER BY T.name
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

801 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