FTP | CCD | Buscar | Trucos | Trabajo | Foros |
|
Registrarse | FAQ | Miembros | Calendario | Guía de estilo | Temas de Hoy |
|
Herramientas | Buscar en Tema | Desplegado |
#1
|
||||
|
||||
Procedimientos Utiles !!!
He aquí un listado de procedimientos que en los ultimos años he hecho para mi trabajo y me han sido muy útiles...
Tal vez alguno pueda usarlos o poner más !!! Reindexar todas las tablas de una db, esto es muy distinto a como lo hace el mantenimiento del MSSQL, quedan mucho mejor... Código:
CREATE PROCEDURE xreindex AS SET NOCOUNT ON DECLARE @Table char(100) DECLARE IndexCursor CURSOR FOR SELECT name FROM sysobjects WHERE type='U' OPEN IndexCursor FETCH NEXT FROM IndexCursor INTO @Table WHILE @@FETCH_STATUS=0 BEGIN DBCC DBREINDEX(@Table) FETCH NEXT FROM IndexCursor INTO @Table END CLOSE IndexCursor DEALLOCATE IndexCursor GO Código:
CREATE PROCEDURE xfragmenta @Tabla varchar(100)='1021' AS IF @Tabla = '1021' BEGIN PRINT 'Analizando fragmentación de todas las tablas...' PRINT '============================================' PRINT ' ' DBCC SHOWCONTIG WITH TABLERESULTS, ALL_INDEXES END ELSE BEGIN PRINT 'Analizando fragmentación tabla: '+@Tabla PRINT '==============================' PRINT ' ' DBCC SHOWCONTIG (@Tabla) END GO Código:
CREATE PROCEDURE xdefrag @maxfrag DECIMAL=30.0 AS SET NOCOUNT ON DECLARE @tablename VARCHAR (128) DECLARE @execstr VARCHAR (255) DECLARE @objectid INT DECLARE @indexid INT DECLARE @frag DECIMAL DECLARE tables CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' CREATE TABLE #fraglist ( ObjectName CHAR (255), ObjectId INT, IndexName CHAR (255), IndexId INT, Lvl INT, CountPages INT, CountRows INT, MinRecSize INT, MaxRecSize INT, AvgRecSize INT, ForRecCount INT, Extents INT, ExtentSwitches INT, AvgFreeBytes INT, AvgPageDensity INT, ScanDensity DECIMAL, BestCount INT, ActualCount INT, LogicalFrag DECIMAL, ExtentFrag DECIMAL) OPEN tables FETCH NEXT FROM tables INTO @tablename WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO #fraglist EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS') FETCH NEXT FROM tables INTO @tablename END CLOSE tables DEALLOCATE tables DECLARE indexes CURSOR FOR SELECT ObjectName, ObjectId, IndexId, LogicalFrag FROM #fraglist WHERE LogicalFrag >= @maxfrag AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0 OPEN indexes FETCH NEXT FROM indexes INTO @tablename, @objectid, @indexid, @frag WHILE @@FETCH_STATUS = 0 BEGIN PRINT 'Ejecutando DBCC INDEXDEFRAG (0, ' + RTRIM(@tablename) + ', ' + RTRIM(@indexid) + ') - fragmentación actual ' + RTRIM(CONVERT(varchar(15),@frag)) + '%' SELECT @execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@objectid) + ', ' + RTRIM(@indexid) + ')' EXEC (@execstr) FETCH NEXT FROM indexes INTO @tablename, @objectid, @indexid, @frag END CLOSE indexes DEALLOCATE indexes GO Código:
CREATE PROCEDURE xbusca_campo @campo varchar(50) AS select so.name as tabla, sy.name as campo, case when sy.isnullable=0 then 'NO' else 'SI' end as nulls, sy.length tamaño, st.name as tipo, sc.text as defecto from sysobjects so (nolock) inner join syscolumns sy (nolock) on so.id=sy.id and so.type='U' and sy.name like @campo inner join systypes st (nolock) on sy.xusertype=st.xusertype left join syscomments sc (nolock) on sy.cdefault=sc.id order by so.name GO Código:
CREATE PROCEDURE xbusca_text_proc @cadena_buscada varchar(100) AS set @cadena_buscada='%'+@cadena_buscada+'%' select distinct so.name from sysobjects so (nolock) inner join syscomments sc (nolock) on so.id = sc.id and so.type='P' and sc.text like @cadena_buscada order by so.name GO
__________________
El diseño dira si tiene futuro... |
#2
|
||||
|
||||
Pues siempre es de agradecer que alguien a instancias propias quiera compartir su código de manera altruista.
Un Saludo y gracias.
__________________
Guía de Estilo de los Foros Cita:
|
#3
|
||||
|
||||
Este es para generar el string de creación de una tabla ya existente...
Como ??? Pues, si tengo una tabla en mi DB que se llama Albaranes y en un procedimientos por un motivo X, necesito hacer una tabla temporal con su misma estructura, podemos hacerlo facilmente con este procedimiento que me retorna el string de creación de la tabla, pero de forma temporal # Código:
CREATE PROCEDURE crea_tabla @tabla varchar(50), @tabli varchar(50) AS Declare @id int, @campo varchar(200), @tipo varchar(100), @tamano int, @collation bit, @sql varchar(5000) Declare @Temporal Table (campo varchar(200), tipo varchar(100), tamano int, collation bit, orden int) select @id=id from sysobjects (nolock) where name=@tabla insert into @Temporal select sc.name, sy.name, sc.length, 1, sc.colid from syscolumns sc (nolock) inner join systypes sy (nolock) on sc.xtype=sy.xtype and sc.id=@id and sc.collation is null insert into @Temporal select sc.name, sy.name, sc.length, 0, sc.colid from syscolumns sc (nolock) inner join systypes sy (nolock) on sc.xtype=sy.xtype and sc.id=@id and sc.collation is not null set @sql='Create Table #'+@tabli+' (' While exists(select top 1 * from @Temporal) Begin select top 1 @campo=campo, @tipo=tipo, @tamano=tamano, @collation=collation from @Temporal order by orden if @collation=0 set @sql=@sql+@campo+' '+@tipo+'('+cast(@tamano as varchar)+'), ' else set @sql=@sql+@campo+' '+@tipo+', ' delete @Temporal where campo=@campo End set @sql=substring(@sql,1,len(@sql)-1)+')' select @sql GO Código:
crea_tabla 'Original', 'Copia' Código:
Create Table #Copia (Cod_Producto varchar(50), Cod_proveedor int, Cod_Fam_Pro int, Nombre_Producto varchar(100), Descrip_Producto varchar(200) , Fec_Alta datetime, Estado bit, Cod_EAN varchar(20), Cod_Pro_proveedor varchar(20), cod_proveedor0 int, cod_producto0 varchar(50) , tipo varchar(50), filler varchar(100), gramos int, unidades float, coste_material float, coste_produccion float, iva float)
__________________
El diseño dira si tiene futuro... |
|
|
|