oracle 接受PLSQL的条件输入

l0oc07j2  于 2023-03-22  发布在  Oracle
关注(0)|答案(2)|浏览(111)

在SQL Developer中使用Oracle SQL:
我正在尝试将我的团队用来查找信息以进行验证的几个查询打包在一起。我有一个查询,它返回一堆关于给定客户的信息。返回的列将始终相同(cust_id,name,zip_cd),但改变的是用户搜索客户的方式。有时我们可以直接搜索客户ID,有时它只是name和zip_cd。
有没有一种方法可以创建一个单一的查询,它提示用户如何搜索(即通过cust_id或通过名称和zip_cd),然后提示用户输入cust_id或名称和zip_cd,这取决于他们以前的输入?
此外,理想情况下,我会喜欢这是显示在结果窗格中像一个正常的查询,但最好的解决方案,我可以想出到目前为止是使用光标。如果你有任何输入,我如何可以做到这一点,将不胜感激!
这和我的第一个查询类似:

SET SERVEROUTPUT ON
ACCEPT choice_prompt PROMPT 'Enter seach criteria: (1) cust_id (2) name and zip_cd: ';
ACCEPT cust_id_prompt PROMPT 'Enter cust_id: '
ACCEPT cust_name_prompt PROMPT 'Enter customer name: '
ACCEPT cust_zip_prompt PROMPT 'Enter customer zip cd: '
BEGIN
    IF &choice_prompt = '1' THEN 
        dbms_output.put_line('cust_id'||chr(9)||'cust_nm'||chr(9)||'cust_zip');
        FOR r_product IN (
            SELECT a.cust_id, c.cust_nm, b.cust_zip 
            FROM customers.cust_id a 
            JOIN customers.cust_addr b ON a.xref_id = b.xref_id 
            JOIN customers.cust_nm c ON a.xref_id = c.xref_id  
            WHERE a.cust_id = &cust_id_prompt
        )
        LOOP
            dbms_output.put_line( a.cust_id ||chr(9)|| c.cust_nm || chr(9) || b.cust_zip );
        END LOOP;
    ELSIF &choice_prompt = '2' THEN
        dbms_output.put_line('cust_id'||chr(9)||'cust_nm'||chr(9)||'cust_zip');
        FOR r_product IN (
            SELECT a.cust_id, c.cust_nm, b.cust_zip 
            FROM customers.cust_id a 
            JOIN customers.cust_addr b ON a.xref_id = b.xref_id 
            JOIN customers.cust_nm c ON a.xref_id = c.xref_id  
            WHERE b.cust_zip = &cust_zip_prompt AND c.cust_nm = &cust_name_prompt
        )
        LOOP
            dbms_output.put_line( a.cust_id ||chr(9)|| c.cust_nm || chr(9) || b.cust_zip );
        END LOOP;
    ELSE
        dbms_output.put_line('There were errors.');
    END IF;
END;

但是它总是提示我所有的东西,而不是只提示我需要的东西。所以我试着把ACCEPT PROMPT嵌入到相关的选项中,但是不能让它工作:

SET SERVEROUTPUT ON
ACCEPT choice_prompt PROMPT 'Enter seach criteria: (1) cust_id (2) name and zip_cd: ';
BEGIN
    IF &choice_prompt = '1' THEN 
        ACCEPT cust_id_prompt PROMPT 'Enter cust_id: '
        dbms_output.put_line('cust_id'||chr(9)||'cust_nm'||chr(9)||'cust_zip');
        FOR r_product IN (
            SELECT a.cust_id, c.cust_nm, b.cust_zip 
            FROM customers.cust_id a 
            JOIN customers.cust_addr b ON a.xref_id = b.xref_id 
            JOIN customers.cust_nm c ON a.xref_id = c.xref_id  
            WHERE a.cust_id = &cust_id_prompt
        )
        LOOP
            dbms_output.put_line( a.cust_id ||chr(9)|| c.cust_nm || chr(9) || b.cust_zip );
        END LOOP;
    ELSIF &choice_prompt = '2' THEN
        ACCEPT cust_name_prompt PROMPT 'Enter customer name: '
        ACCEPT cust_zip_prompt PROMPT 'Enter customer zip cd: '
        dbms_output.put_line('cust_id'||chr(9)||'cust_nm'||chr(9)||'cust_zip');
        FOR r_product IN (
            SELECT a.cust_id, c.cust_nm, b.cust_zip 
            FROM customers.cust_id a 
            JOIN customers.cust_addr b ON a.xref_id = b.xref_id 
            JOIN customers.cust_nm c ON a.xref_id = c.xref_id  
            WHERE b.cust_zip = &cust_zip_prompt AND c.cust_nm = &cust_name_prompt
        )
        LOOP
            dbms_output.put_line( a.cust_id ||chr(9)|| c.cust_nm || chr(9) || b.cust_zip );
        END LOOP;
    ELSE
        dbms_output.put_line('There were errors.');
    END IF;
END;

然后我尝试只使用输入变量,并执行,但它仍然提示一切,而不是只需要什么:

SET SERVEROUTPUT ON
ACCEPT choice_prompt PROMPT 'Enter seach criteria: (1) cust_id (2) name and zip_cd: ';
DECLARE
    enter_cust_id number;
    enter_cust_name varchar2(20);
    enter_cust_zip_cd varchar2(5)
BEGIN
    IF &choice_prompt = '1' THEN
        dbms_output.put_line('cust_id'||chr(9)||'cust_nm'||chr(9)||'cust_zip');
        FOR r_product IN (
            SELECT a.cust_id, c.cust_nm, b.cust_zip 
            FROM customers.cust_id a 
            JOIN customers.cust_addr b ON a.xref_id = b.xref_id 
            JOIN customers.cust_nm c ON a.xref_id = c.xref_id  
            WHERE a.cust_id = &enter_cust_id
        )
        LOOP
            dbms_output.put_line( a.cust_id ||chr(9)|| c.cust_nm || chr(9) || b.cust_zip );
        END LOOP;
    ELSIF &choice_prompt = '2' THEN
        dbms_output.put_line('cust_id'||chr(9)||'cust_nm'||chr(9)||'cust_zip');
        FOR r_product IN (
            SELECT a.cust_id, c.cust_nm, b.cust_zip 
            FROM customers.cust_id a 
            JOIN customers.cust_addr b ON a.xref_id = b.xref_id 
            JOIN customers.cust_nm c ON a.xref_id = c.xref_id  
            WHERE b.cust_zip = &enter_cust_zip_cd AND c.cust_nm = &enter_cust_name
        )
        LOOP
            dbms_output.put_line( a.cust_id ||chr(9)|| c.cust_nm || chr(9) || b.cust_zip );
        END LOOP;
    ELSE
        dbms_output.put_line('There were errors.');
    END IF;
END;
pcww981p

pcww981p1#

如果您只是在SQL Developer中使用替换变量按原样运行查询,它将自动提示您填充它们。根本不需要使用像ACCEPT ... PROMPT这样的脚本命令。一旦您这样做,您就只能从脚本中输出文本,并且无法使用正常的结果工作表或其他结果集工具。

SELECT a.cust_id, c.cust_nm, b.cust_zip 
  FROM customers.cust_id a 
  JOIN customers.cust_addr b ON a.xref_id = b.xref_id 
  JOIN customers.cust_nm c ON a.xref_id = c.xref_id  
 WHERE a.cust_id = &enter_cust_id;

SELECT a.cust_id, c.cust_nm, b.cust_zip 
  FROM customers.cust_id a 
  JOIN customers.cust_addr b ON a.xref_id = b.xref_id 
  JOIN customers.cust_nm c ON a.xref_id = c.xref_id  
 WHERE b.cust_zip = &enter_cust_zip_cd AND c.cust_nm = &enter_cust_name;

您可以将这些查询保存在同一个文件中,只需要选择并运行您特别需要的查询,您就可以在正常的结果窗格中获得所需的结果,而不必处理文本输出(同样,DBMS_OUTPUTreally 并不是为了您使用它的目的而设计的)。(绿色箭头)来运行特定的命令(所以不需要注解掉),而不是“运行脚本(F5)”按钮。
也许这就是您已经在做的事情,在这种情况下,SQL Developer中真的没有太多的事情要做。正如已经指出的那样,PL/SQL并不是您可以用来构建用户界面的那种语言。它更多的是构建一个API来支持用其他语言编码的UI。如果您需要一个真正的用户界面,请查看Python或Oracle Application Express(APEX),这是Oracle数据库附带的低代码/无代码解决方案。

a2mppw5e

a2mppw5e2#

一般来说,PL/SQL对于需要与用户交互的东西来说并不好-像Python这样的东西会更容易使用。但是对于这种情况,你可能不需要..你能做一些像下面这样的事情吗?

ACCEPT cust_id_prompt PROMPT 'Enter cust_id: '
ACCEPT cust_name_prompt PROMPT 'Enter customer name: '
ACCEPT cust_zip_prompt PROMPT 'Enter customer zip cd: '

SELECT a.cust_id, c.cust_nm, b.cust_zip 
FROM customers.cust_id a 
JOIN customers.cust_addr b ON a.xref_id = b.xref_id 
JOIN customers.cust_nm c ON a.xref_id = c.xref_id  
WHERE (a.cust_id = &cust_id_prompt or '&cust_id_prompt' is null)
AND ((b.cust_zip = &enter_cust_zip_cd and c.cust_nm = &enter_cust_name) or '&enter_cust_zip_cd' is null)
AND ('&cust_id_prompt' is not null or '&enter_cust_zip_cd' is not null);

基本上,如果我提供输入,就用它来搜索。最后一行就是在那里,以确保您不会为所有内容输入空格,并返回整个表。
使用Python等还有其他好处(更容易对代码进行单元测试,更好的用户界面等)-但如果您正在寻找一个留在SQL*PLUS中的快速而肮脏的解决方案,这可能会让您达到目的。

相关问题