Add Field to a SQL Geodatabase using T-SQL

Sometimes a new field will need to be added quickly. The usual process of testing changes and pushing said changes to production is not very time sensitive. Here is a method I use when a new field is needed ASAP.

In this case the feature classes have been registered in the geodatabase and archiving has been enabled. This means there is an SDE base table and several SDE delta tables. The information needed to proceed is listed below. This will require DBO level access and SQL Server Management Studio or other suitable RDBMS interface like Toad.

  • Database Name = DemoData
  • New Field Name = NewField
  • New Field Type (Length) = string(50) / nvarchar(50)
    • SDE Field Type Code = Unknown
  • New Field Name = NewField
  • Feature Class Name = Marker
    • Base Table Name = Marker
    • Base Table ID = Unknown
    • Delta Table (Adds) = Unknown
    • Delta Table (Archive) = Unknown

First thing to do is find the unknown values. The SDE Field Type Code via Esri tells us that the SDE Field Type Code for String is "14".
To find the feature class base table ID run the following query.


1
2
3
4
5
6
--Identify the base table registration ID from the SDE table registry
Use DemoData
go
select * from dbo.SDE_table_registry 
 where table_name='Marker'
go

The value from field "registration_id" is the base table ID.






Now time to run the queries which will add the new field. The base table is shown in line 2, the archive delta table is in line 6 and the adds delta table is in line 10. The SDE code type is a value which will be inserted into the 5th column in the SDE column registry table. Each one of these queries must be run to successfully add a new field to a registered database.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
--Alter base and delta tables by adding the new field
alter table GISAdmin.Marker
 add NewField nvarchar(50) NULL
go

alter table GISAdmin.Marker_H
 add NewField nvarchar(50) NULL
go

alter table GISAdmin.a84
 add NewField nvarchar(50) NULL
go
--Insert values into the SDE column registry. 
--Fields: 1)database_name 2)table_name 3)owner aka schema 4)column_name 5)sde_type 6)column_size
--7)decimal_digits 8)description 9)object_flags 10)object_id
insert into dbo.SDE_column_registry
 VALUES ('DemoData','Marker','GISADMIN','NewField',14,50,0,NULL,4,NULL)
go

If the feature class is not enabled for archiving than disregard the archive delta table query.

Comments

Popular posts from this blog

Updating GDB_Items XML Definitions

Animation with ArcGIS Pro

Enterprise Geodatabase Maintenance