我有一个db表说,persons在Postgres中由另一个有列名称的团队传下来,"first_Name".现在我正在尝试使用PG指令器在此列名称上查询此表.
select * from persons where first_Name="xyz";
Run Code Online (Sandbox Code Playgroud)
它只是回归
错误:列"first_Name"不存在
不确定我是在做一些愚蠢的事情,还是我找不到这个问题的解决方法?
Postgres 8.4及更高版本的数据库包含public模式中的模式和公司特定表中的公用表company.
company模式名称始终'company'以公司编号开头并以公司编号结束.
所以可能有以下模式:
public
company1
company2
company3
...
companynn
Run Code Online (Sandbox Code Playgroud)
应用程序始终适用于单个公司.
的search_path相应指定在ODBC或连接Npgsql的字符串,如:
search_path='company3,public'
Run Code Online (Sandbox Code Playgroud)
如何检查给定表是否存在于指定的companyn模式中?
例如:
select isSpecific('company3','tablenotincompany3schema')
Run Code Online (Sandbox Code Playgroud)
应该返回false,并
select isSpecific('company3','tableincompany3schema')
Run Code Online (Sandbox Code Playgroud)
应该回来true.
在任何情况下,该函数应仅检查companyn传递的模式,而不检查其他模式.
如果两者public和传递的模式中都存在给定的表,则该函数应该返回true.
它适用于Postgres 8.4或更高版本.
我想在Postgres函数中传递一个表名作为参数.我试过这段代码:
CREATE OR REPLACE FUNCTION some_f(param character varying) RETURNS integer
AS $$
BEGIN
IF EXISTS (select * from quote_ident($1) where quote_ident($1).id=1) THEN
return 1;
END IF;
return 0;
END;
$$ LANGUAGE plpgsql;
select some_f('table_name');
Run Code Online (Sandbox Code Playgroud)
我得到了这个:
ERROR: syntax error at or near "."
LINE 4: ...elect * from quote_ident($1) where quote_ident($1).id=1)...
^
********** Error **********
ERROR: syntax error at or near "."
Run Code Online (Sandbox Code Playgroud)
以下是更改为此时出现的错误select * from quote_ident($1) tab where tab.id=1:
ERROR: column tab.id does not exist
LINE 1: ...T EXISTS …Run Code Online (Sandbox Code Playgroud) 是否可以定义默认情况下创建新表的模式?(由"不合格的表名称"引用.)
我已经看到了在Postgres中使用"搜索路径"的一些细节,但我认为它只在检索数据时有效,而不是创建.
我有一堆SQL脚本,它们创建了许多表.我没有修改脚本,而是希望默认情况下在特定模式中设置数据库创建表 - 当它们具有非限定名称时.
这可能吗?
我正在PL/pgSQL中编写一个函数,我正在寻找检查行是否存在的最简单方法.
现在我正在选择一个integer进入a boolean,这不起作用.我对PL/pgSQL还没有足够的经验知道最好的方法.
这是我的功能的一部分:
DECLARE person_exists boolean;
BEGIN
person_exists := FALSE;
SELECT "person_id" INTO person_exists
FROM "people" p
WHERE p.person_id = my_person_id
LIMIT 1;
IF person_exists THEN
-- Do something
END IF;
END; $$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
更新 - 我现在正在做这样的事情:
DECLARE person_exists integer;
BEGIN
person_exists := 0;
SELECT count("person_id") INTO person_exists
FROM "people" p
WHERE p.person_id = my_person_id
LIMIT 1;
IF person_exists < 1 THEN
-- Do something
END IF;
Run Code Online (Sandbox Code Playgroud) 我想执行一个动态SQL语句,其返回值是IF语句的条件:
IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN
Run Code Online (Sandbox Code Playgroud)
这会产生错误ERROR: type "execute" does not exist.
是否可以这样做,或者是否有必要在IF语句之前执行SQL变量,然后将变量检查为条件?
postgresql ×6
plpgsql ×3
sql ×3
dynamic-sql ×2
identifier ×2
search-path ×2
database ×1
exists ×1
function ×1
schema ×1