Hi,
I have two tables
Table-Amust contains current data rows.Table-A-History mustcontains historical data
whenever a new data row is available, update Table-A's row and insert a new row into Table-A-History.
This needs to be achieve without using trigger on table,
but may be with help of stored proc
Could anyone of you can help here

Sachin SinghRank #66 · 26.2K reputationPosted Feb 4, 2022, 10:37 AM EST
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 3, 2022, 11:15 AM EST
You can achieve through store procedures. On add new record into Table-A then same time add data into Table-A History. On updating Table-A, to maintain history insert previous or new entry into Table-A History. I have create two store procedure for this:
Add New Record:
CREATE PROCEDURE procAddTableA
@ID BIGINT,
@FName nvarchar(50),
@LName nvarchar(50),
@Address nvarchar(50)
AS
BEGIN
INSERT INTO [dbo].[TableA]([ID],[FName],[LName],[Address]) VALUES(@ID, @FName, @LName, @Address)
INSERT INTO [dbo].[TableAHistory]([ID],[FName],[LName],[Address]) VALUES(@ID, @FName, @LName, @Address)
END
GO
Update Record:
ALTER PROCEDURE [dbo].[procUpdateTableA]
@ID BIGINT,
@FName nvarchar(50),
@LName nvarchar(50),
@Address nvarchar(50)
AS
BEGIN
-- INSERT History Before Update the with Current
INSERT INTO TableAHistory
SELECT * FROM TableA WHERE ID = @ID
UPDATE [dbo].[TableA]
SET [FName] = @FName,[LName] = @LName,[Address] = @Address
WHERE ID = @ID
-- INSERT History With New record After Update the
--INSERT INTO [dbo].[TableAHistory]([ID],[FName],[LName],[Address]) VALUES(@ID, @FName, @LName, @Address)
END
You can change table structure accordingly.
jayu p111 reputationPosted Feb 4, 2022, 7:37 AM EST
Thank you Muhammad Imran Ansari,
but I do not know which @Id will get updated as the source t fill thiese table is different
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 4, 2022, 7:47 AM EST
jayu p111 reputationPosted Feb 4, 2022, 8:14 AM EST
Muhammad Imran Ansari
Source table
target table
target history table
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 4, 2022, 8:35 AM EST
jayu p111 reputationPosted Feb 4, 2022, 8:40 AM EST
Muhammad Imran Ansari
yes, but everytime I do not have to insert all rows but just the affected row
e.g row 1 has changed 5 times today so to insert previous 4 rows into history and the latest will be in main table.
currently to update and insert into target table ,I am using merge logic
I have to achieve this how it will work when we write trigger on insert and update
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 4, 2022, 9:09 AM EST
I have modied the storeprocedures with uniqueidentifier to achieve this functionality, hope this will work for you:
Insert Record:
ALTER PROCEDURE [dbo].[procAddTableA]
@FName nvarchar(50),
@LName nvarchar(50),
@Address nvarchar(50)
AS
BEGIN
DECLARE @GuidID AS uniqueidentifier = NEWID()
INSERT INTO [dbo].[TableA]([GuidID],[FName],[LName],[Address], [RecordDate]) VALUES(@GuidID, @FName, @LName, @Address,GETDATE())
INSERT INTO [dbo].[TableAHistory]([GuidID],[FName],[LName],[Address], [RecordDate]) VALUES(@GuidID, @FName, @LName, @Address,GETDATE())
END
Update Record:
ALTER PROCEDURE [dbo].[procUpdateTableA]
@GUIDID UNIQUEIDENTIFIER,
@FName nvarchar(50),
@LName nvarchar(50),
@Address nvarchar(50)
AS
BEGIN
INSERT INTO TableAHistory
SELECT * FROM TableA WHERE GuidID = @GUIDID
UPDATE [dbo].[TableA]
SET [FName] = @FName,[LName] = @LName,[Address] = @Address, ModifiedDate = GETDATE()
WHERE GuidID = @GUIDID
-- INSERT a New Rown in History
--INSERT INTO [dbo].[TableAHistory]([ID],[FName],[LName],[Address]) VALUES(@ID, @FName, @LName, @Address)
END
Result:
Insert Transaction-1:
[dbo].[procAddTableA] 'Kerry', 'William', 'NY'
Update Transaction-2:
[dbo].[procUpdateTableA] '92505FE9-B574-4ECA-861C-6F2F5968F5A3', 'John', 'William', 'NY'
Update Transaction-3:
[dbo].[procUpdateTableA] '92505FE9-B574-4ECA-861C-6F2F5968F5A3', 'John', 'Velley', 'NY'
Gajendra JangidRank #157 · 14.9K reputationPosted Mar 12, 2022, 5:07 AM EST