1

我也是空间数据类型和 SQL 服务器的新手,我有以下表格:

  • 表(一)ZipCodesUSA

    • 列:zipcodes| latitude| longitude| city| state

  • 表(B)US_Cities_longitude_Latitude

    • 列:ID int| City nvarchar(40)| Coordinates GEOGRAPHY|

我要做的是创建一个 SPROC 将所有数据从表传输AB并更改为 GEOGRAPHY,一次按状态。它确实发生了变化,但每个状态只有 1 行。为什么?我没有指定SELECT 1或任何东西。先感谢您!

ALTER PROC dbo.USA_Cities
@StateAbbr NVARCHAR(2)
AS
BEGIN

    SET NOCOUNT ON;

    DECLARE @City NVARCHAR(30)
    DECLARE @Longitude NVARCHAR(50)
    DECLARE @Latitude  NVARCHAR(50)

    SELECT  @City = City, @Longitude= Longitude,@Latitude= Latitude 
    FROM ZipCode
    WHERE StateAbbr = @StateAbbr


    INSERT INTO dbo.US_Cities(City, Coordinates)
    VALUES(@City, geography::Point(ISNULL(@Longitude, ''),ISNULL(@Latitude, ''), 4326))

END;


EXEC dbo.USA_Cities 'NY'
4

2 回答 2

3

以下代码:

SELECT  @City = City, @Longitude= Longitude,@Latitude= Latitude 
FROM ZipCode
WHERE StateAbbr = @StateAbbr;

每个状态只获取一行——不是一个状态的所有行。您需要将其放入循环中,或将selectand替换为insert

INSERT INTO dbo.US_Cities(City, Coordinates)
    SELECT  @City = City,
            geography::Point(ISNULL(Longitude, ''), ISNULL(Latitude, ''), 4326)
    FROM ZipCode
    WHERE StateAbbr = @StateAbbr;
于 2013-09-02T03:51:14.250 回答
0

CREATE PROC [dbo].[USA_CitiesModified] @StateAbbr NVARCHAR(2) AS BEGIN

SET NOCOUNT ON;     

INSERT INTO dbo.US_Cities(City, Coordinates)
SELECT  City, geography::Point(ISNULL(Longitude, ''),ISNULL(Latitude, ''), 4326)
FROM dbo.ZipCode
WHERE StateAbbr = @StateAbbr

结尾;

效果很好,谢谢!

于 2013-09-02T11:08:37.263 回答