Oracle兼容-存储过程-%TYPE


1. 语法

  1. var_name (ref_var_name | table_name.column_name | ref_rowtype)%TYPE [:= init_val]

2. 定义和用法

在GreatSQL中支持用 %TYPE 来声明变量类型,利用 %TYPE 声明可使得变量的数据类型与其他某个变量或列的数据类型相同。

可以被 %TYPE 应用的变量类型有:

  • 普通变量
  • 表中某列
  • %ROWTYPE类型
  • %ROWTYPE类型中某列

3. Oracle兼容说明

ORACLE 模式下,GreatSQL支持用 %TYPE 来声明变量类型。该用法如下所述:

  1. 支持所有已兼容的数据类型。

  2. 不支持引用临时表中的列。

  3. 不支持事务(VIEW),包括 view%TYPEview%ROWTYPE

  4. 不支持复用自己类型的用法,例如 x x%TYPE

4. 示例

修改 sql_generate_invisible_primary_key 选项设定,因为下面案例中创建的表没有显式主键,关闭该选项可以避免自动创建隐式主键 my_row_id,可能会对下面的案例造成影响。

  1. greatsql> SET SESSION sql_generate_invisible_primary_key = 0;

创建测试表并初始化数据

  1. greatsql> CREATE TABLE t1 (a INT NOT NULL, b VARCHAR(20) NOT NULL);
  2. greatsql> INSERT INTO t1 VALUES(1, 'row1'), (2, 'row2'), (3,'row3') ;
    1. 示例1
  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> DELIMITER //
  3. greatsql> CREATE OR REPLACE PROCEDURE vtype_sp1() AS
  4. v_id t1.a%TYPE;
  5. v_vchar VARCHAR(100) := 'varchar_type';
  6. v3 v_vchar%TYPE;
  7. v4 v3%TYPE;
  8. BEGIN
  9. v_id := 1;
  10. v_vchar := 'v_vchar_type';
  11. v3 := 'v3_varchar';
  12. v4 := 'v4_varchar';
  13. SELECT v_id, v_vchar, v3, v4;
  14. END; //
  15. greatsql> CALL vtype_sp1() //
  16. +------+--------------+------------+------------+
  17. | v_id | v_vchar | v3 | v4 |
  18. +------+--------------+------------+------------+
  19. | 1 | v_vchar_type | v3_varchar | v4_varchar |
  20. +------+--------------+------------+------------+
  21. 1 row in set (0.00 sec)
  22. Query OK, 0 rows affected (0.00 sec)
    1. 示例2
  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> DELIMITER //
  3. greatsql> CREATE OR REPLACE PROCEDURE vtype_sp2() AS
  4. CURSOR cur1 (v_a INT) IS SELECT * FROM t1 WHERE a = v_a;
  5. rec1 cur1%ROWTYPE;
  6. rec2 rec1%TYPE;
  7. BEGIN
  8. FOR rec2 IN cur1(1)
  9. LOOP
  10. SELECT rec2.b;
  11. END LOOP;
  12. END; //
  13. greatsql> CALL vtype_sp2() //
  14. +--------+
  15. | rec2.b |
  16. +--------+
  17. | row1 |
  18. +--------+
  19. 1 row in set (0.00 sec)
  20. Query OK, 0 rows affected (0.00 sec)
    1. 示例3
  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> DELIMITER //
  3. greatsql> DECLARE
  4. rec t1.a%TYPE;
  5. BEGIN
  6. rec := ROUND(RAND()*10240);
  7. SELECT rec;
  8. END; //
  9. +------+
  10. | rec |
  11. +------+
  12. | 8473 |
  13. +------+
  14. 1 row in set (0.01 sec)
  15. Query OK, 0 rows affected (0.01 sec)
    1. 示例4
  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> SET udt_format_result = 'DBA';
  3. greatsql> CREATE OR REPLACE TYPE udt1 AS OBJECT(a INT, b VARCHAR(20));
  4. greatsql> CREATE TABLE udt_t1(a INT, b udt1);
  5. greatsql> INSERT INTO udt_t1 VALUES(1, udt1(1, 'c1_row1'));
  6. greatsql> INSERT INTO udt_t1 VALUES(2, udt1(2, 'c1_row2'));
  7. greatsql> INSERT INTO udt_t1 VALUES(3, udt1(3, 'c1_row3'));
  8. greatsql> DELIMITER //
  9. greatsql> CREATE OR REPLACE PROCEDURE vtype_sp3() AS
  10. vt1 udt_t1.b%TYPE;
  11. vt2 udt1;
  12. BEGIN
  13. SELECT b INTO vt1 FROM udt_t1 WHERE a = 2;
  14. SELECT vt1.a, vt1.b;
  15. SELECT b INTO vt2 FROM udt_t1 WHERE a = 3;
  16. SELECT vt2.a, vt2.b;
  17. SELECT vt1 INTO vt2;
  18. SELECT vt2.a, vt2.b;
  19. END; //
  20. greatsql> CALL vtype_sp3() //
  21. +-------+---------+
  22. | vt1.a | vt1.b |
  23. +-------+---------+
  24. | 2 | c1_row2 |
  25. +-------+---------+
  26. 1 row in set (0.00 sec)
  27. +-------+---------+
  28. | vt2.a | vt2.b |
  29. +-------+---------+
  30. | 3 | c1_row3 |
  31. +-------+---------+
  32. 1 row in set (0.00 sec)
  33. +-------+---------+
  34. | vt2.a | vt2.b |
  35. +-------+---------+
  36. | 2 | c1_row2 |
  37. +-------+---------+
  38. 1 row in set (0.00 sec)
  39. Query OK, 0 rows affected (0.00 sec)
    1. 示例5
  1. -- 在示例4的基础上继续
  2. greatsql> SET sql_mode = ORACLE;
  3. greatsql> DELIMITER //
  4. greatsql> CREATE OR REPLACE PROCEDURE vtype_sp4() AS
  5. vt1 udt_t1%ROWTYPE;
  6. BEGIN
  7. vt1.b := udt1(10, 'c1_row10');
  8. SELECT vt1, vt1.a, vt1.b.a, vt1.b.b;
  9. END; //
  10. greatsql> CALL vtype_sp4() //
  11. +------------------------------+-------+---------+----------+
  12. | vt1 | vt1.a | vt1.b.a | vt1.b.b |
  13. +------------------------------+-------+---------+----------+
  14. | a:NULL | b:a:10 | b:c1_row10 | NULL | 10 | c1_row10 |
  15. +------------------------------+-------+---------+----------+
  16. 1 row in set (0.00 sec)
  17. Query OK, 0 rows affected (0.00 sec)

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx