LightDB 从24.1 版本开始支持 oracle pro*c 中执行匿名块的语法(之前可以通过do 语句执行匿名块):
EXEC SQL EXECUTEanonymous block
END-EXEC;
因为匿名块不是SQL标准的一部分,所以此用法也不存在于SQL标准中。
示例
#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[2000] = {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 executebeginupdate t1 set c ='aa' where id = 2 return c into :c_val;end;end-exec;EXEC SQL EXECUTEBEGIN:c_val:=dbms_metadata.get_ddl('TABLE', 'T1');END;END-EXEC;EXEC SQL DROP table t1;exec sql disconnect;return 0;
}
Note
需要注意的是由于内部实现方式的原因,在匿名块中不能使用 $AnonBlockStmt$
,示例如下:
EXEC SQL EXECUTEdeclareval text := $AnonBlockStmt$'$-/:ad--/*sds*/ds$AnonBlockStmt$;beginbeginupdate t1 set c = '-$--///**/C :test_int[0]' where id = :test_int[0];if SQL%NOTFOUND then:iReturnCode = 1;rollback;end if;:iReturnCode = 0;commit;end;end;END-EXEC;