Convert Geometry to Geography

5.3k Views Asked by At

I want to convert geometry to geography in SQL Server; I followed this article:

https://blogs.msdn.microsoft.com/edkatibah/2008/08/19/working-with-invalid-data-and-the-sql-server-2008-geography-data-type-part-1b/

Here is my query:

INSERT INTO gCOMMUNE         
SELECT 
      [dbo].[commune].[ogr_fid], 
      GEOGRAPHY::STGeomFromWKB(commune.ogr_geometry.STAsBinary(),4326)
FROM [IMMATS].[dbo].[commune]

but when I ran the command to convert I got this error:

Msg 213, Level 16, State 1, Line 26 The name or column number of the values provided does not match the definition of the table.

1

There are 1 best solutions below

7
On

It looks like gCOMMUNE table has different number of columns than the query for selecting the data to be inserted in it. You must specify the column names in the INSERT INTO statement. Assuming there are columns named id and geom (from your clarification it turns out they are [ogr_fid] and [ogr_geog]), here is how your statement could look like:

INSERT INTO gCOMMUNE([ogr_fid], [ogr_geog])
SELECT 
      [dbo].[commune].[ogr_fid], 
      GEOGRAPHY::STGeomFromWKB(commune.ogr_geometry.STAsBinary(),4326)
FROM [IMMATS].[dbo].[commune]