SQL 服务器地理类型为同一个变量创建多个类型
SQL Server Geography type creates multiple types for the same variable
我开始使用 Microsoft SQL Server Geography
数据类型。一切正常,但我 运行 遇到了一个小问题。
我首先创建了一个 table(ClientId 实际上不是主要的)路由,其中包含 2 列:ClientId
(int
)和 Zone
(geography
)
CREATE TABLE [dbo].[routes]
(
[ClientId] [int] NOT NULL,
[Zone] [geography] NULL,
)
我运行下面的脚本(302624121是新创建的table):
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次!!!!
接下来,我向来自上述 table 的 select 数据添加了一个存储过程,其中给定点包含在客户的地理范围内。
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#?
问题是 hierarchyid
、geometry
和 geography
在 sys.systypes table:
中都具有相同的 xtype 值
select name, xtype, xusertype from sys.systypes where xtype = 240
/*
name xtype xusertype
------------- ------- ---------
hierarchyid 240 128
geometry 240 129
geography 240 130
*/
因此,当仅按列 xtype
将此 table 连接到查询中的系统列时,您将获得笛卡尔积。为避免这种情况,请在联接中包含 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)
我开始使用 Microsoft SQL Server Geography
数据类型。一切正常,但我 运行 遇到了一个小问题。
我首先创建了一个 table(ClientId 实际上不是主要的)路由,其中包含 2 列:ClientId
(int
)和 Zone
(geography
)
CREATE TABLE [dbo].[routes]
(
[ClientId] [int] NOT NULL,
[Zone] [geography] NULL,
)
我运行下面的脚本(302624121是新创建的table):
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次!!!!
接下来,我向来自上述 table 的 select 数据添加了一个存储过程,其中给定点包含在客户的地理范围内。
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#?
问题是 hierarchyid
、geometry
和 geography
在 sys.systypes table:
select name, xtype, xusertype from sys.systypes where xtype = 240
/*
name xtype xusertype
------------- ------- ---------
hierarchyid 240 128
geometry 240 129
geography 240 130
*/
因此,当仅按列 xtype
将此 table 连接到查询中的系统列时,您将获得笛卡尔积。为避免这种情况,请在联接中包含 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)