云数据库 GaussDB-执行动态非查询语句:示例

时间:2025-01-26 10:48:52

示例

 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940
--创建表gaussdb=# CREATE TABLE sections_t1(   section       NUMBER(4) ,   section_name  VARCHAR2(30),   manager_id    NUMBER(6),   place_id      NUMBER(4) );CREATE TABLE--声明变量gaussdb=# DECLARE    section       NUMBER(4) := 280;    section_name  VARCHAR2(30) := 'Info support';    manager_id    NUMBER(6) := 103;   place_id      NUMBER(4) := 1400;   new_colname   VARCHAR2(10) := 'sec_name';BEGIN --执行查询    EXECUTE IMMEDIATE 'insert into sections_t1 values(:1, :2, :3, :4)'        USING section, section_name, manager_id,place_id; --执行查询(重复占位符)    EXECUTE IMMEDIATE 'insert into sections_t1 values(:1, :2, :3, :1)'        USING section, section_name, manager_id; --执行ALTER语句(建议采用“||”拼接数据库对象构造DDL语句)    EXECUTE IMMEDIATE 'alter table sections_t1 rename section_name to ' || new_colname;END; /ANONYMOUS BLOCK EXECUTE--查询数据gaussdb=# SELECT * FROM sections_t1; section |   sec_name   | manager_id | place_id ---------+--------------+------------+----------     280 | Info support |        103 |     1400     280 | Info support |        103 |      280(2 rows)--删除表gaussdb=# DROP TABLE sections_t1;DROP TABLE
support.huaweicloud.com/centralized-devg-v3-gaussdb/gaussdb-42-0745.html