在 SQL PL 中开发时,'set' 和 'select into' 有什么区别?
set var = (select count(1) from emp);
select count(1) into var from emp;
它们完全等效吗?我在哪里可以找到关于它们的文档?
发出选择时,它不返回任何值:
您可以检查这两个存储过程的区别:
使用集合:
create or replace procedure test1 (
in name varchar(128)
)
begin
declare val varchar(128);
set val = (select schemaname
from syscat.schemata where schemaname = name);
end @
使用选择进入
create or replace procedure test2 (
in name varchar(128)
)
begin
declare val varchar(128);
select schemaname into val
from syscat.schemata where schemaname = name;
end @
调用集
$ db2 "call test1('nada')"
Return Status = 0
调用 select into
$ db2 "call test2('nada')"
Return Status = 0
SQL0100W No row was found for FETCH, UPDATE or DELETE; or the result of a
query is an empty table. SQLSTATE=02000
这是两者的区别。使用 select into 时,您必须处理处理程序。
They are, to the best of my knowledge
In some cases, you would do one technique over the other ..
eg. You cannot use WITH UR in SET
SET var1=(selct....from t with ur)
but can do
select a into var1 from t with ur
声明的SELECT INTO
作品SELECT
。
您可以直接分配函数的结果、SET
进行计算或分配不同的变量。例如
SET var = var + 1;
SET var1 = var;