I want to run 100 Insert statements of the below Insert`s in a Script. The schema name db1 and db2 must used in the placeholder variable SourceDatabase and TargetDatabase.
DECLARE
SourceDatabase VARCHAR2(50) := 'DB1';
TargetDatabase VARCHAR2(50) := 'DB2';
BEGIN
Insert Into TargetDatabase.TableName (SELECT * FROM SourceDatabase.TableName);
Insert Into TargetDatabase.TableName (SELECT * FROM SourceDatabase.TableName);
Insert Into TargetDatabase.TableName (SELECT * FROM SourceDatabase.TableName);
...
Commit
END;
How can I write that in a dynamic way that Oracle accepts this statement?
Do you really mean “database” in the Oracle definition of the term? Or do you mean “schema”? The pseudocode you posted appears to be assuming that
SourceDatabaseandTargetDatabaseare schemas in a single database. If you really mean to indicate that they are separate databases, you would needd to use database links to query the remote tables.Assuming you mean schemas
Note that it’s generally a good idea to generate the dynamic SQL statement in a separate variable that you can print out because that makes debugging the code much easier. Otherwise, if there is an error, there is no way to retrieve the actual SQL statement that your code tried to execute.