2

在 SQL PL 中开发时,'set' 和 'select into' 有什么区别?

set var = (select count(1) from emp);

select count(1) into var from emp;

它们完全等效吗?我在哪里可以找到关于它们的文档?

4

3 回答 3

3

发出选择时,它不返回任何值:

  • 选择进入抛出异常
  • set 得到一个空值

您可以检查这两个存储过程的区别:

使用集合:

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 时,您必须处理处理程序。

于 2014-06-22T11:51:34.233 回答
1

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 
于 2013-03-05T18:52:30.020 回答
-1

声明的SELECT INTO作品SELECT

您可以直接分配函数的结果、SET进行计算或分配不同的变量。例如

SET var = var + 1;
SET var1 = var;
于 2013-03-05T22:11:38.180 回答