简体   繁体   中英

PostgreSQL substitute case sensitive variables in a command

Hi I have some variables in a plpgsql script. 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:

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?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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