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

これが必要だと思います。オンになっている場合は、ループなしで以下のように使用および機能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 に答える
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 に答える