[英]how to use a loop with the function pgr_dijkstra
I want to use a loop to calculate the distance traveled between two nodes 152 and 17720 (I use the function pgr_dijkstra) by deleting each time a cell. 我想通过每次删除一个单元格来使用循环来计算两个节点152和17720之间的距离(我使用函数pgr_dijkstra)。 A cell contains several road links.
一个单元格包含多个道路链接。 the grid_edges_routard table contains the road links and the corresponding cell.
grid_edges_routard表包含道路链接和相应的单元格。 Iwant to have for each blocked cell the distance traveled between the two nodes.
希望每个阻塞的单元都有两个节点之间的距离。 I must use pgr_dijkstra to display in a second time the links traveled.
我必须使用pgr_dijkstra再次显示链接的运行情况。
CREATE OR REPLACE FUNCTION get_dist_grid()
RETURNS TABLE (
celref_blocked INT,
dist INT
) AS $$
DECLARE
var_r record;
BEGIN
FOR var_r IN(SELECT distinct(cellule)as cel from grid_edges_routard )
LOOP
SELECT * FROM pgr_dijkstra('SELECT id, source, target,cost
FROM road_routard.edges_vulnerabilite
where id not in (select edge_id
from grid_edges_routard
where cellule=var_r) ',152 ,17720, FALSE)
where edge=-1;
celref_blocked := var_r.cel ;
RETURN NEXT;
END LOOP;
END; $$
LANGUAGE 'plpgsql';
select get_dist_grid()
I have an error message: ERROR: column « var_r » does not exist. 我收到一条错误消息:错误:列«var_r»不存在。 I use postgresql 9.5.
我使用PostgreSQL 9.5。
定义类型的记录的新变量(var_q),然后执行你的选择查询到你这样的定义的变量Execute 'SELECT * FROM pgr_dijkstra(''SELECT id, source, target,cost FROM road_routard.edges_vulnerabilite where id not in (select edge_id from grid_edges_routard where cellule='||var_r||') '',152 ,17720, FALSE) where edge=-1' into var_q
这可能会产生一些错误,因为我们必须对内部查询使用引号,如果Execute 'SELECT * FROM pgr_dijkstra(''SELECT id, source, target,cost FROM road_routard.edges_vulnerabilite where id not in (select edge_id from grid_edges_routard where cellule='||var_r||') '',152 ,17720, FALSE) where edge=-1' into var_q
引号,请尝试转义它不起作用,然后您可以像使用celref_blocked:= var_r.cel一样,使用out查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.