2

My company uses a versioned SDE database to perform edits, update features, etc.

I am working on a script that would inevitably update features and would like to set the environment to a specific version.

The environment set to the SDE would look like this:

arcpy.env.workspace = "C:\\Users\\MyName.CENTRAL\\AppData\\Roaming\\ESRI\\Desktop10.3\\ArcCatalog\\Production.sde

My version is called "SY.TEST"

How do I define this version (only) as the workspace?

  • 2
    Versions can't be workspaces, but a default version can be a property of a workspace. – Vince Mar 23 '17 at 0:10
8

You can set your .sde database connection to a specific version in ArcCatalog so that any access to the gdb using that connection will use that version.

In the Catalog window, create a new connection to your database (or copy/paste your existing connection). Right-click on the new connection and select Geodatabase Connection Properties...

enter image description here

In the dialog that opens you can specify which Version to use with that connection file

enter image description here

Now any layers added to ArcMap from that connection will automatically be using the specified version. Using Arcpy with this connection will work the same way.

Alternatively, most interactions with versioned feature classes using arcpy will require an edit session, at which time you can specify a version to use. See arcpy.ChangeVersion_management() and arcpy.da.Editor()

  • Right, thanks! This would work on my local machine but ultimately this would be running on the server. Do you suppose I could have the script create another "instance" of the GDB that defines this parameter and then set the environment that way? – Simon.y Mar 23 '17 at 18:09
  • @Simon.y You don't need another instance of the database, just a connection file. Create a second connection file and reference that. Alternatively arcpy can create connection files, you might be able to specify the version in there, or at least use the Change Version on it. – Midavalo Mar 23 '17 at 18:20
  • To add to Midavalo's answer these tools can do what is being depicted via python (with examples) CreateArcSDEConnectionFile_management, CreateDatabaseConnection_management – Simon.y Apr 26 '17 at 19:05
  • @Midavalo I understand this is a year old, but I'm curious if you can expand more on the workflow you mentioned at the end of your answer. I'm trying to open an edit session under a certain version to run an update cursor on a feature class in arcpy, but I can't quite figure out how to open an edit session on a workspace and then change the version. – jesnes May 30 '18 at 22:52
  • @JesseNestler I believe that's worthy of a new question, including details of what you've tried and where you "can't quite figure out how..." You can link back to this answer as reference. – Midavalo May 30 '18 at 23:05
3

Whenever you work with multiple database connections from arcpy script, I don't think it's a good idea to rely on anything available from ArcMap such as Database Connections window. Here is why:

  • If you will run your scripts on a machine without ArcGIS Desktop where you have only Engine or Server installed, your script will break.

  • You cannot be sure which version you are working with in the code as you don't specify the version name string in the code (you just assume you are working it because the connection file is set up they way you need). If you later will edit the connection file in ArcMap and then re-run your script, you may introduce undesired changes in the database tables.

You can use a GP tool called Create Database Connection which will create a connection file that can be used to connect to a database or an enterprise, workgroup, or desktop geodatabase. When creating a connection file, you will be able to specify which version you will be working with.

I like creating a connection file on demand as I need it, connecting to a specific version instead of trying to access existing connection files and changing its version. When your script finished running, you can delete the connection file.

  • Essentially what I've done. Haven't had a chance to test it yet but I think it should work. Used the "arcpy.CreateDatabseConnection_management(...)" tool – Simon.y Mar 27 '17 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.