Audit Logging-Stored Procedure
1. T_ORDER
For Insert: sp_order_i
For Update: sp_order_u
For Delete: sp_order_d
2. T_ORDER_DETAIL
FOR Insert: sp_order_detail_i
For Update: sp_order_detail_u
For Delete: sp_order_detail_d
For Insert: sp_order_i
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_order_i')
BEGIN
DROP Procedure sp_order_i
END
GO
CREATE Procedure sp_order_i
(
@p_order_id INT OUTPUT,
@p_order_date DATETIME,
@p_supplier VARCHAR(50),
@p_created_by VARCHAR(50),
@p_created_on DATETIME,
@p_last_updated_by VARCHAR(50),
@p_last_updated_on DATETIME,
@p_transaction_no CHAR(36)
)
AS
INSERT [dbo].[T_ORDER]
([ORDER_DATE]
,[SUPPLIER]
,[CREATED_BY]
,[CREATED_ON]
,[LAST_UPDATED_BY]
,[LAST_UPDATED_ON]
,[TRANSACTION_NO])
VALUES
(@p_order_date ,
@p_supplier ,
@p_created_by ,
@p_created_on ,
@p_last_updated_by ,
@p_last_updated_on ,
@p_transaction_no)
SET @p_order_id = SCOPE_IDENTITY()
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
GO
BEGIN
DROP Procedure sp_order_i
END
GO
CREATE Procedure sp_order_i
(
@p_order_id INT OUTPUT,
@p_order_date DATETIME,
@p_supplier VARCHAR(50),
@p_created_by VARCHAR(50),
@p_created_on DATETIME,
@p_last_updated_by VARCHAR(50),
@p_last_updated_on DATETIME,
@p_transaction_no CHAR(36)
)
AS
INSERT [dbo].[T_ORDER]
([ORDER_DATE]
,[SUPPLIER]
,[CREATED_BY]
,[CREATED_ON]
,[LAST_UPDATED_BY]
,[LAST_UPDATED_ON]
,[TRANSACTION_NO])
VALUES
(@p_order_date ,
@p_supplier ,
@p_created_by ,
@p_created_on ,
@p_last_updated_by ,
@p_last_updated_on ,
@p_transaction_no)
SET @p_order_id = SCOPE_IDENTITY()
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
GO
For Update: sp_order_u
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_order_u')
BEGIN
DROP Procedure sp_order_u
END
GO
CREATE Procedure sp_order_u
(
@o_order_id INT,
@p_order_date DATETIME,
@p_supplier VARCHAR(50),
@p_last_updated_by VARCHAR(50),
@p_last_updated_on DATETIME,
@p_transaction_no CHAR(36),
@o_version_no TIMESTAMP
)
AS
UPDATE [dbo].[T_ORDER]
SET [ORDER_DATE] = @p_order_date
,[SUPPLIER] = @p_supplier
,[LAST_UPDATED_BY] = @p_last_updated_by
,[LAST_UPDATED_ON] = @p_last_updated_on
,[TRANSACTION_NO] = @p_transaction_no
WHERE ORDER_ID= @o_order_id
AND VERSION_NO = @o_version_no
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
GO
BEGIN
DROP Procedure sp_order_u
END
GO
CREATE Procedure sp_order_u
(
@o_order_id INT,
@p_order_date DATETIME,
@p_supplier VARCHAR(50),
@p_last_updated_by VARCHAR(50),
@p_last_updated_on DATETIME,
@p_transaction_no CHAR(36),
@o_version_no TIMESTAMP
)
AS
UPDATE [dbo].[T_ORDER]
SET [ORDER_DATE] = @p_order_date
,[SUPPLIER] = @p_supplier
,[LAST_UPDATED_BY] = @p_last_updated_by
,[LAST_UPDATED_ON] = @p_last_updated_on
,[TRANSACTION_NO] = @p_transaction_no
WHERE ORDER_ID= @o_order_id
AND VERSION_NO = @o_version_no
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
GO
For Delete: sp_order_d
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_order_d')
BEGIN
DROP Procedure sp_order_d
END
GO
CREATE Procedure sp_order_d
(
@o_order_id INT,
@p_transaction_no CHAR(36),
@o_version_no TIMESTAMP
)
AS
UPDATE dbo.T_ORDER
SET TRANSACTION_NO = @p_transaction_no ,
NEED_AUDIT = 0
WHERE ORDER_ID = @o_order_id
AND VERSION_NO= @o_version_no
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
DELETE dbo.T_ORDER
WHERE ORDER_ID = @o_order_id
GO
BEGIN
DROP Procedure sp_order_d
END
GO
CREATE Procedure sp_order_d
(
@o_order_id INT,
@p_transaction_no CHAR(36),
@o_version_no TIMESTAMP
)
AS
UPDATE dbo.T_ORDER
SET TRANSACTION_NO = @p_transaction_no ,
NEED_AUDIT = 0
WHERE ORDER_ID = @o_order_id
AND VERSION_NO= @o_version_no
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
DELETE dbo.T_ORDER
WHERE ORDER_ID = @o_order_id
GO
2. T_ORDER_DETAIL
FOR Insert: sp_order_detail_i
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_order_detail_i')
BEGIN
DROP Procedure sp_order_detail_i
END
GO
CREATE Procedure sp_order_detail_i
(
@p_order_id INT,
@p_product_id INT,
@p_product_name VARCHAR(50),
@p_unit_price MONEY,
@p_quantity INT,
@p_created_by VARCHAR(50),
@p_created_on DATETIME,
@p_last_updated_by VARCHAR(50),
@p_last_updated_on DATETIME,
@p_transaction_no CHAR(36)
)
AS
INSERT INTO [dbo].[T_ORDER_DETAIL]
([ORDER_ID]
,[PRODUCT_ID]
,[PRODUCT_NAME]
,[UNIT_PRICE]
,[QUANTITY]
,[CREATED_BY]
,[CREATED_ON]
,[LAST_UPDATED_BY]
,[LAST_UPDATED_ON]
,[TRANSACTION_NO])
VALUES
(@p_order_id,
@p_product_id,
@p_product_name,
@p_unit_price,
@p_quantity,
@p_created_by ,
@p_created_on ,
@p_last_updated_by ,
@p_last_updated_on ,
@p_transaction_no)
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
GO
BEGIN
DROP Procedure sp_order_detail_i
END
GO
CREATE Procedure sp_order_detail_i
(
@p_order_id INT,
@p_product_id INT,
@p_product_name VARCHAR(50),
@p_unit_price MONEY,
@p_quantity INT,
@p_created_by VARCHAR(50),
@p_created_on DATETIME,
@p_last_updated_by VARCHAR(50),
@p_last_updated_on DATETIME,
@p_transaction_no CHAR(36)
)
AS
INSERT INTO [dbo].[T_ORDER_DETAIL]
([ORDER_ID]
,[PRODUCT_ID]
,[PRODUCT_NAME]
,[UNIT_PRICE]
,[QUANTITY]
,[CREATED_BY]
,[CREATED_ON]
,[LAST_UPDATED_BY]
,[LAST_UPDATED_ON]
,[TRANSACTION_NO])
VALUES
(@p_order_id,
@p_product_id,
@p_product_name,
@p_unit_price,
@p_quantity,
@p_created_by ,
@p_created_on ,
@p_last_updated_by ,
@p_last_updated_on ,
@p_transaction_no)
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
GO
For Update: sp_order_detail_u
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_order_detail_u')
BEGIN
DROP Procedure sp_order_detail_u
END
GO
CREATE Procedure sp_order_detail_u
(
@o_order_id INT,
@o_product_id INT,
@p_product_name VARCHAR(50),
@p_unit_price MONEY,
@p_quantity INT,
@p_last_updated_by VARCHAR(50),
@p_last_updated_on DATETIME,
@p_transaction_no CHAR(36),
@o_version_no TIMESTAMP
)
AS
UPDATE [dbo].[T_ORDER_DETAIL]
SET [PRODUCT_NAME] =@p_product_name
,[UNIT_PRICE] = @p_unit_price
,[QUANTITY] = @p_quantity
,[LAST_UPDATED_BY] = @p_last_updated_by
,[LAST_UPDATED_ON] = @p_last_updated_on
,[TRANSACTION_NO] = @p_transaction_no
WHERE ORDER_ID = @o_order_id
AND PRODUCT_ID = @o_product_id
AND VERSION_NO = @o_version_no
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
GO
BEGIN
DROP Procedure sp_order_detail_u
END
GO
CREATE Procedure sp_order_detail_u
(
@o_order_id INT,
@o_product_id INT,
@p_product_name VARCHAR(50),
@p_unit_price MONEY,
@p_quantity INT,
@p_last_updated_by VARCHAR(50),
@p_last_updated_on DATETIME,
@p_transaction_no CHAR(36),
@o_version_no TIMESTAMP
)
AS
UPDATE [dbo].[T_ORDER_DETAIL]
SET [PRODUCT_NAME] =@p_product_name
,[UNIT_PRICE] = @p_unit_price
,[QUANTITY] = @p_quantity
,[LAST_UPDATED_BY] = @p_last_updated_by
,[LAST_UPDATED_ON] = @p_last_updated_on
,[TRANSACTION_NO] = @p_transaction_no
WHERE ORDER_ID = @o_order_id
AND PRODUCT_ID = @o_product_id
AND VERSION_NO = @o_version_no
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
GO
For Delete: sp_order_detail_d
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_order_detail_d')
BEGIN
DROP Procedure sp_order_detail_d
END
GO
CREATE Procedure sp_order_detail_d
(
@o_order_id INT,
@o_product_id INT,
@p_transaction_no CHAR(36),
@o_version_no TIMESTAMP
)
AS
UPDATE [dbo].[T_ORDER_DETAIL]
SET TRANSACTION_NO = @p_transaction_no,
NEED_AUDIT=0
WHERE ORDER_ID = @o_order_id
AND PRODUCT_ID = @o_product_id
AND VERSION_NO = @o_version_no
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
DELETE FROM [dbo].[T_ORDER_DETAIL]
WHERE ORDER_ID = @o_order_id
AND PRODUCT_ID = @o_product_id
GO
BEGIN
DROP Procedure sp_order_detail_d
END
GO
CREATE Procedure sp_order_detail_d
(
@o_order_id INT,
@o_product_id INT,
@p_transaction_no CHAR(36),
@o_version_no TIMESTAMP
)
AS
UPDATE [dbo].[T_ORDER_DETAIL]
SET TRANSACTION_NO = @p_transaction_no,
NEED_AUDIT=0
WHERE ORDER_ID = @o_order_id
AND PRODUCT_ID = @o_product_id
AND VERSION_NO = @o_version_no
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Version conflicts!',10,1)
END
DELETE FROM [dbo].[T_ORDER_DETAIL]
WHERE ORDER_ID = @o_order_id
AND PRODUCT_ID = @o_product_id
GO