What I am trying to do is find out which fields were updated and record the change to a different table.
DECLARE
@BillNo int,
@column_name varchar(500)
SELECT @BillNo = BillNo FROM INSERTED
DECLARE HistoryMonitorLoop CURSOR FOR
SELECT
column_name
FROM
information_schema.columns
WHERE
table_name = 'Shipment';
OPEN HistoryMonitorLoop
FETCH next FROM HistoryMonitorLoop INTO @column_name
WHILE @@Fetch_status = 0
BEGIN
DECLARE
@OldValue varchar(500),
@NewValue varchar(500)
SET @OldValue = (SELECT @column_name FROM Deleted);
SET @NewValue = (SELECT @column_name FROM Inserted);
IF(@OldValue != @NewValue)
BEGIN
DECLARE @Comment varchar(5000)
SELECT @Comment = @column_name + ' Changed from ' + @OldValue + ' to ' + @NewValue
EXEC ShipmentNote_Insert @BillNo=@BillNo,@CoordinatorID=1,@Comment=@Comment
END
FETCH next FROM HistoryMonitorLoop INTO @column_name
END
CLOSE HistoryMonitorLoop
DEALLOCATE HistoryMonitorLoop
what is happening is the
SET @OldValue = (SELECT @column_name FROM Deleted);
SET @NewValue = (SELECT @column_name FROM Inserted);
are setting the @OldValue and @NewValue = to the columnname instead of the value of the column – sql is processing it as SET @OldValue = (SELECT 'column_name' FROM Deleted);