Generating Keys For Data From One Database To Another
Generating Keys For Data From One Database To Another 4,2/5 5286 reviews

Mar 06, 2007 SQL Server Data Import – How To Preserve Primary Keys and Identity Fields Posted on 3/6/2007 by Robert S. Robbins One of my biggest problems working with SQL Server databases is preserving my primary keys and identity fields when I try to copy tables from one database to another. The first method includes usage of SELECT INTO command for copying table from one database to another. The following syntax will be used: SELECT. INTO DestinationDB.dbo.tablename from SourceDB.dbo.SourceTable. The statement will create the tables in the destination database first and then copy the data to these tables.

  • In this article, we use a source database containing a table with data and create another database which contains the copy of the table with data from the source database. So let's take a look at a practical example of how to copy a table with data from one database to another in SQL Server.
  • Apr 20, 2006 For example, if the primary key is 6 columns wide and I need to refer to a row from another table, it’s impractical to make a 6-column wide foreign key. For this reason, database designers sometimes introduce a surrogate key, which uniquely identifies every row in the table and is “more minimal” than the inherently unique aspect of the data.
By: Greg Robidoux Updated: 2006-09-26 Comments (4) Related: 1 2 3 4 More >Identities

Problem
One thing that DBAs are often faced with is moving data from one database to another database to populate lookup tables or some other key table in your database. This may be to keep a test or development environment in synch or maybe there is a need to populate like databases on other servers with the same data values.

Another thing that is often common with SQL Server is the use of identity values or auto incrementing of a key value for new records as they get inserted. Using identity values is a simple way to make sure you have a unique primary key for new records, but there is no simple way to control what identity value will be given for a certain row in a table.

One

When you combine these two items together, having an identity column as a primary key and having the need to push like data to other databases this is where the problems begin. If your tables on these different databases are setup exactly the same way where both tables have an identity value there is no way to control what identity value one table will get vs the other table and therefore you can have issues where the data is not in synch.

Solution
To handle this situation where you need to make sure the data on your two databases stays in synch along with having an identity value for the key, SQL Server has a command SET IDENTITY_INSERTthat allows you to turn on and off the auto generation of the identity value as needed. The command gets turned on prior to the inserting of data and gets turned off after the data has been inserted.

Generating keys for data from one database to another oracle

Here is a simple command that allows you to insert into a table that has an identity value field, but allows you to specify what the value will be instead of having SQL Server pick the next sequential value.

This first query is our regular insert into our primary database. At this point we don't need to specify the ClientID, because this value is auto generated from our identity value on the ClientID column.

INSERT INTO dbo.CLIENT (ClientName) VALUES ('Edgewood Solutions')

INSERT INTO dbo.CLIENT (ClientName) VALUES ('Microsoft')

After we insert the data we have two new records in our Client table.

ClientIDClientName
782Edgewood Solutions
783Microsoft

To move this data to our other databases that have the same table and need to retain the same identity values we issue the command below. First we turn on the identity insert, insert the two records this time including the ClientID value and then we turn off the identify insert to make sure that if any future records get added the clientID column is auto generated.

SET IDENTITY_INSERT dbo.Client ON

INSERT INTO dbo.CLIENT (ClientID, ClientName) VALUES (782, 'Edgewood Solutions')

INSERT INTO dbo.CLIENT (ClientID, ClientName) VALUES (783, 'Microsoft')

SET IDENTITY_INSERT dbo.Client OFF

By using the IDENTITY_INSERT command we are able to make sure that the records that get inserted on our other databases retain the same ClientID value, this way there are no data integrity issues from one database to another.

Next Steps

  • Next time you need to populate like tables on different databases that have identity values look at using this command to make the maintenance of these tables easier

Last Updated: 2006-09-26



About the author
Greg Robidoux is the President of Edgewood Solutions and a co-founder of MSSQLTips.com.
View all my tips
Related Resources


-->

You can compare the data that is contained in two databases. The databases that you compare are known as the source and the target.

Note

Database projects and .dacpac or .bacpac packages cannot be the source or target in a data comparison.

As the data is compared, a Data Manipulation Language (DML) script is generated, which you can use to synchronize the differing databases by updating some or all of the data on the target database. When the data comparison finishes, its results appear in the Data Compare window of Visual Studio.

After the comparison finishes, you can take other steps:

  • You can view the differences between the two databases. For more information, see Viewing Data Differences.

  • You can update all or part of the target to match the source. For more information, see Synchronizing Database Data.

For more information, see Compare and Synchronize Data in One or More Tables with Data in a Reference Database.

Note

You can also compare the schema of two databases or of two versions of the same database. For more information, see How to: Use Schema Compare to Compare Different Database Definitions.

Comparing Database Data

To compare data by using the New Data Comparison Wizard

  1. On the SQL menu, point to Data Compare, and then click New Data Comparison.

    The New Data Comparison wizard appears. Also, the Data Compare window opens, and Visual Studio automatically assigns it a name such as DataCompare1.

  2. Identify the source and target databases.

    If the Source Database list or the Target Database list is empty, click New Connection. In the Connection Properties dialog box, identify the server on which the database resides and the type of authentication to use when connecting to the database. Then, click OK to close the Connection Properties dialog box and return to the Data Compare wizard.

    On the first page of the Data Compare wizard, verify that the information for each database is correct, specify which records you want to include in the results, and then click Next. The second page of the Data Compare wizard appears and shows a hierarchical listing of the tables and views in the database.

  3. Select the check boxes for the tables and views that you want to compare. Optionally, expand the nodes for database objects, and then select the check boxes for columns within those objects that you want to compare.

    Note

    Tables and views must meet two criteria to appear in the listing. First, the schemas of the objects must match between the source and target databases. Second, only tables and views that have a primary key, a unique key, a unique index, or a unique constraint appear in the list. If no tables or views meet both criteria, the list will be empty.

  4. If more than one key is present, you can use the Comparison Key column to specify the key on which to base the data comparison. For example, you can specify whether to base the comparison on the primary key column or on another (uniquely identifiable) key column.

  5. Click Finish.

    The comparison starts.

    Note

    You can stop a data comparison operation that is in progress by opening the SQL menu, clicking Data Compare, and then clicking Stop Data Comparison.

    When the comparison is finished, you can view the data differences between the two databases. You can also update part or all the data in the target database to match the data in the source database.

To compare data by using the Visual Studio automation model

  1. Open the View menu, point to Other Windows, and click Command Window.

  2. In the Command Window, type the following command:

    Replace the placeholders (sServerName, sDatabaseName, sUserName, sPassword, sDisplayName, tServerName, tDatabaseName, tUserName, tPassword, and tDisplayName) with the values for your source and target databases.

    If you do not specify a source and a target, the New Data Comparison dialog box appears. For more information about the parameters for the Sql.NewDataComparison command, see Automation Command Reference for Database Features of Visual Studio Team System.

    The data in the specified source and target databases are compared. The results appear in a Data Compare session. For more information about how to view results or synchronize the data, see Viewing Data Differences and Synchronizing Database Data.

Viewing Data Differences

After you compare the data in two databases, Data Compare lists each database object that you compared and its status. You can also view results for the records within each object, grouped by status. For more information about the status designations, see Compare and Synchronize Data in One or More Tables with Data in a Reference Database.

After you view the differences, you can update the target to match the source for some or all of the objects or records that are different, missing, or new. For more information, see Synchronizing Database Data.

Generating Keys For Data From One Database To Another Using Ssms

To view data differences

Generating Keys For Data From One Database To Another Computer

  1. Compare the data in a source and a target database. For more information, see Compare Database Data.

  2. (Optional) Do one or both of the following:

    • By default, the results for all objects appear, regardless of their status. To display only those objects that have a particular status, click an option in the Filter list.

    • To view results for records within a particular object, click the object in the main results pane, and then click a tab in the records view pane. Each tab displays all records within that object that have a particular status: different, only in source, only in target, and identical. Data appears by record and column.

Generating Keys For Data From One Database To Another Oracle

Synchronizing Database Data

After you compare the data in two databases, you can synchronize them by updating all or part of the target to match the source. You can compare the data in two kinds of database objects: tables and views.

To update target data by using the Write Updates command

  1. Compare the data in a source and a target database. For more information, see Compare Database Data.

    After the comparison finishes, the Data Compare window lists results for the objects that were compared. Four columns (named Different Records, Only in Source, Only in Target, and Identical Records) display information about objects that were not identical. For each such object, these columns display how many records were found to be different, and how many records an update operation would change. Those two numbers match at first, but in step 4 you can change which objects to update.

    For more information, see Compare and Synchronize Data in One or More Tables with Data in a Reference Database.

  2. In the table of the Data Compare window, click a row.

    The details pane shows results for the records in the database object that you clicked. Records are grouped by status onto tabs, which you can use to specify the data that will be propagated from the source to the target.

  3. In the details pane, click a tab whose name contains a number other than zero (0).

    The Update column of the Only in Target table contains check boxes that you can use to select rows to be updated. By default, each check box is selected.

  4. Clear check boxes for records in the target that you do not want to update with data from the source.

    When you clear a check box, you reduce the number of records to update, and the display changes to reflect your actions. This number appears in the status line of the details pane and in the corresponding column in the main results pane, as described in step 1.

  5. (Optional) Click Generate Script.

    A Transact-SQL editor window opens and shows the Data Manipulation Language (DML) script that would be used to update the target.

  6. To synchronize records that are different, missing, or new, click Update Target.

    Note

    While the target database is being updated, you can cancel the operation by clicking Stop Writing to Target.

    The data of the selected records in the target is updated with the data from the corresponding records in the source.

    Note

    If you opt to update indexed views, the Update Target operation might fail if this action causes duplicate keys to be inserted into the same table.

To update target data by using a Transact-SQL script

Generating Keys For Data From One Database To Another Sql

  1. Compare the data in a source and a target database. For more information, see Compare Database Data.

    After the comparison finishes, the Data Compare window lists the objects that were compared. For more information, see Compare and Synchronize Data in One or More Tables with Data in a Reference Database.

  2. (Optional) In the details pane, clear the check boxes for records in the target that you do not want to update, as described in the previous procedure.

  3. Click Generate Script.

    A new window shows the Transact-SQL script that would propagate the changes necessary to make the data in the target match the data in the source. Game dev tycoon no download. The new window is given a name such as DataUpdate_Database_1.sql.

    This script reflects changes that you have made in the details pane. For example, you might have cleared a check box for a given row in the Only in Target page for the [dbo].[Shippers] table. In that case, the script would not update that row.

  4. (Optional) Edit this script in the DataUpdate_Database_1.sql window.

  5. (Optional but recommended) Back up the target database.

  6. Click Execute to update the target database.

    Specify a connection to the target database that you want to update.

    Important

    By default, the updates occur within the scope of a transaction. If errors occur, you can roll back the whole update. You can change this behavior.

    The data of the selected records in the target is updated with the data from the corresponding records in the source.

See Also