5/10/2018 12:11:00 a. m.
Share:
Blog Informático sobre Análisis y Desarrollo de Software.
-- -- -- --Cursor que contiene todos los objetos que ocupan espacio DECLARE objects_cursor CURSOR LOCAL FAST_FORWARD READ_ONLY for SELECT s.name + '.' + o.name from sys.schemas s INNER JOIN sys.objects o ON o.schema_id = s.schema_id WHERE o.type = 'S' or --Tablas de sistema o.type = 'U' or --Tablas de usuario o.type = 'V' or --Vistas (solo las indexadas devuelven tamaño) o.type = 'SQ' or --Cola de servicio o.type = 'IT' -- Tablas internas usadas p.e. por el Service Broker o los indices XML --Tabla temporal para albergar los resultados CREATE TABLE #results (name_with_schema SYSNAME, name SYSNAME, rows CHAR(11), reserved VARCHAR(18), data VARCHAR(18), index_size VARCHAR(18),Unused VARCHAR(18)) DECLARE @resultado AS TABLE (name SYSNAME, rows CHAR(11), reserved VARCHAR(18), data VARCHAR(18), index_size VARCHAR(18),Unused VARCHAR(18)) --Recorremos el cursor obteniendo la información de espacio ocupado DECLARE @object_name AS SYSNAME OPEN objects_cursor FETCH NEXT FROM objects_cursor INTO @object_name; WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO @resultado EXEC sp_spaceused @object_name INSERT INTO #results SELECT @object_name, x.* from @resultado as x delete from @resultado FETCH NEXT FROM objects_cursor INTO @object_name; END; CLOSE objects_cursor; DEALLOCATE objects_cursor; -- Quitamos "KB" para poder ordenar UPDATE #results SET reserved = LEFT(reserved,LEN(reserved)-3), data = LEFT(data,LEN(data)-3), index_size = LEFT(index_size,LEN(index_size)-3), Unused = LEFT(Unused,LEN(Unused)-3) --Ordenamos la información por el tamaño ocupado SELECT name_with_schema, Name, reserved AS [Tamaño en Disco (KB)], data AS [Datos (KB)], index_size AS [Indices (KB)], Unused AS [No usado (KB)], Rows AS Filas FROM #results ORDER BY name_with_schema, name, CONVERT(bigint, reserved) DESC drop table #results
-- -- CREATE PROCEDURE [dbo].[PROC_HABILITAR_DESHABILITAR_TRIGGERS_BASE] @HABILITAR bit = 1 AS DECLARE @string VARCHAR(8000) DECLARE @NombreTabla NVARCHAR(500) DECLARE @NombreEsquema NVARCHAR(500) DECLARE @instruccion NVARCHAR(500) SET @instruccion = CASE WHEN @HABILITAR = 1 THEN 'ENABLE' ELSE 'DISABLE' END DECLARE cur CURSOR FOR SELECT DISTINCT sch.name AS nombre_esquema, ta.name as 'nombre_tabla' FROM sys.objects tr INNER JOIN sys.schemas sch ON tr.schema_id = sch.schema_id inner join sys.objects ta on tr.parent_object_id = ta.object_id where tr.type = 'TR' order by sch.name, ta.name OPEN cur FETCH next FROM cur INTO @NombreEsquema, @NombreTabla WHILE @@fetch_status = 0 BEGIN SET @string ='Alter table '+ @NombreEsquema + '.'+@NombreTabla + ' ' + @instruccion + ' trigger all' EXEC (@string) FETCH next FROM cur INTO @NombreEsquema, @NombreTabla END CLOSE cur DEALLOCATE cur GO
-- -- SELECT name AS tbname, * FROM sysobjects WHERE id IN(SELECT parent_obj FROM sysobjects WHERE xtype='tr') -- o SELECT ta.name AS tbname, * FROM sys.objects ta inner join sys.objects tr on ta.object_id = tr.parent_object_id WHERE tr.type = 'TR'
-- -- select * from Information_schema.Columns where table_name = 'WorkOrder'
-- -- SELECT sch.name AS nombre_esquema, ta.name as 'nombre_tabla', tr.name as nombre_trigger, tr.object_id, * FROM sys.objects tr INNER JOIN sys.schemas sch ON tr.schema_id = sch.schema_id inner join sys.objects ta on tr.parent_object_id = ta.object_id where tr.type = 'TR' order by sch.name, ta.name
-- -- SELECT TAB.name as Table_Name , TRIG.name as Trigger_Name , TRIG.is_disabled FROM [sys].[triggers] as TRIG inner join sys.tables as TAB on TRIG.parent_id = TAB.object_id --where TRIG.is_disabled = 1 --t.name in ('WorkOrder', 'Vendor');
-- -- select so.name, text from sysobjects so, syscomments sc where type = 'TR' and so.id = sc.id and so.name like '%WorkOrder%'
-- -- SELECT o.name AS tbname, s.name AS schema_name FROM sys.objects o INNER JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE type='U' ORDER BY schema_name,o.name
-- -- SELECT schema_Name(schema_id) as Nombre_Esquema, [name] as Nombre_Objeto, -- Nombre de la tabla, procedimiento almacenado, vista o función [type] as Tipo_Objeto -- 'V' para Vistas, 'U' para Tablas, 'P' para Procedimientos Almacenados, 'FN' para funciones FROM sys.objects WHERE [type_desc] IN ( 'USER_TABLE', 'SQL_STORED_PROCEDURE', 'VIEW', 'SQL_SCALAR_FUNCTION') AND [name] NOT LIKE 'sp_%' AND [name] NOT LIKE 'fn_%' ORDER BY 3 DESC, -- primero el tipo de objeto 1 ASC, -- luego el esquema 2 ASC -- luego el nombre de la tabla / función
-- -- SELECT * FROM SYS.SYSCOMMENTS WHERE TEXT LIKE '%CREATE TRIGGER%'
-- -- SELECT s.name,tab.name as tablename, ind.name as indexname FROM sys.indexes ind INNER JOIN sys.tables tab ON ind.object_id = tab.object_id INNER JOIN sys.schemas s on tab.schema_id = s.schema_id ORDER BY s.name,tab.name
-- -- SELECT name, o.create_date, o.modify_date, * FROM AdventureWorks2012.sys.objects o WHERE type = 'p' AND name NOT LIKE 'sp_%' and (convert(date, modify_date) >= '2012-02-01' or convert(date, create_date) >= '2012-02-01') ORDER BY o.modify_date DESCY si lo que se quiere listar son las vistas, entonces habrá que cambiar type='p' por type='v', y para listar funciones escalares usar type = 'fn' y para funciones de tabla: type = 'tf'
-- -- create database Marketing on primary (Name= ‘Marketing′, Filename=’D:\SQL SERVER - practicas\Marketing.mdf’, Size= 4Mb, MaxSize= 30MB, FileGrowth=10%) log on (Name=’Marketing_LDF’, Filename=’D:\SQL SERVER - practicas\Marketing.ldf’, Size= 4Mb, MaxSize= 30MB, FileGrowth=10%) go
-- -- -- Crear una base de datos AdventureWorks2012 en base al archivo AdventureWorks2012_Data.MDF y AdventureWorks2012_Log.LDF CREATE DATABASE [AdventureWorks2012] ON (FILENAME = N'D:\SQL SERVER - practicas\BD_AdventureWorks\AdventureWorks2012_Data.mdf') LOG ON (FILENAME = [D:\SQL SERVER - practicas\BD_AdventureWorks\AdventureWorks2012_Log.ldf]) FOR ATTACH
-- -- -- Cambiar el nombre lógico de un archivo ALTER DATABASE AdventureWorks2012 MODIFY FILE (NAME='AdventureWorks2012_Data', NEWNAME='AdventureWorks_Data') GO ALTER DATABASE AdventureWorks2012 MODIFY FILE (NAME='AdventureWorks2012_Log', NEWNAME='AdventureWorks_Log') GO
-- -- alter database AdventureWorks set restricted_user with rollback IMMEDIATE alter database AdventureWorks set MULTI_USER alter database AdventureWorks set offline with rollback immediate
-- -- alter database AdventureWorks set online
-- -- SELECT distinct TABLE_SCHEMA,TABLE_NAME,COLUMN_Name FROM INFORMATION_SCHEMA.COLUMNS, sys.Objects so WHERE --TABLE_SCHEMA = 'dbo' and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1 and so.Type = 'U' ORDER BY TABLE_NAME
-- -- RESTORE HEADERONLY FROM DISK='D:\SQL SERVER - practicas\BD_AdventureWorks\AdventureWorksDW2012.bak' RESTORE FILELISTONLY FROM DISK='D:\SQL SERVER - practicas\BD_AdventureWorks\AdventureWorksDW2012.bak' RESTORE LABELONLY FROM DISK='D:\SQL SERVER - practicas\BD_AdventureWorks\AdventureWorksDW2012.bak'
-- -- RESTORE DATABASE AdventureWorksDW2012 FROM DISK='D:\SQL SERVER - practicas\BD_AdventureWorks\AdventureWorksDW2012.bak' WITH FILE = 1, RECOVERY, MOVE 'AdventureWorksDW2012' TO 'D:\SQL SERVER - practicas\DATA\AdventureWorksDW2012.mdf', MOVE 'AdventureWorksDW2012_log' TO 'D:\SQL SERVER - practicas\DATA\AdventureWorksDW2012_Log.ldf'
0 comentarios: