When you rename a SQL table, most comparison tools consider this as the deletion or recreation of a new table. In this case, the deployment will lead to data loss of the renamed table.
Solution: To avoid data loss when renaming a table, you should create a migration script to rename the table using the sp_rename
stored procedure.
Note
The comparison engine of the tools doesn’t handle all cases automatically, which is why the cases mentioned above must be resolved manually.
The screenshot displays the Schema Comparison document and the scripts generated by Schema Compare Engine and a migration script, which should be created manually to perform errorless data migration.
The table provides source and target table script examples to illustrate the case:
Source table script example | Target table script example |
---|---|
CREATE TABLE [dbo].[StoreProduct] ( [ProductNumber] [INT] PRIMARY KEY, [ProductPrice] [DECIMAL](5, 2), [ProductName] [NVARCHAR](25) NULL ) GO |
CREATE TABLE [dbo].[Product] ( [ProductNumber] [INT] PRIMARY KEY, [ProductPrice] [DECIMAL](5, 2), [ProductName] [NVARCHAR](25) NULL ) GO INSERT INTO [dbo].[Product] ([ProductNumber], [ProductPrice], [ProductName]) VALUES (1125, 234.56, 'screwriver'); INSERT INTO [dbo].[Product] ([ProductNumber], [ProductPrice], [ProductName]) VALUES (1126, 89.71, 'hummer'); GO |
The table provides examples of scripts generated by the Schema Comparison engine and those that should be generated manually for errorless data migration to illustrate the case:
Script generated by the Schema Comparison engine | Script that should be generated manually for errorless data migration |
---|---|
DROP TABLE [dbo].[Product] GO CREATE TABLE [dbo].[StoreProduct] ( [ProductNumber] [INT] NOT NULL, [ProductPrice] [DECIMAL](5, 2) NULL, [ProductName] [NVARCHAR](25) NULL, PRIMARY KEY CLUSTERED ([ProductNumber]) ) GO |
-- migration script: EXEC sys.sp_rename @objname = 'Product', @newname = 'StoreProduct' -- migration script. |