是否可以将变量的值连接到ACCEPT提示文本中?

时间:2019-06-20 18:56:37

标签: sql oracle plsql oracle-sqldeveloper

我正在尝试在oracle开发人员的接受提示框中添加变量的值

ACCEPT  confirm  char  PROMPT 'Are you sure you want to delete org_name ?  ' ||org_name ;    
SET SERVEROUTPUT ON
SET VERIFY OFF
VARIABLE org_name VARCHAR2(100);
VARIABLE STATUS VARCHAR2(100);
ACCEPT p_orgid CHAR PROMPT 'Enter organization ID: ';

这是我的代码:

    DECLARE
        orgname     VARCHAR(200);  
    BEGIN
        SELECT
            name
        INTO
            :org_name
        FROM
            table
        WHERE
            o_id = '&p_orgid';
        IF
            SQL%found
        THEN
            dbms_output.put_line('Are you sure you want to delete ( '
            || :org_name
            || ' ) ? ');
            :STATUS :=  :org_name || ' has been deleted';  
        END IF;   
    EXCEPTION
        WHEN no_data_found THEN
            dbms_output.put_line('No Data Found ');     
    END;
    /


ACCEPT  confirm  char  PROMPT 'Are you sure you want to delete org_name ?  ' ||org_name ;    

返回的org_name应该显示在提示框中,“接受”提示似乎不接受变量值。

2 个答案:

答案 0 :(得分:0)

我可以向您展示如何在SQL * Plus中做到这一点。看看是否可以在SQL Developer中使用它。

出于测试目的,我在Scott的架构中创建了一个测试表,

SQL> create table test as select * from dept;

Table created.

脚本看起来像这样(我将其命名为“续”,如果...则继续执行脚本):

select * from test;

prompt Which deptno do you want to delete?
accept p_deptno prompt 'Deptno : ';

set verify off;
set heading off;

select 'Are you sure you want to delete ' || d.dname || '? '
from test d
where d.deptno = &p_deptno;

accept odg prompt 'Y / N : ' ;

set termout off;
set pause off;
set echo off;
column a newline;
spool c:\temp\odg.sql

select decode(upper('&odg'), 'Y', 'prompt Executing ...',
                                  'prompt Stopped'
              ),
       decode(upper('&odg'), 'Y', '', 'accept return_key prompt ''Press <Return>''') a,
       decode(upper('&odg'), 'Y', '', 'exit') a
from dual;

spool off;
set termout on;

@c:\temp\odg       

delete from test where deptno = &p_deptno;

select * from test;

accept return_key prompt 'Press <Return>'
exit;

让我们看看它是如何工作的:

SQL> @cont

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

Which deptno do you want to delete?
Deptno : 20

Are you sure you want to delete RESEARCH?

Y / N : y
Executing ...

1 row deleted.


        10 ACCOUNTING     NEW YORK
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

Press <Return>
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

c:\Temp>

在SQL Developer中,您将以以下方式运行

c:\temp\cont.sql 

(您会将其写到工作表中)并作为脚本执行。 SQL Developer将提示您输入参数。在“脚本输出”窗口中观看执行情况。

答案 1 :(得分:0)

您还可以使用NEW_VALUE将:org_name(绑定变量)转换为&nv(替代变量),如下所示。替换变量在ACCEPT字符串中识别。

Reference

.
.
EXCEPTION
    WHEN no_data_found THEN
        dbms_output.put_line('No Data Found ');     
END;
/

COLUMN mybvcol NEW_VALUE nv NOPRINT
SELECT :org_name mybvcol FROM dual;

ACCEPT confirm  char  PROMPT 'Are you sure you want to delete org_name "&nv"?';