何在两个几何表上做UNION?

时间:2016-12-12 06:32:14

标签: sql-server

我试试:

  SELECT 
  ,6 AS ObjectTypeId
  ,[GeomColumn] AS geom
  FROM [dbo].[myTable1]

  UNION

  SELECT 
  ,objTyleId AS ObjectTypeId
  ,[GeomColumn] AS geom
  FROM [dbo].[myTable2]

但得到了错误:

The data type geometry cannot be used as an operand to the UNION, INTERSECT or EXCEPT operators because it is not comparable.

是否存在将几何列与表联合的另一种方法?

1 个答案:

答案 0 :(得分:4)

有两种方法,

1)使用UNION ALL代替UNION

我们知道它们之间存在差异,但也许可以满足您的需求。

2)使用CAST功能将geometry转换为varbinary(MAX)

下一个:

Create table #myTable1 (ObjectTypeId int identity , GeomColumn geometry)
Create table #myTable2 (ObjectTypeId int identity , GeomColumn geometry)

INSERT INTO #myTable1 (GeomColumn)  
VALUES (geometry::STGeomFromText('LINESTRING (100 100, 20 180, 180 180)', 0));  

INSERT INTO #myTable2 (GeomColumn)  
VALUES (geometry::STGeomFromText('LINESTRING (100 100, 20 180, 180 180)', 0));  


select 1 AS ObjectTypeId , Cast( GeomColumn as varbinary(MAX)) geom 
from #myTable1
union 
select ObjectTypeId ,  Cast( GeomColumn as varbinary(MAX)) geom 
from #myTable2


Drop table #myTable1
Drop table #myTable2

结果使用UNION显示的一条记录,以避免出现共享记录

enter image description here