到目前为止,我可以掌握以下内容:
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。
答案 0 :(得分: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)
答案 1 :(得分:1)
我认为这就是你所需要的。如果您在sql-server 2005 or above
,则可以使用CTE
和row_number()
功能,如下所示,无需循环。请使用正确的列替换 cols_that_decides_order
以获取最佳记录。
此外,我认为转换为float(ex; 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