简体   繁体   English

PostgreSQL 在命令中替换区分大小写的变量

[英]PostgreSQL substitute case sensitive variables in a command

Hi I have some variables in a plpgsql script.您好,我在 plpgsql 脚本中有一些变量。 It's all case sensitive.都是大小写敏感的。

schema := 'myschema';
table_name := 'MyTable' 
column_name := 'MyColumn'

I need to put those variables in a simple statement:我需要将这些变量放在一个简单的语句中:

select max(column_name) from schema.table_name.

I feel like I am battling the case sensitivity with PostgreSQL. I basically need to recreate this:我觉得我正在与 PostgreSQL 区分大小写作斗争。我基本上需要重新创建它:

select max("MyColumn") from myschema."MyTable";

For the life of me I can't get this work and am clearly too dumb for PostgreSQL. Trying with EXECUTE(), EXECUTE() w/ FORMAT(), quote_indent() etc etc. Any thoughts?对于我的生活,我无法完成这项工作,并且对于 PostgreSQL 来说显然太愚蠢了。尝试使用 EXECUTE()、EXECUTE() w/ FORMAT()、quote_indent() 等。有什么想法吗?

Got it with this gem得到了这个宝石

execute format('select max(%I) from %s.%I', column_name, schema_name, table_name);

Depressing that that took hours of my life...令人沮丧的是,这花了我几个小时的时间......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM