How to truncate the table in a pl/sql block

Hello guys, I'm trying to truncate a table that includes data using a pl/sql block, so if the table has even a single row then the truncate should run. to do this, I wrote the following code


declare
number num.
Start
Select count (*) in the num from table_name;
If num > 0 then
truncate table table_name;
end if;
end;

Now, when I execute the pl/sql block, I get the following error.

PLS-00103: encountered the symbol "TABLE" when the expected in the following way:

When I run the truncate without pl/sql command, it runs very well.

Can someone please where I'm wrong.

Thank you

Published by: 969224 on April 23, 2013 08:08

969224 wrote:
Hello guys, I'm trying to truncate a table that includes data using a pl/sql block, so if the table has even a single row then the truncate should run. to do this, I wrote the following code

declare
number num.
Start
Select count (*) in the num from table_name;
If num > 0 then
truncate table table_name;
end if;
end;

Now, when I execute the pl/sql block, I get the following error.

PLS-00103: encountered the symbol "TABLE" when the expected in the following way:

When I run the truncate without pl/sql command, it runs very well.

Can someone please where I'm wrong.

Thank you

Published by: 969224 on April 23, 2013 08:08

DDL is not allowed directly within PL/SQL procedures.

You must (ab) use EXECUTE IMMEDIATE issuing the DDL

Tags: Database

Similar Questions

  • How to truncate the table using SQLJ

    Here is a snippet of code SQLJ trying to truncate a table before filling with new records.

    However, while the class compiles fine, I get an error PLS-00103 on execution.

    Your expertise is very welcome and necessary for what seems like a simple problem, for which I can't find an answer.

    import java. IO;
    import java.sql. *;

    public class FadsDirListing
    {
    public static getContentsList (String directory) Sub throws SQLException
    {
    *#sql {FADS_DIR_CONTENTS TRUNCATE TABLE}; * - WHY NOT THIS STATEMENT?
    File [] files = new File (directory) .listFiles ();
    for (int i = 0; (file! = null) & & (I < files.length); (++ I)
    {
    File f = files;
    String filName = f.getName ();
    long filSize = f.length ();
    ModDate timestamp = new Timestamp (f.lastModified ());
    String filType = f.isDirectory ()? "D" : "F";

    {INSERT #sql
    IN FADS_DIR_CONTENTS (FILE_SIZE, TYPE_DE_FICHIER, FILE_NAME, LAST_MODIFIED)
    VALUES (: filName,: filSize,: filType,: ModDate)
    };
    #sql {COMMIT
    };
    }
    }
    }

    Thank you for your help - I appreciate it very much.

    Dave

    You must run a command DDL by NDS. If you wrap it in a PL/SQL block and use NDS to run it, it should work.


    BEGIN
    EXECUTE IMMEDIATE "DDL STATEMENT";
    END;

  • How to initialize the Java variable within pl/sql block in the ODi procedure

    I have a step in the procedure of odi that using oracle technology.

    I want to initialize the java variable inside that.

    Please help me for this.

    < @ if (odiRef.getOption ("USE_PUBADMIN_PARAM_TABLE").equals("1")) {@ >}

    DENIZ

    EH_FAILURE_MESSAGE_TEXT VARCHAR2 (4000);

    EH_FIXED_VERSION VARCHAR2 (4000);

    EH_ISSUE_TYPE VARCHAR2 (4000);

    EH_PRIORITY VARCHAR2 (4000);

    EH_SUMMARY VARCHAR2 (4000);

    EH_DESCRIPTION VARCHAR2 (4000);

    EH_PROJECT_ID VARCHAR2 (4000);

    EH_COMPONENT VARCHAR2 (4000);

    EH_AFFECTED_VERSION VARCHAR2 (4000);

    EH_CUSTOMPROPXML VARCHAR2 (4000);

    EH_LOG_JIRA VARCHAR2 (4000);

    EH_CONTINUE_ON_ERROR VARCHAR2 (4000);

    EH_SEND_MAIL_NOTIFICATION VARCHAR2 (4000);

    EH_NOTIFICATION_RECIPENTS VARCHAR2 (4000);

    EH_JIRAJARPATH VARCHAR2 (4000);

    BEGIN

    SELECT

    DECODE ('< % = odiRef.getOption ("FAILURE_MESSAGE_TEXT") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_FAILURE_MESSAGE_TEXT'),' < % = odiRef.getOption ("FAILURE_MESSAGE_TEXT") % >") IN EH_FAILURE_MESSAGE_TEXT.

    DECODE ('< % = odiRef.getOption ("FIXED_VERSION") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_FIXED_VERSION'),' < % = odiRef.getOption ("FIXED_VERSION") % >") IN EH_FIXED_VERSION.

    DECODE ('< % = odiRef.getOption ("ISSUE_TYPE") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_ISSUE_TYPE'),' < % = odiRef.getOption ("ISSUE_TYPE") % >") IN EH_ISSUE_TYPE.

    DECODE ('< % = odiRef.getOption ("PRIORITY") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_PRIORITY'),' < % = odiRef.getOption ("PRIORITY") % >") IN EH_PRIORITY.

    DECODE ('< % = odiRef.getOption ("SUMMARY") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_SUMMARY'),' < % = odiRef.getOption ("SUMMARY") % >") IN EH_SUMMARY.

    DECODE ('< % = odiRef.getOption ("DESCRIPTION") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_DESCRIPTION'),' < % = odiRef.getOption ('DESCRIPTION') % >") IN EH_DESCRIPTION.

    DECODE ('< % = odiRef.getOption ("project") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_PROJECT_ID'),' < % = odiRef.getOption ("project") % >") IN EH_PROJECT_ID.

    DECODE ('< % = odiRef.getOption ("ELEMENT") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_COMPONENT'),' < % = odiRef.getOption ('ELEMENT') % >") IN EH_COMPONENT.

    DECODE ('< % = odiRef.getOption ("AFFECTED_VERSION") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_AFFECTED_VERSION'),' < % = odiRef.getOption ("AFFECTED_VERSION") % >") IN EH_AFFECTED_VERSION.

    DECODE ('< % = odiRef.getOption ("CUSTOMPROPXML") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_CUSTOMPROPXML'),' < % = odiRef.getOption ("CUSTOMPROPXML") % >") IN EH_CUSTOMPROPXML.

    DECODE ('< % = odiRef.getOption ("LOG_JIRA") % >', '0', PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_LOG_JIRA'),' < % = odiRef.getOption ("LOG_JIRA") % >") IN EH_LOG_JIRA.

    Decode('%=odiRef.GetOption("CONTINUE_ON_ERROR") % > ', '0', PBA_PARAM_PKG. ("GET_PARAMETER ('EH_CONTINUE_ON_ERROR'),' < % = odiRef.getOption ("CONTINUE_ON_ERROR") % >") IN EH_CONTINUE_ON_ERROR.

    DECODE ('< % = odiRef.getOption ("SEND_MAIL_NOTIFICATION") % >', '0', PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_SEND_MAIL_NOTIFICATION'),' < % = odiRef.getOption ("SEND_MAIL_NOTIFICATION") % >") IN EH_SEND_MAIL_NOTIFICATION.

    DECODE ('< % = odiRef.getOption ("NOTIFICATION_RECIPENTS") % >', NULL, PBA_PARAM_PKG.) ("GET_PARAMETER ('EH_NOTIFICATION_RECIPENTS'),' < % = odiRef.getOption ("NOTIFICATION_RECIPENTS") % >") IN EH_NOTIFICATION_RECIPENTS.

    PBA_PARAM_PKG. GET_PARAMETER ('EH_JIRAJARPATH') IN EH_JIRAJARPATH

    Double;

    / * I want to start as it is below. idon't want to control user source and control target conecpt.

    Please help me to below concept.

    */

    < @.

    String V_EH_FAILURE_MESSAGE_TEXT = EH_FAILURE_MESSAGE_TEXT;

    String V_EH_FIXED_VERSION = EH_FIXED_VERSION;

    String V_EH_ISSUE_TYPE = EH_ISSUE_TYPE;

    String V_EH_PRIORITY = EH_PRIORITY;

    String V_EH_SUMMARY = EH_SUMMARY;

    String V_EH_DESCRIPTION = EH_DESCRIPTION;

    String V_EH_PROJECT_ID = EH_PROJECT_ID;

    String V_EH_COMPONENT = EH_COMPONENT;

    String V_EH_AFFECTED_VERSION = EH_AFFECTED_VERSION;

    String V_EH_CUSTOMPROPXML = EH_CUSTOMPROPXML;

    String V_EH_LOG_JIRA = EH_LOG_JIRA;

    String V_EH_CONTINUE_ON_ERROR = EH_CONTINUE_ON_ERROR;

    String V_EH_SEND_MAIL_NOTIFICATION = EH_SEND_MAIL_NOTIFICATION;

    String V_EH_NOTIFICATION_RECIPENTS = EH_NOTIFICATION_RECIPENTS;

    String V_EH_JIRAJARPATH = EH_JIRAJARPATH;

    @ >

    END;

    {< @} @ >

    I have corrected this problem. No need to search on that.

  • Data export of the Disqualification to the table in step should not truncate the table.

    Hello friends,

    Please find blow my requirement and do the necessary.

    We export data cleaned to staging tables, whenever we export data, Disqualification truncates the table and inserts the new data sets into this table.

    My requirement is instead of truncating the table before inserting the data in it, Disqualification must add these records, should not truncate the table.

    Please let me know how to configure this in OEDQ, your help is appreciated.

    Thank you, Prasad

    Could not be easier. Double-click the task to export in your work and change the mode append.

  • Index rebuild required after truncate the table and load data

    Hello


    I have a situation that we truncate tables bit and then we loaded data [only content] on these tables. What you need to rebuild the index online is necessary or not?


    And another question is if we drop a few clues is the total amount of space is released or not. And re-create indexes will use the same amount of space. As I don't have disk space more? In this situation, rebuild the index online will be a better idea...

    Can you please on this...


    truncate the table some the few loading tables + reconstruction markings online is the best (or) droping little tables, a few tables loading + re-create the index is better

    Can you suggest the best way... We have a time that it currently we don't have enough space on the disk... [Option should not effect the space]

    user13095767 wrote:
    Ok. I have it...

    u want to say if we disbale the index while loading... Next, we need to spend the time to build.

    If the indexes are enabled, then rebuild again is not necessary after loading tables...

    Please answer if my understanding is correct...

    above is correct

    >

    If so, how abt the differences in the space occupied by the spaces of storage during the index rebuild and re-create... T he acquires more space if recreate us [deletion and creation] or rebuild online is preferable to an index...?

    space used is the same for all options.

  • raise errors ora-01426 truncating the table

    Hello

    We have a process that runs in the following process for some tabls game
    1 disable constraints
    2. the truncating tables
    3. activation of the constraints

    but after treatment for 5 tables, truncating up error

    BEGIN < process name >; END;



    *

    ERROR on line 1:

    ORA-01426: digital overflow

    ORA-06512: at the "< procedurename >", line 33

    ORA-06512: at line 1


    This line 33 point to run immeidate ' truncate table < table name >

    Can I know what is the reason for throwing "digital overflow" error truncating the table...

    Thanks in advance

    user9080289 wrote:
    Thank you.

    Is my verision of oracle 11g

    Then, you could have the described bug. Follow the instructions how to bypass (flush with low-value stats monitoring) or apply the #8226471 patch.

    Nicolas.

  • How to clear the table dhcp clients

    Does anyone know how to clear the Table of Clients DHCP?

    I have a WRV54G.

    Concerning

    I understand this is a very old post, but since I found it when I was looking for the same answer in 2009, I thought go ahead and give my $.02 worth.

    I have a different router, so it may not be present in your.

    I use a version of the firmware WRT54GLwith 4.30.7 and finally found on the status under "Local network" page a link to the DHCP table. You can click on that and delete existing IP addresses.

  • How to know the tables used in packages of a schema.

    How to know the tables used in packages of a pattern that I have connected.

    SELECT DISTINCT referenced_owner, referenced_name

    Of all_dependencies

    Owner WHERE = "MY_USER_NAME".

    AND type ('PACKAGE', 'BODY of PACKAGE')

    AND referenced_type = 'TABLE '.

  • How to purge the table ECX_OUTQUEUE of the ECX_OUTBOUND AGENT

    How to purge the table ECX_OUTQUEUE of the ECX_OUTBOUND AGENT

    904632 wrote:

    How to purge the table ECX_OUTQUEUE of the ECX_OUTBOUND AGENT

    Please see (how to recreate the ECX Related queues? (Doc ID 1354312.1)).

    Thank you

    Hussein

  • How to design the table from the answer to the question table.

    Hi all

    I am creating an application for student review online.

    There are two types of questions, the only choice of response and multi choice answers.

    My question is less than
    create table question_master
    (
    exam_id number references exam_master(exam_id),
    marks_of_each_question number,
    type_of_question char(1),-- single choice answer/multiple choices answer
    q1 varchar2(2000),
    q2 varchar2(2000),
    q3 varchar2(2000),
    q4 varchar2(2000))
    my table to answer (I'm not satisfied with who is below
    create table answers_of_questions
    (
    answer_id number primary key,
    question_ID number referenes question_master(question_id),
    answer varchar2(4000) not null,
    is_answer_correct char(1),--y/n,
    student_selection char(1),--y/n   student select it or not
    ....
    ...
    now, I'm perfectly how to create the RESPONSE table to contain the answers.
    the only choice is good, but several checkboxes choices, what to do?
    How to design the table from the answer?

    do I have to create 2 tables to contain the answers?
    Note: the QUESTIONS and ANSWERS, all will be entered by the teacher. students will make a choice and I will store this choice in another table
    may be called STUDENT_SELECTED_ANSWERS or something like that.
    If anyone has some reference to the script retail scheme review online, kindly share with me.

    Kind regards.

    Kind regards.

    If you need to have answers in another table:

    Student (student_id, name, etc...)
    Review (exam_id, exam_name, etc...)
    Question (question_id, exam_id, question_text)

    Response (question_id, a_text, b_text, c_text, d_text, a, b, c, d).<-- one-to-one="" with="" question="" table,="" a-d="" flags="" used="" to="" indicate="">
    OR
    Response (question_id, answer_id, answer_text, OK)<-- many-to-one="" with="" question="" table,="" correct="" flag="" used="" to="" indicate="" correctness="" for="" this="" single="">

    Student_Answer (student_id, question_id, a, b, c and d)
    OR
    Student_Answer (question_id, student_id, answer_id)<-- creation="" of="" a="" question_id+answer_id="" in="" this="" table="" implies="" the="" student="" checked/selected="" it="" as="" an="">

    To what extent you want to standardize, it is up to you.

  • How to add the table radio button.

    Hello

    I want to update the record if I want that an option of button raduio shaped tabular .There is no option for tabular.
    How to add the table radio button.


    Thank you

    Published by: 805629 on February 3, 2011 04:56

    Use APEX_ITEM. BOX API in the definition of SQL query for the form of tables

  • How to check the table have are all views in oracle

    Hello
    How to check the table have are all views in oracle
    SELECT * FROM user_dependencies
    WHERE type='VIEW'
    AND referenced_type='TABLE'
    AND referenced_name ='Your_Table_Name' 
    

    You can use dba_dependencies to find views in the different schema.

  • How to solve the error ORA-00001 in SQL Insert?

    Hi all, I need your help appreciated.

    I do a plsql procedure that inserts a line according to the value of the slider, I have error oracle ORA-00001: unique constraint (constraint_name) violated.

    This message may appear if a duplicate entry exists at a different level: in the RDBMS MySQL, I have the syntax IGNORES to solve this error of duplication... and in Oracle?

    Thanks for your time and your advice.
    Miguelito

    user6317803 wrote:
    How to solve the error ORA-00001 in SQL Insert?

    ORA-00001 means table a unique/primary key / index and you attempt to insert a row with the key value already exists in the table. I'll assume table has a primary key on COUNTRY_ID. Then modify SQL for:

    SQL = "INSERT INTO COUNTRIES (COUNTRY_ID, COUNTRY_NAME, REGION_ID) SELECT"BZ","BLZ", 3 DOUBLE WHERE DOES NOT EXIST (SELECT 1 FROM COUNTRIES WHERE COUNTRY_ID ="BZ").

    There is a good chance COUNTRY table also has unique key/index on COUNTRY_NAME. If so use:

    SQL = "INSERT INTO COUNTRIES (COUNTRY_ID, COUNTRY_NAME, REGION_ID) SELECT"BZ","BLZ", 3 DOUBLE WHERE DOES NOT EXIST (SELECT 1 FROM COUNTRIES WHERE COUNTRY_ID = 'BZ' OR 'BLZ' = COUNTRY_NAME).

    SY.

  • A question about the count of several lines of table in a PL/SQL block

    Hi all




    I have a problem on counting the rows from several tables in a PL/SQL block, and I would be grateful if you could kindly give me a helping hand. Here's my problem: file in Microsoft Excel (one column) I have a list of several names of tables. For each table, when the number of rows is equal to 10000 I have to call a procedure. Here's how I tried to do:
    DECLARE
         CURSOR tb_cursor IS
              WITH my_table_names AS
                   (
                        SELECT  'table1'  AS tbname  FROM  DUAL  UNION
                        SELECT  'table2'  AS tbname  FROM  DUAL  UNION
                        SELECT  'table3'  AS tbname  FROM  DUAL  UNION
                        SELECT  'table4'  AS tbname  FROM  DUAL  UNION
                        .
                        .  Here I continue writing one line for each table in order
                        .  to have the table names stored in my Excel file as a table 
                           to be queried by SELECT statement
                        .
                   )
              SELECT *
              FROM my_table_names;
    BEGIN
         -- Here I verify that for each table having more than 10000 lines
         -- I call the specified procedure which is needed
         
         FOR I IN tb_cursor LOOP
              DECLARE
                   -- Here I declare a cursor for counting the number of rows
                   CURSOR currentTableRowCounter IS
                        SELECT COUNT(*) AS rowsNum
                        FROM I.tbname;
                        
                   numberOfRows currentTableRowCounter%ROWTYPE;
              BEGIN
                   OPEN currentTableRowCounter;
                   FETCH numberOfRows INTO numberOfRows;
                   CLOSE currentTableRowCounter;
                   
                   IF (numberOfRows.rowsNum > 10000) THEN
                        -- And here I will call the procedure which has to be run
                   END IF;
              END;
         END LOOP;
    END;
    /
    I already checked this code with tables inividual and it works. The only problem is
    . . .
    SELECT COUNT(*) AS rowsNum
    FROM I.tbname;
    . . .
    Indeed, oracle considers "I.tbname" as an unknown table name (although he refers to by its exact name).
    SQL> @script.sql
    
                                    FROM I.tbname;
                                           *
    ERROR at line 99:
    ORA-06550: line 99, column 12:
    PL/SQL: ORA-00942: table or view does not exist
    How can I solve this problem? I mean, how to use a variable (in my example, I.tbname) as the table name in the FROM clause to query a table instead of explicitly write the name of the table?





    Thanks in advance,
    Dariyoosh

    Replace the following code:

              DECLARE
                   -- Here I declare a cursor for counting the number of rows
                   CURSOR currentTableRowCounter IS
                        SELECT COUNT(*) AS rowsNum
                        FROM I.tbname;
    
                   numberOfRows currentTableRowCounter%ROWTYPE;
              BEGIN
                   OPEN currentTableRowCounter;
                   FETCH numberOfRows INTO numberOfRows;
                   CLOSE currentTableRowCounter;
    
                   IF (numberOfRows.rowsNum > 10000) THEN
                        -- And here I will call the procedure which has to be run
                   END IF;
              END;
    

    By the following:

    Declare
      numberOfRows number;
    begin
      EXECUTE IMMEDIATE 'select count(*) from '||I.tbname into numberOfRows;
    
      IF (numberOfRows.rowsNum > 10000) THEN
        -- And here I will call the procedure which has to be run
      END IF;
    end;
    

    Max
    [My Italian blog Oracle | http://oracleitalia.wordpress.com/2010/01/10/crittografia-in-plsql-utilizzando-dbms_crypto/]

    Published by: Massimo Ruocchio, January 12, 2010 15:25
    Added Variable Declaration

  • How to truncate the output of the field in the column "LONG".

    Hi all

    I have been asked to retrieve all records from a table with a column that has been initially formatted with the format "LONG".

    How can I truncate the output of this area without having to show the Junk characters?

    Very appreciated

    JR

    Wrong forum!

    This forum is ONLY for questions/problems on Sql Developer and your question has NOTHING to do with this product.

    Please mark the thread ANSWERED and repost it in the SQL/PL/SQL forum.

    SQL and PL/SQL

    When you repost you must SHOW an example of what you are wanting to do. You should also explain what you mean by 'LONG Format' - I have never heard such a thing.

Maybe you are looking for