postgresql 中 相似 oracle EXECUTE IMMEDIATE 的寫法

pl/pgsql中,執行動態SQL的格式以下(摘錄自說明文檔):sql

1
EXECUTE command-string [ INTO [STRICT] target ] [ using expression [, ... ] ];

其中,express

command-string就是要執行的動態sql語句(必定要記住:這裏是sql語句,不是pl/pgSQL語句,像raise notice就不能使用);spa

INTO子句是把sql查詢到的值賦給INTO指定的變量;code

using子句是前面的command-string中替代變量($1, $2, ...)的賦值;ci

示例:文檔

1
2
3
4
5
6
7
8
9
do $$
declare
v_c 1  integer;
v_c 2  integer;
begin
execute  'select count(*) as c1, count(*) as c2 from (select 1 as idx union select 11 as idx union select 21 as idx) s where idx > $1' 
into v_c 1 , v_c 2
using  10 ;

搜索

raise notice  '%, %' , v_c 1 , v_c 2 ;
相關文章
相關標籤/搜索