PL/SQL to run FOREACH

I want to do something I think it's pretty simple, but I can't figure out how to put it in an Oracle procedure.

I want to selct a set of tables inspired AS condition in the name of the table, then remove it from the table where the column name is the name of the table with a "_ID" add the name and the column value is <>0.

In PERL this would be very similar to the following...

[create an array with all the table names \[assumes, you already have a db connection\]
@myTables = (SELECT DISTINCT tNom FROM tab WHERE tNom AS 'someCondition');

foreach my $myTabName (@myTables)
{
Print "RUN IMMEDIATELY"DELETE FROM". $myTabName. ' WHERE '. '. $myTabName. "_Id <>0'; \n » ;
}

Now, how you would do this is a stored procedure?

Try this new using the parameters...

create or replace procedure borrar2 (p_argument varchar2) is
--
lCadena varchar2 (2000);
lColumn varchar2 (50);
--
Start
--
for REC in (SELECT DISTINCT table_name
from all_tables
where table_name as SUPERIOR ('%' | p_argument |) ' %')) in a loop
--
Start
Select c.column_name
in lColumn
from all_tab_columns c
where table_name = rec.table_name
and c.column_id = 1;
lCadena: = "DELETE FROM". RECOMM. TABLE_NAME | 'WHERE ' | lColumn | ' = 0';
dbms_output.put_line ('Tabla:' | lCadena);
EXECUTE IMMEDIATE lCadena;
--
commit;
exception
While OTHERS then
null;
end;
end loop;
--
end;

Published by: hpuelman on August 28, 2008 17:43

Tags: Database

Similar Questions

  • Accept the user SQL and run batch

    Hi all

    I want to invite the user to enter their SQL and run it during the execution of a batch file.

    Results (after the running .bat file):

    Press any key to enter your sql

    -will be here sql user input

    You SQL statement has been executed successfully.

    Try:

    SET /P DB = [promptDBname]

    SET /P USER = [promptuserName]

    SET /P PWD = [promptPass]

    SET /P sqlText = [promptSQL]

    sqlText %Echo% > script.sql

    sqlplus %USER%/%PWD%@%DB% @script.sql

    Perversion!

    ----

    Ramin Hashimzade

  • SQL being run by a Session

    DB version: 10.2.0.4

    To get the SQL code running in a session, I usually get the SQL_ID v$ session for the scheman in question
    select sql_id from v$session where schemaname = 'HR'
    Then, I run
    select sql_Text from v$sql where sql_id = 
    But sometimes the SQL_ID will be null for the sessions. So, I would like to know if there is a better way to get the SQL being run by a session now.

    Dear Tom,

    Alternatively, you can join the fixed views together (v$ sql and v$ session).

    Can you please give an example of your statement on the SQL_ID null?

    Kind regards.

    Ogan

  • Exercise enough free SQL Server running?

    I took a course SQL and the pattern of their installation program has been a (sitting at the computer) User Management Studio connection, which in turn connects to the SQL translator, which is attached with DBMS SQL Server, which is connected to the SQL Server database.

    All I want to do a few exercises query, and I wonder if the free SQL Server Express development would be sufficient.  I had a whole CD of SQL files, including some set up tables for questioning.  I would like to be placed on my home computer, which runs Windows 7 Professional 64 bit.

    Hello

    The question you posted would be better suited in the MSDN Forums. I would recommend posting your query in the MSDN Forums:

    http://social.msdn.Microsoft.com/forums/en/category/SQLServer

  • Script Sql to run in the background

    Hi guys,.

    I will carry out two unix machine sql scripts by connecting through sqlplus in two sessions of PuTTY, I need to run the scripts background is there a way to run outside the sh script execution.

    SQL > @test.sql

    .

    .

    .

    Another session

    SQL > @test1.sql

    .

    .

    .

    actutally I need to run the sql in the background scripts so that my putty session will not cut

    Assuming that this is really what you want to do, use screen sessions.

    This puts the interactive session of ATS in the background, allowing you to reconnect to the it (another putty session, or of the same) to a later date to check the results manually intervene, etc.

    Install the screen on the server as root:

    yum-y install screen

    As the launch of PuTTY user session screen:

    screen

    Then start your session of sqlplus, script execution, etc.-, then press ALT-D to disconnect from the session.

    Start at any number of additional screen sessions.

    To list the sessions:

    Screen - ls

    To join your putty for a screen session session:

    session - dr

    Lose your PuTTY connection does not screen sessions you started. You can go home, in the Office VPN, mastic fire upwards and reconnect to sessions of your screen.

  • Dynamic Action run PL/SQL Code runs is not after 4.2.5 upgrade

    APEX 4.2.4 to 4.2.5 11.2 database upgrade

    I have a button to click on DA who owns shares run of PL/SQL Code and update point who worked in version 4.2.4 which is no longer running after the 4.2.5 patch.

    The PL/SQL procedure has been tested autonomous and it runs correctly.

    I tested him DA is actually shot on the click event by adding an alert "DA xxxx pulled" action before the execution of work of PL/SQL action and action alert.

    There is also a similar on DA click which makes the action opposite to the DA problem and it works both are configured in the same way:

    Static DA, run Code PL/SQL - check waiting for result, and not repress with the same Submit and return items.

    Any ideas on a possible cause/solution much appreciated

    Fixed it, the question was the Page elements of return of goods on the action of PL/SQL Code:

    If the target element was empty the action failed, by adding a value in the database through a normal SQL insert then the insert DA completed successfully.

    Removing Page elements at the point of return of the action of PL/SQL and together action update the target element after the DA fires in all scenarios.

    But thanks for the lead

  • How to get the SQL query running of af: search?

    I use JDeveloper 11.1.2.3.0. I have a page where I've set up an af:query and an array of result. The problem is that I can't get the exact query that is used during the execution of this component in a managed bean method. Is it possible to get the query string that is run within the af: query?

    Thank you

    Hello

    Method of the ViewObject getQuery() returns what you need;

    You can replace the executeQueryForCollection(), and prior to calling super, you can print the result of getQuery() method:

    public void executeQueryForCollection (rowset Object,

    Object [] params,

    {int noUserParams)

    System.out.println ("SQL =" + getQuery());

    call the super method here...

    }

  • Script SQL Developer runs all the lines

    I have a group of scripts that I want to execute in the order since a main script.  I created a file referencing these scripts as follows below.  My problem is that when I run this script (via F5 or another script referencing it), it runs only the first line and stops.  I have excluded questions with my references to file by changing the order - always the first runs and stops.  I have tried such as displayed below and with a semicolon; to separate each command.  I have scoured the web and can not find any reason why it does not work.  Each of the scripts listed below contains multiple statements separated by a; that wraps a file to a NAS where he is picked up by our external file transfer program.  It all works properly - the only thing that I have problems with using 1 script to call several files and run them in order.

    Any help is greatly appreciated!

    @EMSTATUS.sql

    @EMPLOYEE.sql

    @HREMPUSF.sql

    @HRHISTORY.sql

    @HRSUPER.sql

    @JOBCLASS.sql

    @JOBCODE.sql

    @PAACTREAS.sql

    @PAEMPLOYEE.sql

    @PAEMPPOS.sql

    @PCODES.sql

    @PCODESDTL.sql

    @PERSACTHST.sql

    @PRDISTRIB.sql

    @PRPAYCODE.sql

    @PSGRELATE.sql

    @REVIEW.sql

    Hello

    Note You can place in the path: Tools-> preferences-> database-> spreadsheet-> "path select default to search scripts"


    I tried the following:

    c:\tmp\calltwoscripts. SQL

    set echo on

    @zone.sql

    @ztwo.sql

    Select 'done' double.

    c:\tmp\zone. SQL:

    Select 'one' double;

    c:\tmp\ztwo. SQL:

    Select 'two' SQL;

    executed in a worksheet:

    @c:/tmp/calltwoscripts.sql

    Output:

    > @zone.sql

    > select "one" of the double

    'A '.

    -----

    one

    > @ztwo.sql

    > Select "both" of the double

    "TWO".

    -----

    two

    > select 'done' double

    'DONE '.

    ----------

    Finish

    The things that could go wrong:

    Your scripts could end with "quit".

    Paths can mingle: try (not @) and full of paths.

    You could hit a code issue, the spreadsheet does not recover-in which case you should get the output on the command line and or Page logging of messages in the view.

    Turloch

    SQLDeveloper - team

    http://www.thatjeffsmith.com/archive/2012/06/how-to-collect-Debug-Info-for-Oracle-SQL-Developer/

    http://totierne.blogspot.co.UK/2013/07/user-check-on-sqldeveloper-worksheet.html

  • sql statements run correctly

    Hello

    It is a question of certification.

    Question: 3
    Examine the structure of the table of PROGRAMS:
    Name Null? Type
    ---------- ------------- ---------------
    PROG_ID NOT NULL NUMBER (3)
    PROG_COST NUMBER (8.2)
    START_DATE DATE NULL NOT
    END_DATE DATE
    The two SQL statements would be run successfully? (Choose two.)
    A. SELECT NVL (ADD_MONTHS(END_DATE,1), SYSDATE) OF programs;
    B. SELECT TO_DATE (NVL (SYSDATE-END_DATE, SYSDATE)) OF programs;
    C. SELECT NVL (MONTHS_BETWEEN (start_date, end_date), 'Ongoing') OF programs;
    D. SELECT NVL (TO_CHAR (MONTHS_BETWEEN (start_date, end_date)), 'Ongoing') OF
    programs;

    Answers: A, D

    My question is:
    If A and D are correct woud B and C run successfully also right?

    Please specify.

    What? You are able to do this in your test db?
    You get an error for B and C for this reason - the expressions in the two queries will assess a number. B trying to do a TO_DATE on a NUMBER, and C tries to substitute a VARCHAR2 string where a NUMBER is expected.

  • PL/SQL to run the exe with the DB settings

    Hi all

    I have a little problem.

    I'm in the need to write that all connection, disconnection and connection fail data to the Windows application log. To do this, I already tried various solutions, but with my configuration (EE Oracle 10.2.0.4) and Windows Server 2003 R2 X 64 the only possible solution is (apparently) the following.

    I create a Table with the information I need:

    --------------------------------------

    CREATE TABLE logonaudittable
    (
    event VARCHAR2 (15).
    SID NUMBER,
    series # NUMBER,
    time DATE,
    name VARCHAR2 (30),
    osuserid VARCHAR2 (30),
    MachineName VARCHAR2 (64)
    )
    /
    --------------------------------------

    I have create a trigger for the connection data, one for logging for the logon fails:

    ------------------------------------------------

    LOGON trigger.

    Database of logonauditing AFTER logon ON CREATE OR REPLACE TRIGGER
    DECLARE
    MachineName VARCHAR2 (64);
    osuserid VARCHAR2 (30);
    v_sid NUMBER (10);
    v_serial NUMBER (10);

    CURSOR c1 IS
    SELECT sid, serial #, osuser, machine
    FROM v$ session WHERE audsid = userenv ('sessionid');
    BEGIN
    OPEN c1;
    EXTRACT the c1 IN v_sid, v_serial, osuserid, machinename;

    INSERT INTO logonaudittable VALUES ('CONNECT', v_sid, v_serial, sysdate,
    user, osuserid, machinename);

    CLOSE c1;
    END;
    /

    -----------------------------------------------

    Logoff trigger

    CREATE OR REPLACE TRIGGER logoffauditing
    BEFORE logoff ON database
    DECLARE
    MachineName VARCHAR2 (64);
    osuserid VARCHAR2 (30);
    v_sid NUMBER (10);
    v_serial NUMBER (10);

    CURSOR c1 IS
    SELECT sid, serial #, osuser, machine
    FROM v$ session WHERE audsid = userenv ('sessionid');
    BEGIN
    OPEN c1;
    EXTRACT the c1 IN v_sid, v_serial, osuserid, machinename;

    INSERT INTO logonaudittable VALUES ('LOGOUT', v_sid, v_serial, sysdate,
    user, osuserid, machinename);

    CLOSE c1;
    END;
    /

    -------------------------------------------------------

    Logon trigger FAILS

    CREATE OR REPLACE TRIGGER logonfailauditing
    AFTER ON SERVERERROR database
    DECLARE
    MachineName VARCHAR2 (64);
    osuserid VARCHAR2 (30);
    v_sid NUMBER (10);
    v_serial NUMBER (10);

    CURSOR c1 IS
    SELECT sid, serial #, osuser, machine
    FROM v$ session WHERE audsid = userenv ('sessionid');
    BEGIN
    IF (IS_SERVERERROR (1017)) THEN
    OPEN c1;
    EXTRACT the c1 IN v_sid, v_serial, osuserid, machinename;
    INSERT INTO logonaudittable VALUES ('FAILLOGON', v_sid, v_serial, sysdate,
    user, osuserid, machinename);
    CLOSE c1;
    END IF;
    END;
    /

    -------------------------------------------

    The I create a trigger that starts each time something is written on the table.
    This trigger must start an EXE file which writes the last data written on the table in the Windows application log.

    So I need a trigger that executes my WRITELOG. EXE file adding parameters. For example:

    Run WRITELOG. Event time username sid EXE

    So my program will write a journal in Windows with these data:

    Event: opening of SESSION
    SID: 2938473
    Time: 12/12/2011 11:45:32
    User name: Scott

    And so on.

    I already have the program that does this, I need to run the program with the right data directly from trigger or procedure performed by my trigger.

    Hope that I was clear and REALLY hope that someone can help me!

    Thanks to you all!

    So practically I should create a job something like this:

    BEGIN
    () dbms_scheduler.create_job
    job_name-online "CONNECTION."
    job_type-online "EXECUTABLE."
    job_action => ' c:\yourdirectory\FILE. LOGON EXE ' | v_sid | » '|| v_serial | » '|| to_char(sysdate,'MM/DD/YYYY') | » '|| the user | » '|| osuserid | » '|| MachineName |';
    number_of_arguments-online 1
    start_date => SYSTIMESTAMP,
    enabled-FALSE, online
    comments => ' SQL * Loader jobs Demo');
    END;
    /

    and then I need to start this work every time that the LOGON trigger fires and it must be the trigger that inserts into the work, the arguments v_sid, v_serial, etc..

    ... I don't know how I can do this. What I need to create a procedure that creates jobs and the procedure is executed by the trigger? and how do you know the procedure to create the job with the data provided by the trigger. Sorry I ask much for the help, but I need this thing made in a time which is not compatible with me studying how to do it by myself. I will look at it, I want to know exactly how these things work, but now I need to speed things up a bit!

    Thank you for all the valuable assistance tfor!

  • trying to get used to sql * more running and creating a simple procedure

    Hello all;

    I'm getting used to sql * more after using for a while using pl/sql developer.

    I am trying to create a simple procedure and run it. See my procedure below
    create or replace procedure test(t in varchar2(200), limit in number(30))
    begin
    select tbl_report.id from tbl_report where tbl_report.id like '|| t ||%' and rownum <= limit;
    end test;
    and then I type in run hr.test ("J", 10); to run this and then press ENTER, and it does nothing. How can I run and create a simple procedure in sql * more

    Hello

    In addition to all the good points, others have done:

    When you issue a SELECT statement in SQL * Plus (or any other front end) two things happen:
    (1) a result set, containing the results of your query, is produced, and
    (2) this result set is displayed.
    Given that these two things are always done together, it is easy to forget that they are two different things.
    In PL/SQL, you must explicitly do something to catch the result set, and you must explicitly do something else to view it (if you want to display it).

    A simple way to get the result set is to use a cursor FOR loop, to extract a line at a time and run code with this line in a record variable (called the country in the example below).
    View the results is to call dbms_output.put_line.

    Here is an example:

    CREATE OR REPLACE PROCEDURE     test
    (       t       IN      VARCHAR2
    ,     lmt      IN     NUMBER          -- LIMIT is an Oracle keyword; best not to use it as a variable name
    )
    IS
    BEGIN
        FOR  country  IN ( SELECT  country_name
                          FROM    hr.countries
                     WHERE   country_name     LIKE t || '%'
                     AND     ROWNUM          <= lmt
                   )
        LOOP
            dbms_output.put_line (  country.country_name
                        || ' = country_name'
                        );
        END LOOP;
    END test;
    /
    SHOW ERRORS
    

    You can use variables, such as t and lmt, in the query without using dynamic SQL, as long as the variables do not have the same names as the columns in the table. In this case, t and lmt are good names; country_name or region_id wouldn't. (In fact, t is a reputable insofar as it cannot be confused with a column in the table of the country. T is not a good reputation in the sense that it does not give much information about what contains the variable, or how it is used. Why not call the variable target_name or name_a_trouver?

    You can run the SQL procedure * more like this:

    SET     SERVEROUTPUT     ON
    
    EXEC  test ('I', 2);
    

    If you issue the SET SERVEROUTPUT ON command, then you will not see the output of dbms_output. You must only issue the command SET SERVEROUTPUT ON once per session, but nothing bad happens if you do it more than once.

    When I ran the procedure as stated above, I got this output:

    Israel = country_name
    India = country_name
    

    Notice that "The Italy" does not appear, because lmt has 2.

    Do not create your own procedures, tables or other objects in the HR schema, or among all the other patterns created by Oracle. Create your own schema and create procedures and other objects in it.

  • SQL that runs for a long time

    Hi guys,.

    How to find the start time of the SQL statements that run for a long time and who are running in the system.

    Hello

    If they are long launched, you can take a look at the view V$ SESSION_LONGOPS. There is a column of start_time.

    Herald tiomela
    http://htendam.WordPress.com

  • SQL comment in "sql * more ' runs inside.

    See code below. Why I get after release:
    SQL>  /*select sysdate from dual*/
    
    SYSDATE
    ----------
    16.08.2010
    I have enterd only a comment why it comes out as a command? Is there a solution for Sql * more so that he would interpret "/ * aa * /"-style comment as true comment? ".
    SQL*Plus: Release 10.2.0.3.0 - Production on E Aug 16 14:17
    
    Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.
    
    
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 -
    With the Partitioning, OLAP and Data Mining options
    
    SQL>  /*select sysdate from dual*/
    SP2-0103: Nothing in SQL buffer to run.
    SQL> select sysdate from dual;
    
    SYSDATE
    ----------
    16.08.2010
    
    SQL>  /*select sysdate from dual*/
    
    SYSDATE
    ----------
    16.08.2010
    
    SQL> /* select sysdate from dual */
    SQL> select sysdate from dual;
    
    SYSDATE
    ----------
    16.08.2010
    
    SQL> /* select sysdate from dual; */
    SQL> select sysdate from dual;
    
    SYSDATE
    ----------
    16.08.2010
    
    SQL> 

    I don't know, but it's because
    entry has / SQL more will take place the last executed query.
    So in / * select sysdate from double * /.
    Maybe sqlplus takes just the first and therefore the executed query previously running

  • SQL * Loader running forever

    Hello

    I use Oracle 10.2.0.4 on RHEL 4u4. I am trying to load a table with data using "Load data user Files" in Enterprise Manager (SQL * Loader).
    When I run the work, she spends just forever, and never ends. I try only to charge about 6 400 lines, so I can't imagine that he should take a very long time. I have created a. BAD file, a. File to DISCARD and one. LOG file, but all are empty. I also followed the alert log, but I see not everything about the load there either. I may have something wrong in the control file?

    Here is a sample of my data in the flat file:

    1 * "LAURA, LAURIE, LORA, LAURI, LARA."
    2 * "SARA, SARAH."
    3 * "' JENNIFER, JENN, JEN, JENNI, JENNY" * "
    ...

    And the control file generated by Enterprise Manager:

    DOWNLOAD THE DATA
    ADD
    IN THE NAMES.NAME_VARIANTS TABLE
    ' FIELDS TERMINATED BY ' *' POSSIBLY PROVIDED BY ' "'
    (
    COUNTER INTEGER EXTERNAL,
    VARIANT_TEXT TANK
    )

    I'm not sure how to solve this because I don't get anything in each log file!

    Mimi

    I'm not sure how to solve this because I don't get anything in each log file!

    script /tmp/capture.log
    sqlldr strace...

  • Column headings missing when the SQL report runs Oracle Conc ask

    Why the column headers for a sql report do not disappear when I try to run Oracle Manager of competing application work? If I copy and paste work in SQL * more and run it, the topics show on the report. Is there a setting I can update to allow these column headers to display?
    Example of what I get (No Headings)
    
    TOM SMITH          12345
    JANE DOE           34355
    
    Example of what I expect (with Headings)
    
    EMPLOYEE           EMPLOYEE NO
    TOM SMITH          12345
    JANE DOE           34355
    Thank you

    More likely:

    SET THE POSITION

    into force or / and

    SET PAGESIZE 0

    is in effect.

    SY.

Maybe you are looking for