Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL Scripts & Samples

Posted Jan 8, 2007

sp_dba_table.sql

By DatabaseJournal.com Staff



>>Script Language and Platform: Sybase Transact SQL
This stored procedure displays user of system table list ordered by size (Ko).

Author: jflebon


use sybsystemprocs
go

if exists (select name from sybsystemprocs..sysobjects 
  where name="sp_dba_table" and type="P")
   begin
        drop procedure sp_dba_table
   end
go

create procedure sp_dba_table @tabletype char(1) = NULL
as
/*

 *@(#) Author   : JF LEBON
 *@(#) Version  : 1.0
 *@(#) Date     : 28/03/2004
 *@(#) Syntax   : exec sp_dba_table <table type> 'S' system tables 'U' user table
 *@(#) Comments :
 *@(#) Changes  :

 */

set nocount on

-- Nobody but SA !
if suser_id() !=1
   begin
        print "WARNING: You have to be SA to execute this procedure."
	return (1)
   end

-- Never use it inside a transaction !
if @@trancount > 0
   begin
   	print "WARNING: You can NOT use this procedure inside a transaction."
	return (1)
   end

-- Few variables...
declare @page_size int

-- User tables or System tables, if none default is 'S' system tables...
if @tabletype is not null
   begin
        select @tabletype = upper(@tabletype)
        if @tabletype not in ('U','S') begin goto USAGE end
   end
else
    begin
         select @tabletype = 'S'
    end

-- What is the dataserver's page size ?
select @page_size = low/1024 from master.dbo.spt_values where type = 'E' and number = 1

-- Enjoy your flight !
select name = o.name,
       row_total = convert(real,sum(rowcnt(i.doampg))),
       data_size = convert(real,sum(convert(numeric(32, 9),data_pgs(i.id, i.doampg)) * @page_size)),
       index_size = convert(real,sum(convert(numeric(32, 9),data_pgs(i.id, i.ioampg)) * @page_size)),
       reserved = convert(real,sum(convert(numeric(32, 9),(reserved_pgs(i.id, i.doampg) + reserved_pgs(i.id, i.ioampg))) * @page_size)),
       unused = convert(real,sum(convert(numeric(32, 9),((reserved_pgs(i.id, i.doampg) + reserved_pgs(i.id, i.ioampg))
	        - (data_pgs(i.id, i.doampg) + data_pgs(i.id, i.ioampg)))) * @page_size))
from master.dbo.spt_values d, sysobjects o, sysindexes i
     where o.type in (@tabletype) and o.id = i.id and d.number = 1 and d.type = "E"
     group by o.id order by data_size desc

return (0)

USAGE:
print "USAGE: exec sp_dba_table ['table type']"
print "Exemple: exec sp_dba_table 'U'"
print "         exec sp_dba_table 'S'"
go


Disclaimer: We hope that the information on these script pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, or fitness for a particular purpose... Disclaimer Continued


Back to Database Journal Home



SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM