PostgreSQL无法检索参数

时间:2018-04-26 15:15:15

标签: postgresql plpgsql greenplum

我试图创建一个函数,如果为true,它将生成第一个查询,当它失败时将运行else查询。

CREATE OR REPLACE FUNCTION update_mig_status(schemaname text,table_name 
text,fulltable text)
RETURNS VOID as $body$
DECLARE 
    passed boolean;

begin
  SELECT EXISTS (
   SELECT 1 
   FROM   pg_catalog.pg_class c
   JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace
   WHERE  n.nspname = $1
   AND    c.relname = $2
   AND    c.relkind = 'r'    -- only tables
 ) into passed;

IF passed = 't' THEN
    UPDATE dwh.table_list_2018 SET
    move_data_to_dca = 'V',
    row_count = (select count(1) from $1.$2),
    table_size = (SELECT 
    sum (table_size_byte)/1024/1024/1024 GB 
    FROM dba.hist_tbl_size_1_prt_m_201804 
    WHERE schema_name=$1 and table_name = $2
    and collect_date='2018-04-25')
    WHERE move_data_to_dca = 'P' and schema = $1 and table_name = $2;
ELSE
    UPDATE dwh.table_list_2018 SET 
    move_data_to_dca = 'V',
    purge_table = 'Y'
    WHERE move_data_to_dca = 'P' and schema = $1 and table_name = $2;
END IF;
END;
$body$
LANGUAGE plpgsql;

问题在于这一行

row_count = (select count(1) from $1.$2)

抛出此错误

ERROR:  syntax error at or near "$1"
LINE 1: ..._to_dca = 'V', row_count = (select count(1) from  $1 . $2 ),...

RROR: syntax error at or near "$1"
SQL state: 42601
Context: SQL statement in PL/PgSQL function "update_mig_status" near line 23

似乎字符串没有插值。 如果我进入这个

,它运作良好
row_count = (select count(1) from schema.table_name)

注意即时通讯使用postgresql 8.4

感谢

0 个答案:

没有答案