Write a process to dynamically call the queried field and the string value in the where condition, where string

Source: Internet
Author: User

Write a process to dynamically call the queried field and the string value in the where condition, where string

Take the emp table in scott user as an example to compile a stored procedure to query the values of a field in the emp table. However, the fields to be queried are not sure, and the strings passed in the where condition are also not sure.

DECLARE   v_a       VARCHAR2(20)   := 'sal';   v_sql     VARCHAR2(200);   TYPE tab_org_name     IS TABLE OF VARCHAR2(50);   v_org_name_tab        tab_org_name;BEGIN   v_sql := 'SELECT '|| v_a ||' FROM emp';   dbms_output.put_line(v_sql);   EXECUTE IMMEDIATE v_sql BULK COLLECT INTO v_org_name_tab;   FOR i IN 1 .. v_org_name_tab.count LOOP      dbms_output.put_line(v_org_name_tab(i));   END LOOP;END;


If there is a where condition, pay attention to the application of single quotes when the where condition contains strings. The correct code is as follows:

DECLARE   v_a       VARCHAR2(20)   := 'sal';   v_info    VARCHAR2(20)   := '''CLERK''';    v_sql     VARCHAR2(200);   TYPE tab_org_name     IS TABLE OF VARCHAR2(50);   v_org_name_tab        tab_org_name;BEGIN   v_sql := 'SELECT '|| v_a ||' FROM emp WHERE emp.JOB = '||v_info;   dbms_output.put_line(v_sql);   EXECUTE IMMEDIATE v_sql BULK COLLECT INTO v_org_name_tab;   FOR i IN 1 .. v_org_name_tab.count LOOP      dbms_output.put_line(v_org_name_tab(i));   END LOOP;END;

The execution result is as follows:


Zookeeper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.