PL/SQL dynamic dynamic SQL SELECT block vs

Hello

I have a question about the best way to encode a dynamic SELECT INTO statement. Here are the 2 posiibilities I know:


_1. Dynamically, the execution of the SELECT statement and making use of the INTO clause the EXECUTE IMMEDIATE statement_

FUNCTION to CREATE or REPLACE get_num_of_employees (p_loc VARCHAR2, VARCHAR2 p_job)
RETURN NUMBER
IS
v_query_str VARCHAR2 (1000);
v_num_of_employees NUMBER;
BEGIN
v_query_str: = 'SELECT COUNT (*) FROM emp_.
|| p_loc
|| ' WHERE employment =: bind_job';
V_query_str EXECUTE IMMEDIATE
IN v_num_of_employees
Using p_job;
RETURN v_num_of_employees;
END;
/


_2. Encapsulation of the SELECT INTO statement in a block and dynamically exectuting the gabarit@sur

FUNCTION to CREATE or REPLACE get_num_of_employees (p_loc VARCHAR2, VARCHAR2 p_job)
RETURN NUMBER
IS
v_query_str VARCHAR2 (1000);
v_num_of_employees NUMBER;
BEGIN
v_query_str: = ' start
"SELECT INTO COUNT (*): into_bind FROM emp_"
|| p_loc
|| ' WHERE employment =: bind_job;
end;';
V_query_str EXECUTE IMMEDIATE
With the HELP of v_num_of_employees, p_job;
RETURN v_num_of_employees;
END;
/


I was wondering which way would be best? I know that the second method uses a variable binding for the INTO clause, but one also uses link varialbes (No. semicolon)? Differences in terms of efficiency or speed?

Thank you very much

Edited by: BYS2 October 19, 2011 01:23

SELECT Dynamics also links the output - it no worries.

The dynamic PL/SQL block would result in a change in additional context of PL/SQL for SQL inside the block. It would be a very slight overhead.

I'd go for the SELECT dynamic - I see no reason to associate a PL/SQL block.

Tags: Database

Similar Questions

  • SQL SELECT statement as a parameter in the procedure of string

    Hi all

    I'm new to PL/SQL and I must get a procedure as parameter select SQL select statement as a VARCHAR and the production output the result of the statement with a few changes. Can someone help me solve this problem?

    A possibility I thought at was to use a REF CURSOR. The following example works but binds me to a specific table (in the example of the "customers" table). However, I need the ability to specify arbitrary tables in the select statement parameter.
    SET SERVEROUTPUT ON
    DECLARE
      TYPE my_cur IS REF CURSOR;
      query_cursor my_cur;
    BEGIN
      OPEN query_cursor FOR 'SELECT * FROM customers';
      DECLARE
        record_type customers%ROWTYPE;
      BEGIN
         LOOP
            FETCH query_cursor INTO record_type;
            EXIT WHEN query_cursor%NOTFOUND;
            DBMS_OUTPUT.PUT_LINE('some output ... ');
         END LOOP;
      END;
      CLOSE query_cursor;
    END;
    /
    My idea was to modify this example to work in the following code, but it does not work (I get a compile error):
    SET SERVEROUTPUT ON
    DECLARE
      TYPE my_cur IS REF CURSOR;
      query_cursor my_cur;
    BEGIN
    
      OPEN query_cursor FOR 'SELECT * FROM customers';
    
      DECLARE
        record_type query_cursor%ROWTYPE;
      BEGIN
         LOOP
            FETCH query_cursor INTO record_type;
            EXIT WHEN query_cursor%NOTFOUND;
            DBMS_OUTPUT.PUT_LINE('some output ... ');
         END LOOP;
      END;
      
      CLOSE query_cursor;
    
    END;
    /
    Does anyone has an idea how can I solve my problem?

    Thank you very much!
    (I'm using Oracle 11 g)

    This can help.
    Re: Dynamic Extraction on dynamic Sql

  • LV 2009 bug - dynamic distribution selection dialog does not have the VI forward, if it is already open

    In 2009 of LV, if you double-click an implementation in the dynamic distribution selection dialog and VI is already open, it will not be credited toward the front. It worked correctly in earlier versions.

    Have a look at the attached draft for an example.

    Please try to drop the VI attached on top of program NIUninstaller Instruments\LabVIEW XXXX\resource\Framework\Providers\LVClassLibrary\ChooseImplementation\CLSUIP_OpenImplementations.vi and let me know if it works for you.

  • A loop using the SQL select query.

    Hello

    I wanted to know, if the concept of loop is possible thanks to the SQL select statement.

    For example
    =======

    Table T1

    Col1 Col2
    Slim 10

    I want to write a query in such a way, so that when I get the result of the query, I format below.

    Col1 Col2
    Slim 10
    Slim 10
    Slim 10

    Basically what I'm trying to achieve is, I have a table with two columns and it has a single line displayed above.

    I want to display this line several times as the output of my SELECT query.

    Let me know, if this target is achievable.

    Kind regards
    Saurabh

    Like this?

    SQL> SELECT 10 col1, 'Sourabh' col2
      2    FROM DUAL
      3  CONNECT BY LEVEL <= 3;
    
          COL1 COL2
    ---------- -------
            10 Sourabh
            10 Sourabh
            10 Sourabh
    
    SQL> 
    
  • What is the sql SELECT statement to list all service names in the database?

    What is the sql SELECT statement to list all service names in the database?

    I tried
    SELECT name FROM dba_services
    But a long with valid service, he list also some other documents that are not the names of services.

    Hello

    You can try this query:

    select name, value from v$parameter where name = 'service_names';
    

    Hope this helps.
    Best regards
    Jean Valentine

  • Strategic review of using Pro * C package vs embedded SQL Select

    Hello
    I have an old application package.procedure that I'm looking using proC call to get the necessary data through Oracle Package.Procedure. Real call to the package see below, 1 parameter, released as a REF Cursor, SQL Select is simple-nothing fancy.

    I was wondering what might be the benefits (if any) to do that instead of coding real SQL Select into my proC code?
    In the case of any change, you need to change the proC and body package, rather than just do it in place if used directly?

    Source control, PM, nice structure inside of the Oracle, departmental approach just? Everyone at least one list?

    Appreciate your opinion, thank you all.



    Trent

    ----------------
    DECLARE
    number of in_idryba2;
    SIA OUT_LIST. UP_CRUD_ryba. SOR_LIST;
    RLIST SIA.t_ryba%ROWTYPE;
    BEGIN
    IN_IDryba2: = 2;
    SIA. UPK_CRUD_ryba. USP__GETLIST_01 (in_idryba2 = > in_idryba2, OUT_LIST = > OUT_LIST);
    LOOP
    EXTRACT THE OUT_LIST IN RLIST;
    WHEN THE EXIT OUT_LIST % NOTFOUND;
    DBMS_OUTPUT. PUT_LINE ("ID ='|") RLIST.ID_ryba |' DESCRP =' | RLIST. SRIPT);
    END LOOP;

    END;

    Hello

    This is not so much a direct answer to your question, but this thread AskTom discusses some of the advantages to using code in the database and the client.

    http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:30732069210515

    If search you around you can find several other discussions about the same thing.

    The centralization and reusability are two factors that lead me to focus on the code that is stored in the database.

    Kind regards

    Mark

  • HTML form in a "PL/SQL (anonymour block).

    Hello

    I need a little guidance ugent

    I create a "form" in a "PL/SQL (anonymour block). The requirement is to show what looks like an HTML form as you build the code

    The problem is that I'm "Place Setting" treatment of the APEX which is wwv_flow.accept... I've added an example below...

    Any help very good home

    Thank you

    Pete

    ______________________________________________________________________________________________

    HTP.PRN('<!) DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional / / IN".
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
    (< html xmlns = "http://www.w3.org/1999/xhtml" > ');

    HTP. PRN ("< style type =" text/css"> '");

    HTP. PRN ('#form {do-family: "Trebuchet MS", Verdana, without serif; width: 25em ;}');})
    HTP. PRN ('h2 {margin: 0 0 0 0; padding: 0 ;}}) ") ;
    HTP. PRN ('p {margin: 0 0 1em 0; padding: 0; do-size: 90 %} ""});
    HTP. PRN ('fieldset {background: #C361D2; border: none; margin-bottom: 1em; width: 24em; padding-top: 1.5em}');

    HTP. PRN ('p.legend {background: #DED983;})

    color: black;
    Padding: .2em .3em;
    Width: 750px;
    do-size: 18px;
    border: 6px start #DED980;
    position: relative;
    margin:-2em 0 1em 1em;
    Width: 20em ;} ") ;

    HTP. PRN ('fieldset {margin-bottom: 1em; width: 66em; padding-top: 1.5em ;}}) ") ;
    HTP. PRN ('#company {background: #F3B4F5; border: departure #F3B4F5; width = "700" ;}}) ") ;
    HTP. PRN ('#company tag {position: absolute;})
    do-family: arial;
    do-size: 16px;
    Padding: .2em ;} ") ;

    HTP. PRN (' input {margin-left: 9è; margin-bottom: .2em; line-height: 1.4em ;}}) ") ;
    HTP. PRN (' #message1 {background: #a3B4F5; border: departure #a3B4F5; width = "700" ;}}) ") ;
    HTP. PRN (' #message2 {background: #c3B4F5; border: departure #c3B4F5; width = "700" ;}}) ") ;

    HTP. PRN ('button1 {make: 48px "Trebuchet MS", "Verdana", sans-serif;})
    background: #F0888A;
    {border: start #6EC6F1} ");"

    HTP. PRN (' input #buttons1 {background: #DED983;})
    {font: 1.2em "Trebuchet MS", Verdana, without serif} ");"

    HTP. PRN ('p #buttons1 {white-space: nowrap} "");

    HTP. PRN ("</style > '");


    HTP. PRN ("< table width ="760"> < tr bgcolor ="#D5EAE9"> ');

    HTP. PRN ("< BR > < BR > '");

    HTP. PRN ("< form method =" "action =" "> ');

    HTP. PRN ("< fieldset id ="company"> < class p ="legend"> < /p > company '");

    HTP. PRN ("< label > Comapany name: < / label > < input name ="company"type ="Text"size ="30"/ >");

    HTP. PRN ("< br > < br >");
    HTP. PRN ("< / fieldset > '");

    HTP. PRN ("< br > < br > < br >");
    HTP. PRN ("< fieldset id ="message1"> < class p ="legend"> < /p > Message'");
    HTP. PRN ("< / fieldset > '");
    HTP. PRN ("< br > < br > < br >");
    HTP. PRN ("< fieldset id ="message recu2"> < class p = 'legend' > two Message < /p > '");
    HTP. PRN ("< / fieldset > '");
    HTP. PRN ("< br > < br >");

    HTP. PRN ("< / make >");
    HTP. PRN ("< /tr > < / table > '");
    End;

    ______________________________________________________________________________________________________

    Pete:

    Delete the name attributes of all the entry elements defined by the process of pl/sql. For example
    should be replaced byor
    APEX accept process recognizes a predefined set of HTML form entry name. No matter which entry with a name not this game will result in the failure of the process accept. F01 through f50 are valid names for the acceptance procedure.

    CITY

  • NULL in PL/SQL SELECT statement..

    Hi all

    I need to use as a result of a SELECT statement in my function to get PCODE from the table. Cur_rec here. CODE comes from cursor that runs before the declaration.

    SELECT PCODE in v_pcode TABLE_XYZ where CODE = cur_rec. CODE and PCODE = 'VIEW '.


    Now the problem is the part of the CODE has all the information in the table TABLE_XYZ. In this case, it triggers an error NO_DATA_FOUND. To solve it, I can put a part of the EXCEPTION and handle it. But I think in another way...

    If there is no data for a CODE, the default value of v_pcode would be 'No Code'. I tried to use the NVL function, but it does not work here as a SQL SELECT statement.

    Can someone give an idea how to achieve this?

    Hello

    declare
      ...
      v_cnt number;
    begin
      SELECT count(*)
           into v_cnt
          from TABLE_XYZ
        where CODE = cur_rec.CODE
           and PCODE='SEE';
      if v_cnt=0 then
        v_pcode = 'No code';
      else
        v_pcode = 'SEE';
      end if;
    end;
    

    Bartek

  • the selection of the PL/SQL nested block table error...

    SQL > create type string_table is the table of the varchar (100);
    2.

    Type of creation.



    declare
    v_names string_table: = string_table();
    Start
    v_names. EXTEND (3);
    v_names (1): = "Name1"
    v_names (2): = "Name2";
    v_names (3): = 'name3 ';
    dbms_output.put_line (v_names (1));
    dbms_output.put_line (v_names (2));
    dbms_output.put_line (v_names (3));
    dbms_output.put_line (v_names. COUNT());
    Select * from table (v_names);
    end;
    /




    Select * from table (v_names);
    *
    ERROR on line 12:
    ORA-06550: line 12, column 7:
    PLS-00428: an INTO clause in this SELECT statement

    Select * from table (v_names);

    I guess, here you have tried to put the contents of the NT NT one another, or simply trying to print it.
    But I don't think that INTO Clause is mandatory here.

    Please check your modified code (w/o INTO) and the output:

    DECLARE
       TYPE string_table IS TABLE OF VARCHAR (100);
    
       v_names   string_table := string_table ();
       v_test    string_table := string_table ();
    BEGIN
       v_names.EXTEND (3);
       v_names (1) := 'name1';
       v_names (2) := 'name2';
       v_names (3) := 'name3';
       DBMS_OUTPUT.put_line ('Old collection - '||v_names (1));
       DBMS_OUTPUT.put_line ('Old collection - '||v_names (2));
       DBMS_OUTPUT.put_line ('Old collection - '||v_names (3));
       DBMS_OUTPUT.put_line ('Old collection - '||v_names.COUNT ());
    
       DBMS_OUTPUT.put_line (CHR(10));
    
       /* SELECT * FROM TABLE (v_names); */
       v_test := v_names;
       DBMS_OUTPUT.put_line ('New collection -- '||v_test (1));
       DBMS_OUTPUT.put_line ('New collection -- '||v_test (2));
       DBMS_OUTPUT.put_line ('New collection -- '||v_test (3));
       DBMS_OUTPUT.put_line ('New collection -- '||v_test.COUNT ());
    
       DBMS_OUTPUT.put_line (CHR(10));
    
       /* Printing using FOR LOOP */
       FOR i IN v_test.FIRST..v_test.LAST
       LOOP
         DBMS_OUTPUT.put_line ('In FOR Loop --- '||v_test (i));
       END LOOP;
    
    EXCEPTION
       WHEN OTHERS
       THEN
          DBMS_OUTPUT.put_line ('Error ' ||SQLERRM|| DBMS_UTILITY.format_error_backtrace);
    END;
    

    gives o/p:

    Old collection - name1
    Old collection - name2
    Old collection - name3
    Old collection - 3
    
    New collection -- name1
    New collection -- name2
    New collection -- name3
    New collection -- 3
    
    In FOR Loop --- name1
    In FOR Loop --- name2
    In FOR Loop --- name3
    

    See this link - http://docs.oracle.com/cd/E11882_01/appdev.112/e17126/tuning.htm#CIHGGBGF

    Published by: Vanessa B on 26 December 2012 14:29
    -modified code

    Published by: Vanessa B on 26 December 2012 14:45
    LOOP FOR-updated - added 'again' code

  • Dynamically by selecting only the two highest values

    Hello. I have the following query:
      1  select distinct quarter
      2  from random_selection
    SQL> /
    
    QUARTER
    ------------
    114
    121
    113
    112
    I would like to know if it is possible to modify this query so that it dynamically returns the value of 'quarter' for only two values (higher) the most recent quarter. In the case above, I want him back just 121 and 114.

    Is this possible?

    Hello

    Of course, it is possible.
    One way is to use ROWNUM:

    WITH  in_descending_order     AS
    (
         SELECT DISTINCT  quarter
         FROM             random_selection
         ORDER BY      quarter     DESC
    )
    SELECT  quarter
    FROM     in_descending_order
    WHERE     ROWNUM     <= 2
    ;
    

    Another way is to use DENSE_RANK:

    WITH  got_r_num     AS
    (
         SELECT DISTINCT  quarter
         ,                 DENSE_RANK () OVER (ORDER BY quarter DESC)     AS r_num
         FROM             random_selection
    )
    SELECT  quarter
    FROM     got_r_num    -- Corrected
    WHERE     r_num     <= 2
    ;
    

    DENSE_RANK requires a bit more code, but it is more flexible. For example, you can easily display to find the last two quarters for each account.

    Published by: Frank Kulash, Sep 16, 2011 15:05
    Request corrected DENSE_RANK.

  • With the help of inplace column number of the column name in the SQL Select statement

    Is it possible to execute instructions select sql with the number of columns in
    place names of columns?

    Run SQL
    select AddressId,Name,City from Address
    Is it possible
    select 1,2,5 from Address
    Thanks in advance

    You can extract the names of the columns for user_tab_columns
    with

    SELECT Column_name
    FROM user_tab_columns
    where table_name = 'MY_TABLE'
    and Column_id in (1,2,5)
    

    Use dynamic SQL and then the result to get the values.

    But as others already mentioned, this wil it be given incorrect if your table is changed.

  • Region of HTLM apex 4.0 Refresh (sql/pl sql anonymous block)

    Is it possible to update this area of dynamic action through refreshment?

    No luck for me.

    Thank you

    Hello

    M don't know if pl/sql refreshment APEX 4.x area or not...

    But there is a work around to do the same thing to the point 3.2 and will work for 4.0...

    Instead of the region of PL/SQL create HTML Region and enter the source

    Then, through a combination of javascript and ondemand process... fill your div with content you want...

    Kind regards
    Shijesh

  • run in parallel sql statement block

    Hello
    I'm looking for a way to run 2 blocks from the sql statement at the same time in sqlplus

    Block1
    drop table CTSGLOB_TABLE;
    create the table CTSGLOB_TABLE select * from CTSGLOB_VIEW;
    create index TEMPS_CTSGLOB_IDX ON CTSGLOB_TABLE("TEMPS");
    create index ORGA_CTSGLOB_IDX ON CTSGLOB_TABLE("ORGA");
    create index RUBSRH_CTSGLOB_IDX ON CTSGLOB_TABLE("RUBSRH");

    Block2
    drop table CTSGLOB_TABLE_2;
    create the table CTSGLOB_TABLE_2 select * from CTSGLOB_VIEW_2;
    create index TEMPS_CTSGLOB_IDX_2 ON CTSGLOB_TABLE_2("TEMPS");
    create index ORGA_CTSGLOB_IDX_2 ON CTSGLOB_TABLE_2("ORGA");
    create index RUBSRH_CTSGLOB_IDX_2 ON CTSGLOB_TABLE_2("RUBSRH");

    I know that I can open 2 client session to run, but I would like to know another way.

    Best regards
    Jean-Marc

    Well, that depends on what you mean exactly when you say "run 2 blocks from the sql statement at the same time in sqlplus '...
    You can also copy all statements in a sql script and run that, just.
    If this isn't what you want, then please explain why.

  • Oracle SQL Developer 3.0: Debugging of PL/SQL anonymous blocks: ISSUES

    Hello
    I just downloaded the Oracle SQL Developer 3.0. I used EA versions because they have emerged and was happy to see the final version. So I immediately tried to debug an anonymous block (something I was not looking to do in EA versions) and nothing happened.

    The "Debug" is grayed out and the string 'ctrl-SHIFT-F10' key did nothing. I found this forum:
    Re: 30EA1: debugging of anonymous block?
    and Vadim Tropashko follow-up advice. This has nothing to my anonymous block but has worked well for a simple example.

    So I started to whittle my anonymous block to the bottom to find the culprit, here's a breaking point repeatable for me:
    declare
        stmt1 long;
        stmt2 long;
        stmt3 long;
        stmt4 long;
        p_data varchar2( 500 );
        i      varchar2( 10 );
    BEGIN
        STMT1 := 1;
        STMT2 := 1;
        STMT3 := 1;
        STMT4 := 1;
        
        --the moment this is in the block "Debug" is no longer an option
         select
            SendDocumentResult
           into
            p_data
           from
            XMLTABLE( '/data' 
                    PASSING 
                    xmltype.createxml( '<?xml version="1.0" encoding="utf-8"?><data><SendDocumentResult>test</SendDocumentResult></data>' ) 
                    COLUMNS SendDocumentResult varchar2( 1000 ) PATH 'SendDocumentResult' ) ;
    end;
    The moment that I SELECT INTO... XMLTABLE() it fails (a normal SELECT INTO works very well).

    Is this a problem with my environment or there's a problem with SQL Developer 3.0.04. Overlooking the comments of K, it seems that debugging has worked for 'simple' blocks, so I wonder if it's just out of reach...

    My environment:
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production     
    PL/SQL Release 11.2.0.1.0 - Production                                           
    CORE     11.2.0.1.0     Production                                                         
    TNS for IBM/AIX RISC System/6000: Version 11.2.0.1.0 - Production                
    NLSRTL Version 11.2.0.1.0 - Production
    
    and Oracle SQL Developer
    3.0.04 (Buld Main 04.34 with bundled Java) on a Window's XP box.

    The analysis has been the problem indeed. Can offer no alternative workaround, that packing horror XML in a view.

  • pl/sql anonymous block

    Hello
    I use the apex of oracle 10g 3.2

    I want to display some fields in a table format for which I use anonymous pl/sql block.
    I use the slider for that.

    declare
    News c is select...
    Start
    I'm in the loop c
    -----------
    HTP.p ();
    ---------
    end loop;
    end;

    Databasetable A has 3 columns.
    Name of the person, education, brands.

    Now, a person can have more than one education as

    A 12TH-90
    A 10-98
    B - b - 90

    Now if I just use a slider I row-by-row and I just use htp.p and display fields.

    Now what I want is whenever there are more then one entry for a person I want to view together
    that is in one table itself all the information should be there.

    p. ex. A - 10th, 12th - 98.90
    B - b - 90

    So in a page, you need only one table for each person. If there are more entries for a person in the db table,
    then I should the club upward?

    How can I reach it?

    Thank you

    Hello

    See aggregation Techniques String

    * 009 *.

Maybe you are looking for

  • canvas of handwriting

    I downloaded ISO 10 - I got the handwriting of landscape painting and then it wouldn't work or to go in this mode after some time to play with all the new features. I closed the app, restarted the phone and I still do not have the option more.

  • Sync lost my favorites

    Hello. I got a system down yesterday, a raid failure (a hard drive died of a raid 0 array), I reinstalled Windows 7 x 64, to a new hard disk (normal, not in raid or something like that). I installed Firefox 18 and I set up the sync account. Then I tr

  • IPod Shuffle Gen 2 will not load.

    I pod shuffle 2 generation does not load. Light does not illuminate. May be time for a new or repaired.

  • Re: Poor volume on my laptop satellite

    Hello I made a post recently on the volume on my laptop being bad.It gave me a link and from there was able to download the latest audio driver, but still, the volume is turnd upward in my volume control and everything what it turned up to the max in

  • Try to install the SP3 crashes my machine. Twice I had to do a system recovery. I don't want to do it again. What is going on?

    System settings: MS Windows XP The home Ed. Version2002 SP2 AMD Athlon 64 3400 +. 2.41 GHz 1.87 GB Ram No particular reason, why the attempt to update to SP3 blocks this system?  EPC now ongoing execution protection.  Not sure what worked in the past