The document contains code for two SQL triggers. The first trigger saves error information like the error number, severity, state, procedure, line, and message to an ErrorLog table when a DDL event occurs on the database. The second stored procedure retrieves all records from the Producto table in the AdventureWorks2019 database that have been modified within the last 24 hours.
The document contains code for two SQL triggers. The first trigger saves error information like the error number, severity, state, procedure, line, and message to an ErrorLog table when a DDL event occurs on the database. The second stored procedure retrieves all records from the Producto table in the AdventureWorks2019 database that have been modified within the last 24 hours.
The document contains code for two SQL triggers. The first trigger saves error information like the error number, severity, state, procedure, line, and message to an ErrorLog table when a DDL event occurs on the database. The second stored procedure retrieves all records from the Producto table in the AdventureWorks2019 database that have been modified within the last 24 hours.
The document contains code for two SQL triggers. The first trigger saves error information like the error number, severity, state, procedure, line, and message to an ErrorLog table when a DDL event occurs on the database. The second stored procedure retrieves all records from the Producto table in the AdventureWorks2019 database that have been modified within the last 24 hours.
Download as TXT, PDF, TXT or read online from Scribd
Download as txt, pdf, or txt
You are on page 1of 2
sebastian galindo
daniel beitar
--Trigger para guardar la informacion de un error cuando un usuario hace un evento
(los puestos en el case) un dato en una tabla. CREATE OR ALTER TRIGGER [SaveDDLErrors] ON DATABASE FOR DDL_DATABASE_LEVEL_EVENTS AS BEGIN SET NOCOUNT ON; -- No muestra las filas afectadas
-- En caso de que el error no se de en un stored Procedure, el codigo
verifica si fue una accion de agregar, consultar, borrar o modificar, etc.. SET @operationType = CASE EVENTDATA().value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(128)') -- Case para darle un valor a la variable operationType WHEN 'CREATE_TABLE' THEN 'CREAR' WHEN 'ALTER_TABLE' THEN 'MODIFICAR' WHEN 'DROP_TABLE' THEN 'ELIMINAR' WHEN 'CREATE_VIEW' THEN 'CREAR' WHEN 'ALTER_VIEW' THEN 'MODIFICAR' WHEN 'DROP_VIEW' THEN 'ELIMINAR' WHEN 'CREATE_PROCEDURE' THEN 'CREAR' WHEN 'ALTER_PROCEDURE' THEN 'MODIFICAR' WHEN 'DROP_PROCEDURE' THEN 'ELIMINAR' ELSE 'DESCONOCIDO' END;
INSERT INTO dbo.ErrorLog (ErrorTime, UserName, ErrorNumber, ErrorSeverity,
-- stored procedure que valida que registros en la tabla prodcutos se han realizado en las ultimas 24 horas
CREATE PROCEDURE GetAllNewRecordsAdventureWorks
AS BEGIN SET NOCOUNT ON;
DECLARE @FromDate DATETIME;
SET @FromDate = DATEADD(HOUR, -24, GETDATE());
SELECT t.name AS TableName, *
FROM sys.tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE s.name = 'dbo' AND EXISTS ( SELECT 1 FROM sys.columns c WHERE c.object_id = t.object_id AND c.system_type_id = 61 -- Datetime2 data type ) AND ( SELECT MAX([ModifiedDate]) FROM [AdventureWorks2019].[dbo].[Producto] ) >= @FromDate END