Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL函数子查询中未识别的变量?_Sql_Postgresql_Stored Procedures - Fatal编程技术网

PostgreSQL函数子查询中未识别的变量?

PostgreSQL函数子查询中未识别的变量?,sql,postgresql,stored-procedures,Sql,Postgresql,Stored Procedures,工作解决方案: CREATE OR REPLACE FUNCTION my_search(tsq tsquery, translation_id integer, lang regconfig, count integer DEFAULT 10, skip integer DEFAULT 0) RETURNS TABLE(id int, chapter int, number int, headline text) AS $$ SELECT id, chapter, number,

工作解决方案:

CREATE OR REPLACE FUNCTION my_search(tsq tsquery, translation_id integer, lang regconfig, count integer DEFAULT 10, skip integer DEFAULT 0)
RETURNS TABLE(id int, chapter int, number int, headline text) AS $$
      SELECT id, chapter, number, ts_headline($3, text, $1, 'StartSel = <em>, StopSel = </em>'::text) FROM (
          SELECT id, chapter, number, text FROM lyrics
          WHERE $1 @@ search_text AND translation_id = $2
          LIMIT $4 OFFSET $5) AS matches;
$$ LANGUAGE SQL STABLE;
函数变量似乎不在子查询的范围内。我一直在搜索PostgreSQL文档,但似乎找不到原因。有人知道我的变量发生了什么吗?

语言SQL只接受位置参数:

CREATE OR REPLACE FUNCTION my_search(tsq tsquery, translation_id integer, lang text, count integer DEFAULT 10, skip integer DEFAULT 0)
RETURNS TABLE(id int, chapter int, number int, headline text) AS $$
      SELECT id, chapter, number, ts_headline($3, text, $1, 'StartSel = <em>, StopSel = </em>') FROM (
          SELECT (id, chapter, number, text) FROM my_texts
          WHERE $1 @@ search_text AND translation_id = $2
          LIMIT $4 OFFSET $5) AS matches;
$$ LANGUAGE SQL STABLE;
从:

SQL函数的参数在函数体中使用语法$n引用:$1引用第一个参数,$2引用第二个参数,依此类推。如果参数是复合类型,则可以使用点表示法(例如$1.name)访问参数的属性


啊,就这样,谢谢你。对于那些可能对搜索存储过程感兴趣的人,我会用一个更有趣的标题更新这个问题:我会将标题还原回去,以便人们能够在Google上找到它。这更多地是关于位置参数,而不是全文搜索:
psql:scriptura.pgsql:7: ERROR:  column "tsq" does not exist
LINE 5:           WHERE tsq @@ search_text AND translation_id = tran...
                        ^
CREATE OR REPLACE FUNCTION my_search(tsq tsquery, translation_id integer, lang text, count integer DEFAULT 10, skip integer DEFAULT 0)
RETURNS TABLE(id int, chapter int, number int, headline text) AS $$
      SELECT id, chapter, number, ts_headline($3, text, $1, 'StartSel = <em>, StopSel = </em>') FROM (
          SELECT (id, chapter, number, text) FROM my_texts
          WHERE $1 @@ search_text AND translation_id = $2
          LIMIT $4 OFFSET $5) AS matches;
$$ LANGUAGE SQL STABLE;