---
title: "Store the previous record in its history table before updating it in the SQL table using a trigger."  
description: "Store the previous record in its history table before updating it in the SQL table using a trigger."  
author: "Ashutosh Patel"  
published: 2023-03-22  
updated: 2023-04-14  
canonical: https://www.mindstick.com/forum/157521/store-the-previous-record-in-its-history-table-before-updating-it-in-the-sql-table-using-a-trigger  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# Store the previous record in its history table before updating it in the SQL table using a trigger.

[Store](https://www.mindstick.com/articles/13125/tips-to-increase-sales-of-your-woocommerce-store) the [previous record](https://answers.mindstick.com/qa/38785/name-the-18-year-old-javelin-thrower-who-set-a-new-world-record-for-junior-atheletes-by-threwing-a-spear-a-distance-of-81-04m-distance-breaking-previous-record-of-79-20m) in its [history](https://yourviews.mindstick.com/view/81373/indian-railways-create-history-with-it-s-shramik-special-trains) table before updating it in the [SQL table](https://www.mindstick.com/forum/160519/how-can-i-update-sql-table-by-dataset-data-in-ado-dot-net) using a trigger.

## Replies

### Reply by Krishnapriya Rajeev

To store the [previous](https://yourviews.mindstick.com/view/81421/unlock-2-0-is-just-like-previous-corona-lockdowns) record in its history [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) before updating it in an [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) table using a trigger, you can create a trigger that executes before the update statement. The trigger will need to insert a copy of the current record into the history table before the update is executed.

Here is an example of how you can create a trigger in SQL to achieve this:

```plaintext
CREATE TRIGGER update_record_trigger
BEFORE UPDATE ON my_table
FOR EACH ROW
BEGIN
  INSERT INTO my_table_history (column1, column2, column3, ...)
  VALUES (OLD.column1, OLD.column2, OLD.column3, ...);
END;
```

In this example, *my_table* is the name of the table you want to track changes for, and *my_table_history* is the name of the table you want to store previous versions of the record in. The OLD keyword refers to the current version of the record being updated and allows you to reference the values of its columns.

With this trigger in place, whenever an update is executed on *my_table*, the trigger will automatically insert a copy of the current record into the *my_table_history* table before the update is executed. This allows you to maintain a history of changes to the table over time.


---

Original Source: https://www.mindstick.com/forum/157521/store-the-previous-record-in-its-history-table-before-updating-it-in-the-sql-table-using-a-trigger

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
