ITPub博客

首页 > 数据库 > 国内数据库 > select into 时有无strict关键字的区别

select into 时有无strict关键字的区别

原创 国内数据库 作者:瀚高PG实验室 时间:2022-04-02 14:54:28 0 删除 编辑

瀚高数据库
目录
文档用途
详细信息

文档用途
介绍说明在select into语法中有无strict关键字的区别。

详细信息
一个产生单一行(可能有多个列)的 SQL 命令的结果可以被赋值给一个记录变量、行类型变量或标量变量行域列表。这通过书写基础 SQL 命令并增加一个INTO子句来达成。例如:

SELECT select_expressions INTO [STRICT] target FROM ...;

如果STRICT没有在INTO子句中被指定,那么target将被设置为该查询返回的第一个行,或者在该查询不返回行时设置为空。第一行之后的任何结果行都会被抛弃。你可以检查特殊的FOUND变量来确定是否返回了一行:

SELECT * INTO myrec FROM emp WHERE empname = myname;IF NOT FOUND THEN
    RAISE EXCEPTION 'employee % not found', myname;END IF;

如果指定了STRICT选项,该查询必须刚好返回一行或者将会报告一个运行时错误,该错误可能是NO_DATA_FOUND(没有行)或TOO_MANY_ROWS(多于一行)。如果你希望捕捉该错误,可以使用一个异常块,例如:

BEGIN
    SELECT * INTO STRICT myrec FROM emp WHERE empname = myname;
    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            RAISE EXCEPTION 'employee % not found', myname;
        WHEN TOO_MANY_ROWS THEN
            RAISE EXCEPTION 'employee % not unique', myname;END;

成功执行一个带STRICT的命令总是会将FOUND置为真。

测试实例:

create table strict_test_tb (id int,content varchar);create or replace function test_no_strict() returns varchar as $$
declare
  param1 strict_test_tb%rowtype;
  begin
 begin
    select * into param1 from strict_test_tb;
    EXCEPTION
  	  WHEN NO_DATA_FOUND THEN
    RAISE EXCEPTION 'param1 % not found', '1';
  	  WHEN TOO_MANY_ROWS THEN
    RAISE EXCEPTION 'param1 % not unique', '2';
      when others then
    RAISE exception 'other unkown exception';  
   end;
   RAISE notice 'param1 is %',param1;
   return null;end;  $$ language plpgsql;    create or replace function test_has_strict() returns varchar as $$
declare
  param1 strict_test_tb%rowtype;begin  
 begin
    select * into strict param1 from strict_test_tb;
    EXCEPTION
  	  WHEN NO_DATA_FOUND THEN
    RAISE EXCEPTION 'param1 % not found', '1';
  	  WHEN TOO_MANY_ROWS THEN
    RAISE EXCEPTION 'param1 % not unique', '2';
  end;
   raise notice 'param1 is %',param1;
   return null;end;$$ language plpgsql;

当没有数据被返回时:

highgo=# select * from strict_test_tb;
 id | content 
----+---------(0 rows)highgo=# select test_no_strict();NOTICE:  param1 is (,)
 test_no_strict 
---------------- (1 row)highgo=# select test_has_strict();ERROR:  param1 1 not found
CONTEXT:  PL/pgSQL function test_has_strict() line 10 at RAISE

当有一数据被返回时:

highgo=# select test_no_strict();NOTICE:  param1 is (1,1)
 test_no_strict 
---------------- (1 row)highgo=# select test_has_strict();NOTICE:  param1 is (1,1)
 test_has_strict 
-----------------(1 row)

当有多条数据被返回时:

highgo=# select test_no_strict();NOTICE:  param1 is (1,1)
 test_no_strict 
----------------
 (1 row)highgo=# select test_has_strict();ERROR:  param1 2 not unique
CONTEXT:  PL/pgSQL function test_has_strict() line 12 at RAISE

结论:

select into 当没有数据被返回时,返回null,当有多条数据被返回时,对变量赋值第一条。

select into strict 当没有数据被返回时,产生no_data_found异常;当有多条数据被返回时,产生too_many_rows异常。


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69994931/viewspace-2885541/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
瀚高PG实验室(Highgo PG Lab)依托于瀚高DBA运维团队及瀚高数据库PG内核研发团队,旨在深入研究PostgreSQL技术、使用技巧、内核探秘、PG教学等,并进行分享。欢迎大家关注、交流。

注册时间:2021-02-18

  • 博文量
    184
  • 访问量
    126058