0

到目前为止,这是我能掌握的:

declare @i int = 7;
declare @lat float;
declare @lng float;
declare @point ???; -- what type?

declare @location table(lat float, lng float)

insert @location values (32.097218, 34.770438199999944)
insert @location values (32.083847, 34.775618)
insert @location values (32.1600788, 34.771902)
insert @location values (31.9914283, 34.80780099999993)
insert @location values (32.1574281, 34.775191800000016)
insert @location values (29.5543212, 34.89448429999993)
insert @location values (32.8018919, 34.96268420000001)

while @i > 0
    begin
            -- this is a wrong way to do it
        set @point = (select top (1) * from @location);
            -- must set @lat and @lng here somehow, based on the
            -- one row selected above. Deleting here is not
            -- mandatory, but may be used, if needed.
        delete top (1) from @location;
        update top (1) rest_tbl
        set lat = @lat, lng = @lng
        where lat is null and private_label_id = 3
        set @i = @i - 1
    end;

请不要介意我创建@location变量的部分——在现实世界中,它是一个实际的表,我只是将它用于 PoC。

4

2 回答 2

1

我认为Cursor在这件事上使用会有所帮助

DECLARE db_cursor CURSOR FOR (Select  lat,lng  from @location
where lat not in (Select lat from rest_tbl where lat is not null));

DECLARE @lat Float;
DECLARE @lng Float;

OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO @lat,@lng;
WHILE @@FETCH_STATUS = 0  
BEGIN  
        UPDATE top (1) rest_tbl
        SET  lat =@lat,lng =@lng
        where lat is null

       FETCH NEXT FROM db_cursor INTO @lat,@lng;
END;
CLOSE db_cursor;

Delete from  @location
    where lat in (Select lat from rest_tbl where lat is not null)
于 2013-02-27T12:20:10.373 回答
1

我认为这就是你所需要的。如果你在,sql-server 2005 or above你可以使用CTErow_number()运行如下没有循环。请cols_that_decides_order用正确的列替换以获得最高记录。

此外,我认为您lng在转换为浮点数时会被四舍五入(例如;34.770438199999944 >> 34.7704382)。

--Declare the table with auto incremented identity with seed=7 and increment = -1.
declare @location table(mykey int identity(7,-1), lat float, lng float)

insert @location values (32.097218, 34.770438199999944)
                       ,(32.083847, 34.775618)
                       ,(32.1600788, 34.771902)
                       ,(31.9914283, 34.80780099999993)
                       ,(32.1574281, 34.775191800000016)
                       ,(29.5543212, 34.89448429999993)
                       ,(32.8018919, 34.96268420000001)

;with cte as (
   select lat,lng, row_number() over (order by cols_that_decides_order) rn
   from rest_tbl
   where lat is null and private_label_id = 3
)
update c set c.lat = t.lat, c.lng = t.lng
from cte c join @location t on c.rn = t.myKey
于 2013-02-27T12:44:11.740 回答