1

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

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

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#?

4

1 回答 1

2

问题是hierarchyid,geometry并且geography在 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
*/

因此,在仅按 column 将此表连接到查询中的 syscolumns 时,您会得到一个笛卡尔积xtype。为避免这种情况,请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)
于 2015-02-12T12:17:50.033 回答