1

I have to create a plsql block which will add a new column on a table ( with dynamic Sql) and then it will insert some data in that new added column.

When I execute my block, I got a message that says that my column is not defined, even if it has been added before the insert statement.

Do you have any idea of what could be causing this problem?

It's like my insert statement doesn't know that the new column has been added.

1
  • are you inserting using dynamic sql too? If you can share some sample code, that will help. Commented Sep 12, 2018 at 19:39

1 Answer 1

5

If you add new column and update/insert data in the new column using dynamic SQL then it will work. Example -

begin
  execute immediate 'ALTER TABLE table_name ADD column_name column-definition'; 
  execute immediate 'update table_name set column_name =''sample-data''';
  COMMIT ;
end;

But, if you add new column using dynamic SQL and use static SQL to update/insert then it will throw error message.
Reason - The static SQL is validated before its execution.
Example -

begin
  execute immediate 'ALTER TABLE table_name ADD column_name column-definition'; 
  update table_name set column_name ='sample-data';
  COMMIT ;
end;
Sign up to request clarification or add additional context in comments.

2 Comments

That's very nice. Thanks a lot.
Hello Jaydip Rakholiya, What do you mean by SQL is validated before its execution ? Could you give me more details please.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.