Using quote_ident () in plpgsql function

I'm just new to creating the plpgsql function. I need some clarification regarding the use of quote_ident () (and even quote_literal ()) for dynamic commands executed inside this function. Hope someone can give me a concrete explanation of how they work inside the function. TIA

Here is an example:

EXECUTE 'UPDATE tbl SET ' || quote_ident(colname) || ' = ' || quote_literal(newvalue) || ' WHERE key = ' || quote_literal(keyvalue);
+4
source share
1 answer

quote_identused to quote identifiers. quote_literalused for string quoting.

postgres=# select quote_ident('tablename');
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ quote_ident β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•‘
β”‚ tablename   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
(1 row)

postgres=# select quote_ident('special name');
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  quote_ident   β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•‘
β”‚ "special name" β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
(1 row)

postgres=# select quote_literal(e'some text with special char"\'"');
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           quote_literal           β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•‘
β”‚ 'some text with special char"''"' β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
(1 row)

? , , , ... ? - ( ). , - SQL.

- . quote_literal USING ( ), quote_ident format (- ):

EXECUTE format('UPDATE tbl SET %I=$1 WHERE key=$2', colname) 
  USING newvalue, keyvalue;

EXECUTE format('UPDATE tbls SET %I=%L WHERE key=%L', colname, newvalue, keyvalue);

SQL a) ( ), b) SQL-.

+11

Source: https://habr.com/ru/post/1598801/


All Articles