当前位置:网站首页>Anchor type and row data type of DB2 SQL pl
Anchor type and row data type of DB2 SQL pl
2022-06-10 18:45:00 【Blue black 2020】
Environmental Science
- operating system :Ubuntu 20.04
- Db2:11.5.0.0
Anchor data type
An anchor type is a type defined based on another database object , Like columns 、 Global variables 、SQL Variable etc. .
The difference between an anchor variable and a normal variable is , The former will be quoted to something specific . Look at it this way , I feel that the anchor type is somewhat similar to “ Foreign keys ” The concept of .
Create table tab1 and tab2 as follows :
CREATE TABLE tab1(col1 INT, col2 CHAR)
INSERT INTO tab1 VALUES (1,2)
INSERT INTO tab1 VALUES (3,4)
CREATE TABLE tab2 (col1a INT, col2a CHAR)
create a file test1.sql as follows :
CREATE OR REPLACE PROCEDURE p1()
BEGIN
DECLARE var1 ANCHOR tab1.col1;
SELECT col1 INTO var1 FROM tab1 WHERE col2 = 2;
INSERT INTO tab2 VALUES (var1, 'a');
END@
CALL p1()@
Run script test1.sql , as follows :
* temp0527 db2 -[email protected] -f test1.sql
DB20000I The SQL command completed successfully.
Return Status = 0
See the table tab2 , It can be seen that there is an additional line of records :
* temp0527 db2 "select * from tab2"
COL1A COL2A
----------- -----
1 a
1 record(s) selected.
In this case , Anchor variable var1 Defined as reference tab1 Tabular col1 Column , Next, assign a value to the... Of a specific row col1 The value of the column , Finally, it can be used as a general variable var1 .
Be careful , to var1 assignment , There can be no value that satisfies the condition , But there cannot be more than one value that satisfies the condition .
- If
SELECT col1 FROM tab1 WHERE col2 = 2The result is empty. , bevar1The value of isNULL, No problem ( This is also consistent with foreign keys , The value of the foreign key can be null , That is, no specific values are referenced ); - If
SELECT col1 FROM tab1 WHERE col2 = 2The result has multiple values , May be an error ;
establish test2.sql The documents are as follows :
CREATE OR REPLACE PROCEDURE p2(in var1 anchor tab1.col1)
BEGIN
INSERT INTO tab2 VALUES (var1, 'b');
END@
CALL p2(100)@
function test2.sql Script , as follows :
* temp0527 db2 -[email protected] -f test2.sql
DB20000I The SQL command completed successfully.
Return Status = 0
The result was a bit unexpected , 100 Not at all tab1 Tabular col1 Value of field , But there's no mistake , tab2 A line was inserted in the (100, 'b') The record of . In this case , That statement is anchor tab1.col1 What's the use ?
Row data type
Line variable , seeing the name of a thing one thinks of its function , It represents a line of records .
To use row variables , You need to create row data types first , such as :
create or replace type myrow as row(c1 int, c2 varchar(100))
Then you can declare the line variables , such as :
declare r1 myrow
When creating row variables , Each of these fields is initialized to NULL value .
The following types of values can be assigned to row variables :
- literal
set r1 = (1, 'aaa')
- expression
values (1, 'aaa') into r1
- Function return value
set r1 = f1()
- Other variables
set r1 = r2
Be careful : r1 and r2 Must be of the same or compatible type . If r1 and r2 Is a completely different data type myrow1 and myrow2 , Even if myrow1 and myrow2 Same definition , It can't pass set r1 = r2 To assign a value , Because the two types are incompatible .
- Query results
set r1 = (select c1, c2 from t1)
Be careful : Must be 0 Row or single row results .
NULL
Be careful : Each field is set to NULL , But the row variable itself is not NULL .
Row variable assignment method :
set
See the previous example .
values into
See the previous example .
select into
select 1, 'aaa' into r1 from sysibm.sysdummy1
fetch into
declare c1 cursor for select 1, 'aaa' from sysibm.sysdummy1;
open c1;
fetch c1 into r1;
close c1;
For fields of row variables ( namely < Line variable >.< Column > ) The type and manner of assignment is similar to that of assignment to row variables .
You cannot directly compare two row variables , Only its field values can be compared .
Can be in insert Statement using line variables , such as :
insert into t1 values r1
Equivalent to :
insert into t1 values (r1.c1, r1.c2)
Can be in a insert Insert multiple row variables into the statement :
insert into t1 values r1, r2
Let's look at a complete example .
create a file test3.sql as follows :
create or replace type myrow as row(c1 int, c2 varchar(100))@
set serveroutput on@
begin
declare r1, r2 myrow;
declare c1 cursor for select 3, 'ccc' from sysibm.sysdummy1;
set r1 = (1, 'aaa');
call dbms_output.put_line('1)' || r1.c1 || ', ' || r1.c2);
set r2 = r1;
call dbms_output.put_line('2)' || r2.c1 || ', ' || r2.c2);
set r1 = null;
call dbms_output.put_line('3)' || coalesce(cast(r1.c1 as varchar(100)), '') || ', ' || coalesce(r1.c2, ''));
select 2, 'bbb' into r1 from sysibm.sysdummy1;
call dbms_output.put_line('4)' || r1.c1 || ', ' || r1.c2);
open c1;
fetch c1 into r1;
close c1;
call dbms_output.put_line('5)' || r1.c1 || ', ' || r1.c2);
end@
set serveroutput off@
drop type [email protected]
Run script test3.sql , as follows :
* temp0527 db2 -[email protected] -f test3.sql
DB20000I The SQL command completed successfully.
DB20000I The SET SERVEROUTPUT command completed successfully.
DB20000I The SQL command completed successfully.
1)1, aaa
2)1, aaa
3),
4)2, bbb
5)3, ccc
DB20000I The SET SERVEROUTPUT command completed successfully.
DB20000I The SQL command completed successfully.
notes : stay 3) It's about , because NULL After splicing with other strings, it is still NULL , So here we use coalesce() Function will NULL To '' , however int Type of NULL and '' Are not compatible , So first cast to varchar type , And then use coalesce() function .
Reference resources
边栏推荐
- Wechat applet, get the current page and judge whether the current page is a tabbar page
- 使用DAP-Link单独下载可执行文件到MM32F5微控制器
- Three ways generated by stream lambda
- Custom types: structural bodies
- 记录一个超级乌龙的智障bug,也许能帮助类似我的白吃
- Uniapp native JS to convert the Gregorian calendar to the lunar calendar
- 商业智能BI的价值,可视化报表等于商业智能BI吗?
- JS Touch
- 单调栈结构
- uniapp 原生js实现公历转农历
猜你喜欢

改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年

SaleSmartly | 再添新渠道Slack,助你拉近客户关系

数字化转型怎样转?朝哪转?

Developers changing the world - Yao Guang teenagers playing Tetris

商业智能BI在企业的价值之:业务分析发展决策

干货 | 一文搞定 uiautomator2 自动化测试工具使用

Stream生成的3张方式-Lambda

低碳数据中心建设思路及未来趋势

leecode27,977-双指针法

After the qtmqtt source code compilation is set to keepalive, the Ping package timeout error does not return a problem repair (qmqtt:: mqttnopingresponse, qmqtt:: clientprivate:: onpingtimeo)
随机推荐
数据的软删除—什么时候需要?又如何去实现?
flutter系列之:UI layout简介
Vcsa7u3c installation tutorial
期货网上开户安全吗,具体怎么开户的
华为云鲲鹏DevKit代码迁移实战
商业智能BI如何帮企业降低人力、时间和管理成本?
【QNX Hypervisor 2.2 用户手册】3.3 配置Guest
“数字化转型,数据先行”,谈谈数据治理对企业来说到底有多重要
LoRa模块无线收发通信技术详解
mysql备份和shell脚本手动执行没问题,crontab定时执行失败
Faster Planner——Kinodynamic Astar详解
[QNX hypervisor 2.2 user manual] 3.3 configure guest
Adobe Premiere基础-素材嵌套(制作抖音结尾头像动画)(九)
作为程序员,对于底层原理真的有那么重要吗?
完全背包问题以及优化小技巧
Ruijie x32pro brush openwrt enable wireless 160MHz
AFL fuzzy multithreading
c语言---7 初识操作符
[kuangbin] topic 22 interval DP
After the qtmqtt source code compilation is set to keepalive, the Ping package timeout error does not return a problem repair (qmqtt:: mqttnopingresponse, qmqtt:: clientprivate:: onpingtimeo)