Clone data from one column to another is a standard procedure in database development. As a basic function, it is essential to maintaining data integrity, allowing for effective analysis, and supporting a range of data management activities. Developers looking to maximize the potential of their databases must comprehend the process of cloning data in SQL tables, whether it’s for computations, backup preservation, performance optimization, or allowing schema changes.
This article will teach you how to use T-SQL and the dbForge Data Compare tool to copy data from one column to another within the same SQL Server table.
Reasons to clone data from one column to another in SQL tables
Data can be copied from one column of a SQL table to another for a number of reasons. Here are some typical situations:
As you can see, in a variety of SQL table scenarios, developers can take advantage of flexibility, performance optimization, data integrity, and historical tracking capabilities by simply copying data from one column to another.
Before you begin
Here are some pointers to help you ensure data consistency, reduce the chance of data loss or integrity problems, and streamline the cloning process before we get started:
- Make a backup of the database or table before attempting any changes. In the event that any unforeseen problems arise during the cloning process, you will know that you have a copy of the original data.
- Verify that the data types in the source and destination columns are compatible. To prevent any data truncation, loss, or type conversion errors, the data types must match or be converted.
- Take into account any constraints—primary key, unique, and non-null—that have been defined for the columns.
- Manage the copying procedure inside a transaction based on the volume and intricacy of the information. In the event that something goes wrong or fails, you can reverse the transaction.
- To modify a SQL table, connect to the SQL Server using a username that has been granted the ALTER permission.
Utilizing T-SQL, duplicate data between columns in a single SQL table
Using a T-SQL query, the UPDATE statement can be used to copy data from one column to another. The statement’s syntax would be as follows:
UPDATE table SET new_column=old_column WHERE conditions;
- table is the name of the table to be updated.
- new_column is the name of the column to which the data will be copied.
- old_column is the name of the column from which the data will be copied.
- conditions are the conditions to be met for the UPDATE statement you want to execute. If no conditions are provided, all records in the table will be updated. This parameter is optional.
We have already prepared the AdventureWorks2019 database’s Orders table for use in our example. In order to see the data in the table, let’s run the SELECT statement.
The data from the TotalAmountOld column will be duplicated and added to a new, empty column called TotalAmountNew. It is significant to remember that, in order to preserve consistency, the data type of the TotalAmountNew and TotalAmountOld columns must match. Use the ALTER TABLE statement to make changes to the table:
ALTER TABLE dbo.Orders ADD TotalAmountNew MONEY;
Using the UPDATE statement, we can now copy the data from one column to another.
UPDATE dbo.Orders SET TotalAmountNew = TotalAmountOld;
Next, run the following SELECT statement to check if the data has been copied:
The TotalAmountNew and TotalAmountOld columns show the same data, as you can see.
Now let’s look at how to use the dbForge Data Compare tool to copy data between columns in the same SQL table. The Column Mapping feature makes it possible to map particular columns—even ones with different names—in order to accomplish this.
Clone data between the columns using dbForge Data Compare
There are two steps involved in using the dbForge tool to clone data between two columns:
- Step 1: Data Comparison.
- Step 2: Data Synchronization.
We have made the Sales table in the AdventureWorks2019 database as a prerequisite for this example:
As you can see, the DiscountNew column has been purposefully left empty because the cloned data will be displayed in it at a later time.
Step 1: Data Comparison.
Click New Data Comparison when dbForge Data Compare opens to get going. Select the same server connection and database as the source and target in the New Data Comparison wizard that appears.
Go to the Mapping page after that. All objects are chosen for data comparison by default. We clear the checkboxes next to the other objects in order to exclude them from the comparison since we only want to compare the columns from the Sales table.
The number of columns for the chosen database table that need to be compared is shown in the Columns in Comparison grid column. To open the Column Mapping dialog and choose which columns to compare, click the More Options menu.
The source columns are displayed on the left and the destination columns are displayed on the right of the Column Mapping dialog.
To proceed, select all columns by clearing the checkboxes beside them, with the exception of SalesID (the primary key) and Discount (the column from which the data will be copied). Perform the following actions on the right side of the Select columns for comparison grid:
- For the empty column – DiscountNew, select None.
- For the source Discount column, select the DiscountNew destination column that will contain the cloned data.
Click OK to save the changes after you’re done. To start the comparison, click the Compare button in the New Data Comparison wizard.
Upon successful comparison of the data sources, the comparison outcomes are displayed in the grid. The Different tab shows the values that differ between the source and destination.
Step 2: Data Synchronization.
We can now execute synchronization to complete the cloning procedure. Click Synchronize data to the target database at the top of the document to accomplish this.
Choose to run the script directly against the target database in the Data Synchronization Wizard that appears, then click Synchronize. Click OK to confirm the action when the pop-up window advising that you are about to modify the database on the designated server appears.
Let’s investigate if any information has been copied from one SQL table column to another. Use the SELECT statement to achieve this.
The values from the Discount column have been copied to the DiscountNew column, as can be seen in the output.
In conclusion, there are various methods for copying data between columns in a single SQL table; the method you choose will depend on your particular requirements. However, we would like to point out that cloning with dbForge Data Compare is easy and doesn’t require a lot of technical expertise. This tool greatly increases the productivity of developers by streamlining and speeding up repetitive tasks.