Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Tracking changes

Author  Topic 

Bex
Aged Yak Warrior

580 Posts

Posted - 2010-10-20 : 06:35:06
I have the following:

create database [Online]
go
use [Online]
go
create table OrderHeader
(
OrderID INT PRIMARY KEY
, OrderDate DATETIME
, OrderAmount MONEY
, BillingAddressID INT
, OrderStatus TINYINT
)
GO

CREATE TABLE OrderDelta
(
OrderID INT
, TableName VARCHAR(100)
, ColumnName VARCHAR(100)
, BeforeValue VARCHAR(100)
, AfterValue VARCHAR(100)
, DateTimeStamp DATETIME
)
GO

INSERT INTO OrderHeader
VALUES (1,'20101019 00:00:00.000', 23.98,123,1)

CREATE TABLE #MyTempTable
( OrderID INT PRIMARY KEY
, [Action] VARCHAR(20)
, BeforeOrderDate DATETIME
, BeforeOrderAmount MONEY
, BeforeBillingAddressID INT
, BeforeOrderStatus TINYINT
, AfterOrderDate DATETIME
, AfterOrderAmount MONEY
, AfterBillingAddressID INT
, AfterOrderStatus TINYINT
);

GO

CREATE PROCEDURE InsertOrderHeader
(
@OrderID INT
, @OrderDate DATETIME
, @OrderAmount MONEY
, @BillingAddressID INT
, @OrderStatus TINYINT
)
AS
SET NOCOUNT ON

BEGIN
MERGE OrderHeader AS target
USING (SELECT @OrderID, @OrderDate, @OrderAmount, @BillingAddressID, @OrderStatus) AS source (OrderID, OrderDate, OrderAmount, BillingAddressID, OrderStatus)
ON (target.OrderID = source.OrderID)
WHEN MATCHED THEN
UPDATE SET OrderDate = source.OrderDate, OrderAmount = source.OrderAmount, BillingAddressID = source.BillingAddressID, OrderStatus = source.OrderStatus
WHEN NOT MATCHED THEN
INSERT (OrderID, OrderDate, OrderAmount, BillingAddressID, OrderStatus)
VALUES (source.OrderID, source.OrderDate, source.OrderAmount, source.BillingAddressID, source.OrderStatus)
OUTPUT deleted.OrderID, $action, deleted.OrderDate, deleted.OrderAmount, deleted.BillingAddressID, deleted.OrderStatus,
inserted.OrderDate, inserted.OrderAmount, inserted.BillingAddressID, inserted.OrderStatus; --INTO #MyTempTable;
END
GO

INSERT INTO #MyTempTable
EXEC InsertOrderHeader @OrderID = 1, @OrderDate = '20101019 00:00:00.000', @OrderAmount = 20.00, @BillingAddressID = 123, @OrderStatus = 1

SELECT * FROM #MyTempTable


I want the result to look like:

OrderID TableName ColumnName BeforeValue AfterValue DateTimeStamp
1 OrderHeader OrderAmount 23.98 20.00 '2010-10-20 11:33:.348

And multiple rows if there were more than 1 column change.

Thanks

Hearty head pats

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-20 : 13:23:23
for that you might need to put OUTPUT values into a temporary table and use like:-

SELECT OrderID,'Column1Name',Column1OldValue,column1NewValue
FROM TempTable
WHERE Column1OldValue <> column1NewValue
UNION ALL
SELECT OrderID,'Column2Name',Column2OldValue,column2NewValue
FROM TempTable
WHERE Column2OldValue <> column2NewValue
....


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2010-10-21 : 02:48:51
Thanks for you help

That looks cleaner than my current method.

Hearty head pats
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-23 : 02:31:51
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -