使用 SQL 2008 中的变量返回多条记录

发布于 2024-07-09 21:37:20 字数 445 浏览 10 评论 0原文

我很确定这不是正确的方法,所以我正在寻找一些建议。

我不认为我的问题在于我试图解决空间问题。 我只是不确定获取纬度和经度并在 select 语句中返回地理数据类型的好方法。 我已经成功创建了一个地理列并输入了数据。

Use thedatabase;
GO
Declare @Lat numeric(11,6)
Declare @Long numeric(11,6)
DECLARE @g geometry;




Select @Lat = Latitude, @Long = Longitude
from dbo.thetable

set @g = geometry::STGeomFromText('POINT (' + Cast(@Lat as varchar(30)) + ' ' + CAST(@Long as varchar(30)) + ')', 0);


select @g

I'm pretty sure this is not the right way to do this so I'm looking for some suggestions.

I don't think my problem so much is that I'm trying to solve a spatial problem. I'm just not sure of a good way to take the latitude and longitude and return a geography data type in a select statement. I have successfully created a geography column and input the data though.

Use thedatabase;
GO
Declare @Lat numeric(11,6)
Declare @Long numeric(11,6)
DECLARE @g geometry;




Select @Lat = Latitude, @Long = Longitude
from dbo.thetable

set @g = geometry::STGeomFromText('POINT (' + Cast(@Lat as varchar(30)) + ' ' + CAST(@Long as varchar(30)) + ')', 0);


select @g

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-07-16 21:37:20

你能澄清一下你想做什么吗?

如果问题与文本的使用有关 - 据我了解,WKT 格式在这里很正常,所以不是问题。

如果您想返回多个几何值,例如:

SELECT geometry::STGeomFromText('POINT (' + CAST(Latitude as varchar(30))
     + ' ' + CAST(Longitude as varchar(30)) + ')', 0)
FROM dbo.thetable

如果您想使用多个几何图形,请声明一个表变量并插入此类查询的结果:

DECLARE @data TABLE (location geometry)
INSERT @data (location)
SELECT geometry::STGeomFromText('POINT (' + CAST(Latitude as varchar(30))
     + ' ' + CAST(Longitude as varchar(30)) + ')', 0)
FROM dbo.thetable

并将 @data 作为一组数据使用

Can you clarify what you want to do?

If the question related to the use of text - as I understand it, the WKT format is pretty normal here, so not a problem.

If you want to return multiple geometry values, something like:

SELECT geometry::STGeomFromText('POINT (' + CAST(Latitude as varchar(30))
     + ' ' + CAST(Longitude as varchar(30)) + ')', 0)
FROM dbo.thetable

If you want to work with multiple geometries, declare a table variable and insert the results of such a query:

DECLARE @data TABLE (location geometry)
INSERT @data (location)
SELECT geometry::STGeomFromText('POINT (' + CAST(Latitude as varchar(30))
     + ' ' + CAST(Longitude as varchar(30)) + ')', 0)
FROM dbo.thetable

and work with @data as a set of data

甜嗑 2024-07-16 21:37:20

几何类型应仅用于投影数据。 由于您已经有未投影的纬度/经度数据,因此您应该使用地理类型。 此外,还有一个用于创建点的静态方法:

SELECT geography::Point(t.lat, t.lon, 0) FROM yourtable t

Geometry type should be used only for projected data. Since you already have unprojected lat/long data you should use Geography type instead. Also, there is a static method for creating points:

SELECT geography::Point(t.lat, t.lon, 0) FROM yourtable t
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文