0% found this document useful (0 votes)
9 views2 pages

Trig Proc

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.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
9 views2 pages

Trig Proc

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.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 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

DECLARE @errorNumber INT = ERROR_NUMBER();


DECLARE @errorSeverity INT = ERROR_SEVERITY();
DECLARE @errorState INT = ERROR_STATE();
DECLARE @errorProcedure NVARCHAR(126) = ERROR_PROCEDURE();
DECLARE @errorLine INT = ERROR_LINE();
DECLARE @errorMessage NVARCHAR(400) = ERROR_MESSAGE();
DECLARE @errorTime DATETIME = GETDATE();
DECLARE @userName SYSNAME = SUSER_SNAME();
DECLARE @operationType NVARCHAR(20);

-- 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,


ErrorState, ErrorProcedure, ErrorLine, ErrorMessage)
VALUES (@errorTime, @userName, @errorNumber, @errorSeverity, @errorState,
@errorProcedure, @errorLine, @operationType + ': ' + @errorMessage);
END;

-- 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

You might also like