Share via

Checking for difference in rows.

1023 6 Reputation points
2021-09-22T16:58:25.587+00:00

Hello,
I would like to compare two adjacent rows in each column of a table.
I use temporal table:

ServiceID ServiceName Price CompanyName REV_UserName REV_ReasonChange REV_Date StartTime EndTime
4 service-4 100 company_d John1 Insert data 2021-09-21 2021-09-21 07:04:53.8564368 2021-09-21 07:06:53.8564368
4 service-4 200 company_d John2 Price increase 2021-09-21 2021-09-21 07:06:53.8564368 2021-09-21 08:05:23.8564368
4 service-4 300 company_d John3 Price increase 2021-09-21 2021-09-21 08:05:23.8564368 2021-09-21 09:00:03.8564368
4 service-4 300 company_e John4 Change company 2021-09-21 2021-09-21 09:00:03.8564368 2021-09-21 10:15:03.8564368

I would like to create a storage procedure that when a change is detected in a column (in adjacent rows), inserts the change information into a new temporary table, as below.

I created a procedure that takes a table, filters it and returns the number of rows.

ServiceID Table_Name ChangeColumn OldValue NewValue UserName ReasonChange Date
4 DDServices Price 100 200 John2 Increase price 2021-09-21 07:06:53.8564368
4 DDServices Price 200 300 John3 Increase price 2021-09-21 08:05:23.8564368
4 DDServices CompanyName company_d company_e John4 Change company 2021-09-21 08:05:23.8564368
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[show_history]
--    @DATA_BASE_NAME varchar(50)
      @TABLE_NAME varchar(128) 
    , @ID_COLUMN_NAME varchar(128) 
    , @ID_VALUE varchar(128)    
--  , @DATA_BASE_SCHEMA varchar(50) = 'dbo'
AS
BEGIN

--Table to get result
CREATE TABLE #TEMP_RESULT(
IDColumn varchar(100),
TableName varchar(100),
ChangeColumn varchar(100), 
OldValue varchar(100),
NewValue varchar(100),
UserName varchar(100),
ReasonChange varchar(100),
DataTime datetime
)

--Table get names of column
CREATE TABLE #TEMP_COLUMN_NAME(
columnName varchar(100))

DECLARE @sql_filtered_data NVARCHAR(MAX);
DECLARE @sql_row_count NVARCHAR(MAX);
DECLARE @ROW_COUNT INT;

--Calculation of number of rows in table after filtering
SET @sql_row_count = 'SELECT @ROW_COUNT = COUNT(*) FROM ' + @TABLE_NAME + ' WHERE ' + @ID_COLUMN_NAME + '=''' + @ID_VALUE + '''';
EXEC sp_executesql @sql_row_count, N'@ROW_COUNT INT OUTPUT', @ROW_COUNT OUTPUT;
SELECT @ROW_COUNT

--Get column name and type
insert into #TEMP_COLUMN_NAME SELECT columnName FROM [db].[SfTableColumns] (@TABLE_NAME)
SELECT * from #TEMP_COLUMN_NAME;

--Check data
DECLARE @cnt INT = 0;

WHILE @cnt < @ROW_COUNT
BEGIN
   {...statements...}
   SET @cnt = @cnt + 1;
END;
END
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.