LightDB - ecpg 支持dml 中使用 return into 【24.1】
在之前的版本中ecpg 中只能使用returning into 来给c 变量赋值,如下:
exec sql update t1 set c ='aa' where id = 2 returning c into :c_val;
为了兼容oracle pro*c 中return into 的用法,从24.1 开始, LightDB 也支持通过return into 来返回return 的值到c变量中。使用方式如下:
示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
print_sqlca()
{
fprintf(stderr, "==== sqlca ====\n");
fprintf(stderr, "sqlcode: %ld\n", sqlca.sqlcode);
fprintf(stderr, "sqlerrm.sqlerrml: %d\n", sqlca.sqlerrm.sqlerrml);
fprintf(stderr, "sqlerrm.sqlerrmc: %s\n", sqlca.sqlerrm.sqlerrmc);
fprintf(stderr, "sqlstate: %5s\n", sqlca.sqlstate);
fprintf(stderr, "===============\n");
}
int main() {
exec sql begin declare section;
char c_val[100] = {0};
exec sql end declare section;
ECPGdebug(1, stderr);
EXEC SQL CONNECT TO tcp:postgresql://127.0.0.1:5432/test_o;
EXEC SQL SET AUTOCOMMIT TO ON;
EXEC SQL WHENEVER SQLWARNING SQLPRINT;
EXEC SQL WHENEVER SQLERROR CALL print_sqlca();
exec sql create table t1(
id integer,
t text,
d1 numeric,
d2 float8,
c char(10));
exec sql insert into t1 values
(1, 'a', 1.0, 1, 'a'),
(2, null, null, null, null),
(4, 'd', 4.0, 4, 'd');
exec sql update t1 set c ='aa' where id = 2 return c into :c_val;
exec sql delete from t1 where id = 2 return c into :c_val;
exec sql insert into t1 values(2,null, null,null, 'bb') return c into :c_val;
exec sql
Do $$
Begin
update t1 set c ='aa' where id = 2 return c into :c_val;
end;
$$ Language plorasql;
exec sql
Do $$
Begin
delete from t1 where id = 2 return c into :c_val;
end;
$$ Language plorasql;
exec sql
Do $$
Begin
insert into t1 values(2,null, null,null, 'bb') return c into :c_val;
end;
$$ Language plorasql;
EXEC SQL DROP table t1;
exec sql disconnect;
return 0;
}
原文地址:https://blog.csdn.net/qq_17713935/article/details/136366254
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!