【发布时间】:2016-04-17 16:09:29
【问题描述】:
我不知道如何访问我的 plpgsql 函数中的变量。我在 Cygwin 下使用 postgres 9.5。
functions.sql
-- this works fine
\echo Recreate = :oktodrop
CREATE OR REPLACE FUNCTION drop_table(TEXT) RETURNS VOID AS
$$
BEGIN
IF EXISTS ( SELECT * FROM information_schema.tables WHERE table_name = $1 ) THEN
-- syntax error here:
IF (:oktodrop == 1 ) THEN
DROP TABLE $1;
END IF;
END IF;
END;
$$
language 'plpgsql';
psql.exe -v oktodrop=1 -f functions.sql
Password:
Recreate = 1
psql:functions.sql:13: ERROR: syntax error at or near ":"
LINE 5: IF (:oktodrop == 1 ) THEN
^
【问题讨论】:
-
一个简单的
select $$ :oktodrop $$也不会插入变量:字符串中的内容超出了 psql 解析器的范围。除此之外,PG 9.5 提供了DROP TABLE IF EXISTS tablename;,所以你可能根本不需要那个函数。
标签: postgresql