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) 在PostgreSQL中有一个简单的替代方法来生成Oracle中的这个语句吗?
select table_name from user_tab_columns
where table_name = myTable and column_name = myColumn;
Run Code Online (Sandbox Code Playgroud)
然后我测试查询是否返回任何内容以证明列存在.
我知道使用psql我可以单独找到这些,但这是在我编写的程序中生成结果以验证我的数据库表中存在请求的属性字段所必需的.
我们有几个迁移脚本,它们可以在不同版本之间改变模式.
有时会发生迁移步骤(例如,向表中添加列)已经手动或通过修补程序安装完成,因此迁移脚本失败.
如何防止脚本因错误而停止(理想情况下是在特定的预期错误处),而是记录消息并继续脚本?
我们使用PostgresQL 9.1,PostgresQL的解决方案以及一般的SQL解决方案都可以.
以下适用于Postgres 9.6但不适用于Redshift:
ALTER TABLE stats
ADD COLUMN IF NOT EXISTS panel_exit timestamp;
Run Code Online (Sandbox Code Playgroud)
可以在Redshift中实现相同的功能吗?
postgresql ×4
sql ×3
database ×1
ddl ×1
dynamic-sql ×1
function ×1
identifier ×1
plpgsql ×1
search-path ×1