Problem with instructions to update within the if statement

Hello

I have the following PL/SQL script. It was written in a way so it can be run several times, without worrying about if she has previously run means that it should only perform the update and edit, if it has not already been done.

DECLARE
CNT NUMBER;

BEGIN
-Rename column
COUNT (*) of SELECT INTO cnt FROM user_tab_columns WHERE TABLE_NAME = 'WELL_TEST_DATA_QUERIES' AND COLUMN_NAME = 'PRIMARY ';

IF (cnt = 1)
THEN
UPDATE WELL_TEST_DATA_QUERIES
THE PRIMARY VALUE = 0
WHERE PRIMARY = 1;

RUN IMMEDIATELY 'ALTER TABLE WELL_TEST_DATA_QUERIES RENAME COLUMN PRIMARY TO WELL_TEST_TYPE;


END IF;
END;

However, when it is executed several times, it seems to execute the body of the if statement each time, despite the fact that the select statement must return a count of 0 (and I checked that this is the case).

If I change the script and put the UPDATE statement in an EXECUTE IMMEDIATE, it works fine:

DECLARE
CNT NUMBER;

BEGIN
-Rename column
COUNT (*) of SELECT INTO cnt FROM user_tab_columns WHERE TABLE_NAME = 'WELL_TEST_DATA_QUERIES' AND COLUMN_NAME = 'PRIMARY ';

IF (cnt = 1)
THEN
IMMEDIATE EXECUTION
' UPDATE WELL_TEST_DATA_QUERIES
THE PRIMARY VALUE = 0
WHERE PRIMARY = 1';

RUN IMMEDIATELY 'ALTER TABLE WELL_TEST_DATA_QUERIES RENAME COLUMN PRIMARY TO WELL_TEST_TYPE;


END IF;
END;

Can someone give me an indication of what is happening here?

Thank you
Kathryn

user10855910 wrote:
Hello

I have the following PL/SQL script. It was written in a way so it can be run several times, without worrying about if she has previously run means that it should only perform the update and edit, if it has not already been done.

Are you referring to getting this:

SQL> create table well_test_data_queries (primary number);

Table created.

SQL> insert into well_test_data_queries values (1);

1 row created.

SQL> commit;

Commit complete.

SQL> ed
Wrote file afiedt.buf

  1  DECLARE
  2    cnt NUMBER;
  3  BEGIN
  4    -- Rename column
  5    SELECT COUNT(*) INTO cnt FROM user_tab_columns WHERE TABLE_NAME = 'WELL_TEST_DATA_QUERIES' AND COLUMN_NAME = 'PRIMARY';
  6    IF cnt = 1 THEN
  7      UPDATE WELL_TEST_DATA_QUERIES
  8      SET PRIMARY = 0
  9      WHERE PRIMARY = 1;
 10      EXECUTE IMMEDIATE 'ALTER TABLE WELL_TEST_DATA_QUERIES RENAME COLUMN PRIMARY TO WELL_TEST_TYPE';
 11    END IF;
 12* END;
SQL> /

PL/SQL procedure successfully completed.

SQL> select * from well_test_data_queries;

WELL_TEST_TYPE
--------------
             0

SQL> DECLARE
  2    cnt NUMBER;
  3  BEGIN
  4    -- Rename column
  5    SELECT COUNT(*) INTO cnt FROM user_tab_columns WHERE TABLE_NAME = 'WELL_TEST_DATA_QUERIES' AND COLUMN_NAME = 'PRIMARY';
  6    IF cnt = 1 THEN
  7      UPDATE WELL_TEST_DATA_QUERIES
  8      SET PRIMARY = 0
  9      WHERE PRIMARY = 1;
 10      EXECUTE IMMEDIATE 'ALTER TABLE WELL_TEST_DATA_QUERIES RENAME COLUMN PRIMARY TO WELL_TEST_TYPE';
 11    END IF;
 12  END;
 13  /
    WHERE PRIMARY = 1;
          *
ERROR at line 9:
ORA-06550: line 9, column 11:
PL/SQL: ORA-00904: "PRIMARY": invalid identifier
ORA-06550: line 7, column 5:
PL/SQL: SQL Statement ignored

SQL>

The problem is not because the IF statement entered the swiped m the cnt = 1, but because, when the code is compiled the PRIMARY column does not exist. The code should compile properly against the database, and all verified database object references, until the code is actually running.

As you have discovered, when you do the dynamic update to help to run immediately, you will not get this problem, because execute immediate statements are strings that can not be validated at compile time and so the code will compile ok and then run.

Tags: Database

Similar Questions

Maybe you are looking for

  • Carpet * a UJ - 831S engraving options (Satellite M40X-237)

    Hello I recently bought a Satellite M40X-237 with this carpet * a DVD - RAM drive.For DVD playback in a home theater system, I usually burn DVDs with booktype set to DVD - ROM, so that the reader can read correctly.Problem is that the latter does not

  • Equium L10 300 Wont start white screen

    Hi I wonder if anyone can help or advise me with a problem starting It's a portable satellite L10 300 XP homedition, after running PC doctor to remove Popups and adware my laptop stopped commissioning. Initially started after the system check it kept

  • Is compatible with BNC-2090 PCI-6723 has?

    Hi everyone, I am working in a project where I'm forced to use both a PCI-6723 connected to a BNC-2090 case has... In the datasheet of the 6723 only to versions of the BNC-2110 and 2115 are suggested. Anyone know if I can still use the one I have no

  • stand mode

    Lately the computer goes to sleep in the middle of my work.  How can I control?

  • Question of size BrowserField2 displayContent fonts

    We are calling BrowserField2.displayContent (String, String) and passing in HTML. On 5.0.0 police find normal, but on 6.0.0 it is very large. I checked the zoomScale on both and they are 1.0f. I checked the size of the BrowserField and the Manager ho