From a2a1ae507f523c806200b39a8589572f4de5f6a8 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Fri, 21 Mar 2025 06:46:42 +1000 Subject: [PATCH] [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. --- .../mssqlspatial/ogrmssqlspatialdatasource.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ogr/ogrsf_frmts/mssqlspatial/ogrmssqlspatialdatasource.cpp b/ogr/ogrsf_frmts/mssqlspatial/ogrmssqlspatialdatasource.cpp index a22944103b75..7a13c7c8014d 100644 --- a/ogr/ogrsf_frmts/mssqlspatial/ogrmssqlspatialdatasource.cpp +++ b/ogr/ogrsf_frmts/mssqlspatial/ogrmssqlspatialdatasource.cpp @@ -1360,9 +1360,7 @@ OGRErr OGRMSSQLSpatialDataSource::InitializeMetadataTables() CPLODBCStatement oStmt(&oSession); oStmt.Append( - "IF NOT EXISTS (SELECT * FROM sys.objects WHERE " - "object_id = OBJECT_ID(N'[dbo].[geometry_columns]') AND type in " - "(N'U')) " + "IF OBJECT_ID(N'[geometry_columns]', N'U') IS NULL " "CREATE TABLE geometry_columns (f_table_catalog varchar(128) not " "null, " "f_table_schema varchar(128) not null, f_table_name varchar(256) " @@ -1373,9 +1371,7 @@ OGRErr OGRMSSQLSpatialDataSource::InitializeMetadataTables() "CONSTRAINT geometry_columns_pk PRIMARY KEY (f_table_catalog, " "f_table_schema, f_table_name, f_geometry_column));\n"); - oStmt.Append("IF NOT EXISTS (SELECT * FROM sys.objects " - "WHERE object_id = OBJECT_ID(N'[dbo].[spatial_ref_sys]') " - "AND type in (N'U')) " + oStmt.Append("IF OBJECT_ID(N'[spatial_ref_sys]', N'U') IS NULL " "CREATE TABLE spatial_ref_sys (srid integer not null " "PRIMARY KEY, auth_name varchar(256), auth_srid integer, " "srtext varchar(2048), proj4text varchar(2048))");