Skip to content

Commit a2a1ae5

Browse files
committedMar 21, 2025
[mssqlspatial] Fix creation of metadata tables
The query was testing whether [dbo].[geometry_columns] exists, but then attempting to create just [geometry_columns] (without "dbo"). This fails if the table will be created in any schema other then "dbo". Adjust to check whether just [geometry_columns] exists, so that we are checking whether the table exists in the ACTUAL schema we'll be creating it in.
1 parent 26081e9 commit a2a1ae5

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed
 

‎ogr/ogrsf_frmts/mssqlspatial/ogrmssqlspatialdatasource.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -1360,9 +1360,7 @@ OGRErr OGRMSSQLSpatialDataSource::InitializeMetadataTables()
13601360
CPLODBCStatement oStmt(&oSession);
13611361

13621362
oStmt.Append(
1363-
"IF NOT EXISTS (SELECT * FROM sys.objects WHERE "
1364-
"object_id = OBJECT_ID(N'[dbo].[geometry_columns]') AND type in "
1365-
"(N'U')) "
1363+
"IF OBJECT_ID(N'[geometry_columns]', N'U') IS NULL "
13661364
"CREATE TABLE geometry_columns (f_table_catalog varchar(128) not "
13671365
"null, "
13681366
"f_table_schema varchar(128) not null, f_table_name varchar(256) "
@@ -1373,9 +1371,7 @@ OGRErr OGRMSSQLSpatialDataSource::InitializeMetadataTables()
13731371
"CONSTRAINT geometry_columns_pk PRIMARY KEY (f_table_catalog, "
13741372
"f_table_schema, f_table_name, f_geometry_column));\n");
13751373

1376-
oStmt.Append("IF NOT EXISTS (SELECT * FROM sys.objects "
1377-
"WHERE object_id = OBJECT_ID(N'[dbo].[spatial_ref_sys]') "
1378-
"AND type in (N'U')) "
1374+
oStmt.Append("IF OBJECT_ID(N'[spatial_ref_sys]', N'U') IS NULL "
13791375
"CREATE TABLE spatial_ref_sys (srid integer not null "
13801376
"PRIMARY KEY, auth_name varchar(256), auth_srid integer, "
13811377
"srtext varchar(2048), proj4text varchar(2048))");

0 commit comments

Comments
 (0)