Creada el: 07/07/98 - Actualizada el: 01/08/98
Procedimiento Almacenado que actualiza las estadisticas de las tablas. |
| CREATE PROCEDURE sp_sys_updatestats
AS Begin declare @table varchar(30) declare object_cursor cursor for select name from sysobjects where type = 'U' set nocount on open object_cursor fetch next from object_cursor into @table while (@@fetch_status <> -1) begin if (@@fetch_status <> -2) begin print @table exec ('update statistics ' + @table) end fetch next from object_cursor into @table end print 'Las estadisticas han sido cambiadas para todas las tablas.' deallocate object_cursor end |
Procedimiento Almacenado que Informa los tamaños de las tablas y los indices |
| create procedure sp_sys_allspace -- IMPORTANT! usage info. should be updated before running me. -- Empty tables are ommitted from the results. as declare @objname varchar(92) -- The object we want size on. declare @id int -- The object id of @objname. declare @type smallint -- The object type. declare @pages int -- Working variable for size calc. declare @dbname varchar(30) declare @dbsize dec(15,0) declare @trows dec(15,0) declare @tall dec(15,0) declare @tdata dec(15,0) declare @tidx dec(15,0) declare @tuu dec(15,0) declare @srows dec(15,0) declare @sall dec(15,0) declare @sdata dec(15,0) declare @sidx dec(15,0) declare @suu dec(15,0) declare @irows dec(15,0) declare @iall dec(15,0) declare @idata dec(15,0) declare @iidx dec(15,0) declare @iuu dec(15,0) declare @iname varchar(20) declare @msg varchar(80) declare AllTables insensitive cursor for select name from sysobjects order by name open AllTables /* ** We need to create a temp table to do the calculation. ** reserved: sum(reserved) where indid in (0, 1, 255) ** data: sum(dpages) where indid < 2 + sum(used) where indid = 255 (text) ** indexp: sum(used) where indid in (0, 1, 255) - data ** unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */ create table #spt_space ( name varchar(92), rows int null, reserved dec(15) null, data dec(15) null, indexp dec(15) null, unused dec(15) null ) set nocount on select getdate() fetch next from AllTables into @objname /* ** Insert the row for the table */ insert into #spt_space (name,rows,reserved,data,indexp,unused) values ('zzUser Table Totals',0,0,0,0,0) insert into #spt_space (name,rows,reserved,data,indexp,unused) values ('zzSystem Table Totals',0,0,0,0,0) while @@fetch_status = 0 begin /* ** Find the object. */ select @id = null select @id = id, @type = sysstat & 0xf from sysobjects where id = object_id(@objname) /* ** See if it's a space object. ** types are: ** 1 - system table ** 2 - view ** 3 - user table ** 4 - sproc ** 6 - default ** 7 - rule ** 8 - trigger ** 9 - primary key ** 10 - check constraint ** 11 - foreign key ** 12 - replication filter stored proc */ if @type in (1,3) begin /* ** Insert the row for the table */ insert into #spt_space (name) values (@objname) /* ** Now calculate the summary data. ** reserved: sum(reserved) where indid in (0, 1, 255) */ /* insert into #spt_space (reserved) */ select @iall = sum(reserved) from sysindexes where indid in (0, 1, 255) and id = @id update #spt_space set reserved = @iall where name = @objname /* Update Totals */ if @type = 3 update #spt_space set reserved = reserved + @iall where name = 'zzUser Table Totals' else update #spt_space set reserved = reserved + @iall where name = 'zzSystem Table Totals' /* ** data: sum(dpages) where indid < 2 ** + sum(used) where indid = 255 (text) */ select @pages = sum(dpages) from sysindexes where indid < 2 and id = @id select @pages = @pages + isnull(sum(used), 0) from sysindexes where indid = 255 and id = @id update #spt_space set data = @pages where name = @objname /* Update Totals */ if @type = 3 update #spt_space set data = data + @pages where name = 'zzUser Table Totals' else update #spt_space set data = data + @pages where name = 'zzSystem Table Totals' /* index: sum(used) where indid in (0, 1, 255) - data */ select @iidx = sum(used) from sysindexes where indid in (0, 1, 255) and id = @id update #spt_space set indexp = @iidx - data where name = @objname /* Update Totals */ if @type = 3 update #spt_space set indexp = indexp + (@iidx - data) where name = 'zzUser Table Totals' else update #spt_space set indexp = indexp + (@iidx - data) where name = 'zzSystem Table Totals' /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */ update #spt_space set unused = reserved - @iidx where name = @objname /* Update Totals */ if @type = 3 update #spt_space set unused = unused + (reserved - @iidx) where name = 'zzUser Table Totals' else update #spt_space set unused = unused + (reserved - @iidx) where name = 'zzSystem Table Totals' select @irows = i.rows from sysindexes i where i.indid < 2 and i.id = @id update #spt_space set rows = @irows where name = @objname /* Update totals */ if @type = 3 update #spt_space set rows = rows + @irows where name = 'zzUser Table Totals' else update #spt_space set rows = rows + @irows where name = 'zzSystem Table Totals' end fetch next from AllTables into @objname end /* end while loop here and print out totals */ select 'Table' = substring(#spt_space.name, 1, 20), 'Rows' = str(rows,12), 'Reserved KB' = (str(reserved * d.low / 1024.,15)), 'Data Space KB' = (str(data * d.low / 1024.,15)), 'Index Space KB' = (str(indexp * d.low / 1024.,15)), 'Unused Space KB' = (str(unused * d.low / 1024.,15)) from #spt_space, master.dbo.spt_values d where (d.number = 1 and d.type = 'E') -- and rows 0 -- do not show empty tables order by #spt_space.name close AllTables deallocate AllTables return (0)
|
Procedimiento Almacenado muestra las estructuras de las tablas |
| CREATE PROCEDURE sp_sys_table As Begin /********************************************************************/ -- Procedimiento que imprime detalladamente todas las tablas /********************************************************************/ if exists ( select * from sysobjects where id = object_id('tempdb.dbo.boolean') and sysstat & 0xf = 3) truncate table tempdb.dbo.boolean else create table #boolean ( value bit, text varchar(4)) insert into #boolean values (0, 'No') insert into #boolean values (1, 'Yes') /********************************************************************/ print 'Tipos defindos por el usuario' print '=============================' print ' ' SELECT usertype, type, name INTO #base_systypes FROM systypes WHERE usertype < 100 SELECT 'User Type' = CONVERT(char(25), A.name), 'Base Type' = CONVERT(char(20), B.name), 'Bytes' = A.length, 'Nulls' = A.allownulls FROM systypes A, #base_systypes B WHERE A.type = B.type AND A.usertype <> B.usertype AND B.name <> 'sysname' AND A.name <> 'sysname' AND A.name <> 'binary' AND A.name <> 'timestamp' ORDER BY A.name declare @table_id int, @table_name varchar(30), @line varchar(70) print ' ' print ' ' print 'Definicion de las Tablas' print '========================' print ' ' declare dict_csr cursor for select id, name from sysobjects where type = 'u' order by name open dict_csr fetch next from dict_csr into @table_id, @table_name while (@@fetch_status = 0) begin select @line = 'Tabla: ' + @table_name print @line print '' select 'Field' = CONVERT(char(25), A.name), 'Type' = CONVERT(char(20), B.name), 'Bytes' = A.length, 'Nulls' = C.text from syscolumns A, systypes B, #boolean C where id = @table_id and A.usertype = B.usertype and B.allownulls = C.value fetch next from dict_csr into @table_id, @table_name print '' print '' end close dict_csr deallocate dict_csr drop table #boolean DROP TABLE #base_systypes End return (0) |
Procedimiento que expande una base de datos |
| -- Incrementa una base de datos en
un tamaño de @increment, -- la unidad @increment = 1Mb. create proc
dbexpand (@dbname sysname, @dbdevice sysname, @increment int) |
Por Luis Walter Reynoso para Ethek & Friends