Skip to content

Instantly share code, notes, and snippets.

@tearf001
Last active July 25, 2018 16:37
Show Gist options
  • Save tearf001/038fc36481863f3b732641c01c6d5ed9 to your computer and use it in GitHub Desktop.
Save tearf001/038fc36481863f3b732641c01c6d5ed9 to your computer and use it in GitHub Desktop.
动态查询引用三级
EXECUTE 'UPDATE tbl SET '
|| quote_ident(colname) --
|| ' = '
|| quote_nullable(newvalue)
|| ' WHERE key = '
|| quote_literal(keyvalue);
EXECUTE format('UPDATE tbl SET %I = %L '
'WHERE key = %L', colname, newvalue, keyvalue);
%I is equivalent to quote_ident, and %L is equivalent to quote_nullable.
The format function can be used in conjunction with the USING clause:
EXECUTE format('UPDATE tbl SET %I = $1 WHERE key = $2', colname)
USING newvalue, keyvalue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment