0

I am storing schema names in table columns (e.g., SOURCE_SCHEMA, TARGET_SCHEMA) and am retrieving the schema name I want to work with as a variable in the DB2 stored procedure (e.g., vSchema).

How do I then use the variable in a SQL statement, for example:

SELECT * from vSchema.TABLE_NAME

Is there a better way to do this? Thanks in advance.

mustaccio
  • 18,234
  • 16
  • 48
  • 57
Philipp
  • 1
  • 1
  • Without additional context I can only tell you that you'll need to use dynamic SQL, e.g. as [shown here](http://stackoverflow.com/questions/6762376/upsert-into-table-with-dynamic-table-name). – mustaccio Oct 17 '14 at 12:01
  • SQL can't compile a SQL `SELECT` statement without knowing the schema and table names. Without those, it has no way to know how to assign memory for values retrieved from the database. If you don't know those names when the proc is created, you have to use dynamic SQL statements that are essentially compiled at run-time every time the proc is called. Review `PREPARE`, `EXECUTE` and `EXECUTE-IMMEDIATE` SQL statements to learn about dynamic SQL. – user2338816 Oct 19 '14 at 06:59
  • @user2338816 Please post your comment as an answer, because I think it is correct and can help other people ;) – bluish May 18 '17 at 14:51
  • You can found a more complete answer at https://stackoverflow.com/questions/36281235/using-dynamic-table-name-in-db2 – Didier68 Nov 20 '17 at 10:02

1 Answers1

-3

Utiliza SET PATH = TU_LIBRERIA, OTRA_LIBRERIA

Xrkoma
  • 1