Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Dynamic SQL : passing table name as parameter

1062401Dec 30 2013 — edited Dec 31 2013

Hi

I have a SQL query (a store procedure )  that i want to convert to PLSQL

This is a part of my SQL query that i am trying to to find a solution for it, because i cant convert it to oracle :

DECLARE lookupTableRow CURSOR FOR

  SELECT TableName FROM SYS_LookUpTable

  OPEN lookupTableRow

  FETCH NEXT FROM lookupTableRow INTO @tableName

  WHILE @@FETCH_STATUS=0

  BEGIN

  SET @sql='SELECT * FROM '+@tableName

EXECUTE sp_executesql @sql

  IF @counter=0

  BEGIN

  INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table', @tableName)

  END

  ELSE

  BEGIN

  INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table'+CONVERT(NVARCHAR(10),@counter), @tableName)

  END

  SET @counter=@counter+1

  FETCH NEXT FROM lookupTableRow INTO @tableName

  END

  CLOSE lookupTableRow

  DEALLOCATE lookupTableRow

As i understand i can't use ORACLE dynamic sql (execute immediate) when the table name is a parameter

Furthermore when i execute this dynamic query in my SQL store procedure each SELECT statement return me as a result the relevant table rows , those result are different in each loop .

So i cant do this too with ORACLE dynamic sql .

Please advice for any solution

* how can i use dynamic sql with table name as parameter ?

* how can i use a "dynamic" cursor, in order to be able to display the dynamic results ?

Thanks for the advice

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 28 2014
Added on Dec 30 2013
4 comments
8,301 views