此片段是函数中的一部分,它将整数(和ID)和字符串(表名)作为indata,称为$ 1和$ 2。在第一个示例中,由于函数当前有效,输入将为(68, 'heat_arcs')
并且函数可以正常工作。但是,需要扩展该函数以使用不同的表,因此我认为我可以将cost FROM heat_arcs'
更改为cost FROM ' || $2 ||'
,但由于"未终止的引用字符串&#34,这会产生语法错误;。
为什么我可以在查询的后半部分成功地将字符串与输入数据连接,但不是早期部分? pgr_trsp要求字符串作为第一个和最后一个参数的输入
电流:
FROM pgr_trsp(
'SELECT gid as id, source, target, cost FROM heat_arcs',
r, 2, false, false, 'select 1000 as to_cost, b.target_id, a.via_path::text from
(select gid as target_id from '|| $2 ||' where source = ' || $1 ||' or target = ' || $1 ||' ) as b,
(select gid as via_path from '|| $2 ||' where source = ' || $1 ||' or target ='|| $1 ||' ) as a WHERE b.target_id <> a.via_path'
所需:
FROM pgr_trsp(
'SELECT gid as id, source, target, cost FROM '|| $2 ||',
r, 2, false, false, 'select 1000 as to_cost, b.target_id, a.via_path::text from
(select gid as target_id from '|| $2 ||' where source = ' || $1 ||' or target = ' || $1 ||' ) as b,
(select gid as via_path from '|| $2 ||' where source = ' || $1 ||' or target ='|| $1 ||' ) as a WHERE b.target_id <> a.via_path'
答案 0 :(得分:0)
你要开始自己这个:你有一个迷路的引号!如果你仔细观察,你的问题(以及你的编辑器中)突出显示的语法就会消失。
在工作版本中,我们有一个字符串,一个逗号和一个文字r
:
'SELECT gid as id, source, target, cost FROM heat_arcs', r
在破碎的版本中,我们有一个字符串包含逗号和r:
'SELECT gid as id, source, target, cost FROM '|| $2 ||', r
取出串联,即:
'SELECT gid as id, source, target, cost FROM heat_arcs, r
所以你真正想要的是:
'SELECT gid as id, source, target, cost FROM '|| $2, r
请注意,由于您正在创建动态SQL,因此您应该确保使用正确的quoting functions(以及理想情况下对白名单进行完整性检查):
'SELECT gid as id, source, target, cost FROM '|| quote_ident($2), r