SQL Server Geography类型为同一变量创建多个类型

时间:2015-02-12 12:12:14

标签: sql-server sqlgeography

我开始使用Microsoft SQL Server Geography数据类型。一切都很好,但我遇到了一个小问题。

我首先创建了一个包含2列的表(ClientId实际上不是主要的)路由:ClientIdint)和Zonegeography

CREATE TABLE [dbo].[routes]
(
    [ClientId] [int] NOT NULL,
    [Zone] [geography] NULL,
)

我运行了以下脚本(302624121是新创建的表):

SELECT
    o.id as 'Id', c.name as 'Name', t.name AS 'Type', 
    c.length AS 'Length'
FROM 
    sys.sysobjects as o 
INNER JOIN 
    sys.syscolumns AS c ON o.id = c.id 
INNER JOIN 
    sys.systypes AS t ON c.xtype = t.xtype 
WHERE 
    (o.id = 302624121)

并且瞧,这就是我得到的:

302624121   ClientId    int 4
302624121   Zone    hierarchyid -1
302624121   Zone    geometry    -1
302624121   Zone    geography   -1

该区域已创建3次!!!!

接下来,我添加了一个存储过程来从上表中选择数据,其中给定点包含在客户端的地理位置中。

Create proc [dbo].[up_RoutesSelectByGeography]
    @ClientId int,
    @Zone geography
as
begin
    SELECT [ClientId], [Zone]         
      FROM [dbo].[Routes]         
      where ClientId = @ClientId and [Zone].STContains(@Zone) = 1 
end

我使用过程的id运行以下查询:

SELECT
    o.id as 'Id', c.name as 'Name', 
    t.name AS 'Type', c.length AS 'Length'
FROM 
    sys.sysobjects as o 
INNER JOIN 
    sys.syscolumns AS c ON o.id = c.id 
INNER JOIN 
    sys.systypes AS t ON c.xtype = t.xtype 
WHERE 
    (o.id = 334624235)

我总是为同一个变量获得3种类型:

334624235   @ClientId   int 4
334624235   @Zone   hierarchyid -1
334624235   @Zone   geometry    -1
334624235   @Zone   geography   -1

这个问题给我带来了一个问题,因为我无法用变量映射字段名称,因为我得到相同的变量名三次。

对正在发生的事情有何看法?哪些变量要映射到我的c#??

1 个答案:

答案 0 :(得分:2)

问题是hierarchyidgeometrygeography在sys.systypes表中都具有相同的xtype值:

select name, xtype, xusertype from sys.systypes where xtype = 240
/*
name          xtype   xusertype
------------- ------- ---------
hierarchyid   240     128
geometry      240     129
geography     240     130
*/

因此,只有按xtype列,才能在查询中将此表连接到syscolumns时获得笛卡尔积。要避免这种情况,请在联接中包含xusertype列:

select o.id as 'Id', c.name as 'Name', t.name AS 'Type', c.length AS 'Length' 
FROM sys.sysobjects as o 
INNER JOIN sys.syscolumns AS c ON o.id = c.id 
INNER JOIN sys.systypes AS t ON c.xtype = t.xtype AND c.xusertype = t.xusertype  -- here!
WHERE (o.id = 334624235)