How to select into a variable in PL/SQL when the result might be null?

后端 未结 8 736
广开言路
广开言路 2020-12-07 14:08

Is there a way in to just run a query once to select into a variable, considering that the query might return nothing, then in that case the variable should be null.

8条回答
  •  臣服心动
    2020-12-07 14:49

    I know it's an old thread, but I still think it's worth to answer it.

    select (
            SELECT COLUMN FROM MY_TABLE WHERE ....
            ) into v_column
    from dual;
    

    Example of use:

    declare v_column VARCHAR2(100);
    begin
      select (SELECT TABLE_NAME FROM ALL_TABLES WHERE TABLE_NAME = 'DOES NOT EXIST')
      into v_column 
      from dual;
      DBMS_OUTPUT.PUT_LINE('v_column=' || v_column);
    end;
    

提交回复
热议问题