当前位置:网站首页>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
边栏推荐
猜你喜欢

锐捷x32pro刷openwrt开启无线160MHz

“数字化转型,数据先行”,谈谈数据治理对企业来说到底有多重要

NaturalSpeech模型合成语音在CMOS测试中首次达到真人语音水平
![[CEPH] CEPH configuration source code analysis | common/config*](/img/d6/ba8d3ccc28d11e7fd9662b730fd998.jpg)
[CEPH] CEPH configuration source code analysis | common/config*

Metadata management, the basic construction of enterprises in the digital era

Wechat applet, get the current page and judge whether the current page is a tabbar page

当前有哪些主流的全光技术方案?-下篇

数据可视化入门学习,要警惕陷阱误区,把握关键节点

Huawei cloud Kunpeng devkit code migration practice

基础提升---树形DP补充
随机推荐
[QNX hypervisor 2.2 user manual] 3.2.2 VM configuration example
C语言---1 C语言认知
企业管理者的质疑,这么多年的信息化,我们的钱花哪去了?
数字化时代,企业为什么要做数字化转型?
三部曲套路解bob活命问题
当前有哪些主流的全光技术方案?-下篇
华为云HCDE上云之路第二期:华为云如何助力制造业中小企业数字化转型?
Stream生成的3张方式-Lambda
How to realize the management "desire" of Business Intelligence BI service objects and enterprise managers?
不确定性推理:让模型知道自己不知道
关于YUV格式的一些总结
Linked List
MySQL index invalidation scenario
作为程序员,对于底层原理真的有那么重要吗?
Data URL
【QNX Hypervisor 2.2 用户手册】3.2.1 VM配置语法
Qtablewidget / qtableview practical tips
[Interface tutorial] how does easycvr set platform cascading through the interface?
商业智能BI的服务对象,企业管理者的管理“欲望”该如何实现?
低碳数据中心建设思路及未来趋势