在PostgreSQL中创建以下函数时,我得到了
错误:与OID 80424的关系不存在语境:SQL函数 " test_update_icd_code"语句7 SQL状态:42P01
那么如何在函数中创建表?
(注意:在完成该功能时将删除该表。)
TIA
CREATE OR REPLACE FUNCTION test_update_icd_code()
RETURNS void
AS $$
-- Create a test table that references dx_log.
DROP TABLE IF EXISTS test;
CREATE TABLE test
(
recid serial PRIMARY KEY,
dx_log_recid integer NOT NULL,
CONSTRAINT test_dx_log_fk FOREIGN KEY (dx_log_recid)
REFERENCES dx_log (recid) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT
)
WITH (
OIDS = FALSE
);
-----------
$$ LANGUAGE 'sql' VOLATILE;
答案 0 :(得分:0)
不确定为什么您的函数调用不起作用。我拿了你上面的代码,通过psql
运行它似乎工作正常:
postgres=# DROP TABLE IF EXISTS test;
DROP TABLE
postgres=# DROP TABLE IF EXISTS dx_log;
DROP TABLE
postgres=# CREATE TABLE dx_log (recid INTEGER PRIMARY KEY) ;
CREATE TABLE
postgres=# CREATE OR REPLACE FUNCTION test_update_icd_code()
postgres-# RETURNS void
postgres-# AS $$
postgres$#
postgres$# -- Create a test table that references dx_log.
postgres$# DROP TABLE IF EXISTS test;
postgres$#
postgres$# CREATE TABLE test
postgres$# (
postgres$# recid serial PRIMARY KEY,
postgres$# dx_log_recid integer NOT NULL
postgres$# ,CONSTRAINT test_dx_log_fk FOREIGN KEY (dx_log_recid) REFERENCES dx_log (recid) MATCH SIMPLE ON UPDATE CASCADE ON DELETE RESTRICT
postgres$# )
postgres$# WITH (
postgres$# OIDS = FALSE
postgres$# );
postgres$#
postgres$# -----------
postgres$#
postgres$# $$ LANGUAGE 'sql' VOLATILE;
CREATE FUNCTION
postgres=# SELECT test_update_icd_code();
NOTICE: table "test" does not exist, skipping
test_update_icd_code
----------------------
(1 row)
postgres=# SELECT * FROM test
postgres-# ;
recid | dx_log_recid
-------+--------------
(0 rows)
你如何调用函数 - 实际命令,什么客户端?函数在进入CREATE TABLE之前是否出错了?如果您注释掉DROP TABLE并运行该函数该怎么办?它会怎么做?
我正在测试Postgres 10.0