Editing versioned data in SQL Server using multiversioned views and SQL
You can edit versioned attribute data in a geodatabase in Microsoft SQL Server by editing a multiversioned view created on the data. Beginning with ArcGIS 10 SP2, there are two ways you can do this: editing a named version or editing the DEFAULT version. See A quick tour of editing versioned data using SQL for a description of each editing model.
There are several stored procedures installed with ArcSDE that help you work with multiversioned views to edit a named version (versions other than DEFAULT). These components and their descriptions are as follows:
Stored procedure |
Purpose |
Syntax and description |
---|---|---|
create_version |
Creates a named geodatabase version. If you are editing named versions, always edit your own, separate version; multiple editors cannot edit the same version using multiversioned views. Do not use this procedure if you are editing the DEFAULT version. |
EXEC {dbo | sde}.create_version '<parent_version>', '<child_version>', <name_rule>, <access>, '<description>'
|
set_current_version |
Set which geodatabase version and state you will access. You access the state the version was pointing to when you called setcurrentversion. Do not use this procedure if you are editing the DEFAULT version. |
EXEC {dbo | sde}.set_current_version '<version_name>'
|
edit_version |
Start and end an edit session on a named version. Edits in the transaction are committed whenever this procedure is called. |
EXEC {dbo | sde}.edit_version '<version_name>',{1 | 2} Specify 1 to start the edit session. Specify 2 to end it. |
delete_version |
Delete a named geodatabase version. |
EXEC {dbo | sde}.delete_version '<version_name>' |
When you edit versioned tables through multiversioned views, changes are made to the delta tables, and row ID (ObjectID) values for new records are automatically generated. However, unlike editing versioned data in an ArcGIS edit session, no internal version reconciliation is done with these edits.
- Version management operations, such as reconciling, resolving conflicts, and posting, must be done using ArcGIS software.
- Never use database management system (DBMS) tools to update any row ID (ObjectID) field maintained by ArcGIS in the database. These ObjectID fields are allocated and managed by the geodatabase and, therefore, should not be altered using SQL.
If you want to edit the DEFAULT version, create a multiversioned view, then edit that view using SQL. Do not set the version or start an edit session to edit the DEFAULT version. Be sure to commit changes to the database after each edit; exclusive locks are held on the delta tables until you commit, thereby closing the transaction.
The following steps guide you through editing a named geodatabase version. In the examples, the multiversioned view created to perform the edits on the table is code_mv, and the version created and used for editing is mvedits.
Remember that the dataset you edit through a multiversioned view must already have been registered as versioned and you must have privileges in the database sufficient to edit the dataset and view.
-
At an MS-DOS command prompt, execute the sdetable utility to create a multiversioned view.
sdetable –o create_mv_view –T code_mv –t code_enf –i sde:sqlserver:myserver\ssinst –D code –u sarja –p not4u
-
You can use the create_version procedure to create a version in which to perform your edits.
EXEC sde.create_version 'sde.DEFAULT', 'version2', 1, 2, 'version for my mv edits'
-
Open a Transact SQL query window and execute the set_current_version stored procedure.
EXEC sde.set_current_version 'version2'
Note:If you do not set the current version, each time you query you will see the latest state of the DEFAULT version. If you set the current version, the state is cached and your view of the version you specified remains static.
-
Start an edit session by executing the edit_version stored procedure and specifying 1.
EXEC sde.edit_version 'version2', 1
The 1 indicates an edit session should be started.
-
Perform the first edit to the multiversioned view using SQL.
In this example, an existing record is updated.
UPDATE code_mv SET propowner = 'C. Industry' WHERE vid = 2233
-
Perform the next edit to the multiversioned view using SQL.
In this example, a record is inserted to the table.
INSERT INTO code_mv (codenum, propowner) VALUES (456, 'Anjo Badsu')
Tip:In the INSERT statement, no ObjectID value is specified; the multiversioned view automatically gets the next available ObjectID and inserts it for the row.
Although it is possible to version a table that has only been registered with ArcSDE and has a user-maintained row ID column, this is not recommended. If you do this anyway, though, you must provide a row ID with the INSERT statements; the multiversioned view cannot generate a value for you.
-
Stop the edit session by executing the edit_version stored procedure, but specify 2.
EXEC sde.edit_version 'version2', 2
The 2 indicates the edit session should be ended.
-
When all editing is done, reconcile and post edits through ArcGIS Desktop, then delete the geodatabase version you created in step 2. Or if you decide you do not want the changes, you can delete the version you created in step 2 without reconciling and posting the edits.
EXEC sde.delete_version 'version2'