EXECUTE IMMEDIATE help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • finlma

    EXECUTE IMMEDIATE help

    I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it
    doesn't work for me. I'm trying to create a column conditionally but
    it doesn't work. It fails because there are apostrophes within the
    statement. How do I override the apostrophe?

    DECLARE cCount NUMBER;
    BEGIN SELECT count(*)
    INTO cCount
    FROM all_tab_columns
    WHERE owner = 'Owner'
    AND table_name = 'table_name'
    AND column_name = 'column_name' ;

    IF cCount = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE owner.table_nam e
    ADD (column_name CHAR(1) DEFAULT ' ' NOT NULL)';
    ELSE
    EXECUTE IMMEDIATE 'ALTER TABLE owner.table_nam e
    MODIFY column_name DEFAULT ' '';
    END IF;
    END;

    Thanks
  • Frank

    #2
    Re: EXECUTE IMMEDIATE help

    finlma wrote:
    I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it
    doesn't work for me. I'm trying to create a column conditionally but
    it doesn't work. It fails because there are apostrophes within the
    statement. How do I override the apostrophe?
    >
    DECLARE cCount NUMBER;
    BEGIN SELECT count(*)
    INTO cCount
    FROM all_tab_columns
    WHERE owner = 'Owner'
    AND table_name = 'table_name'
    AND column_name = 'column_name' ;
    >
    IF cCount = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE owner.table_nam e
    ADD (column_name CHAR(1) DEFAULT ' ' NOT NULL)';
    ELSE
    EXECUTE IMMEDIATE 'ALTER TABLE owner.table_nam e
    MODIFY column_name DEFAULT ' '';
    END IF;
    END;
    >
    Thanks
    You don't override, you escape - with an extra quote:
    to insert "It's XMAS time" in a table, you would:
    insert into table(column) values ('It''s XMAS time');
    And there's inefficient code: you do not need to know
    *how* many (into cCount), you just want to know IF any:
    select 1 into cCount from dual
    where exists (...)
    would be more efficient.
    --
    Regards, Frank van Bortel

    Comment

    • FaheemRao

      #3
      Re: EXECUTE IMMEDIATE help

      you would like to include brackets in the statement as well like this


      EXECUTE IMMEDIATE ('ALTER TABLE owner.table_nam e
      ADD (column_name CHAR(1) DEFAULT '' '' NOT NULL)');


      Thanks

      Faheem


      finlaymark@hotm ail.com (finlma) wrote in message news:<8b454add. 0312150935.7559 8e78@posting.go ogle.com>...
      I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it
      doesn't work for me. I'm trying to create a column conditionally but
      it doesn't work. It fails because there are apostrophes within the
      statement. How do I override the apostrophe?
      >
      DECLARE cCount NUMBER;
      BEGIN SELECT count(*)
      INTO cCount
      FROM all_tab_columns
      WHERE owner = 'Owner'
      AND table_name = 'table_name'
      AND column_name = 'column_name' ;
      >
      IF cCount = 0 THEN
      EXECUTE IMMEDIATE 'ALTER TABLE owner.table_nam e
      ADD (column_name CHAR(1) DEFAULT ' ' NOT NULL)';
      ELSE
      EXECUTE IMMEDIATE 'ALTER TABLE owner.table_nam e
      MODIFY column_name DEFAULT ' '';
      END IF;
      END;
      >
      Thanks

      Comment

      Working...