%ROWTYPE variable from table name

前端 未结 3 1503
北荒
北荒 2021-01-17 07:08

I\'ve got an Oracle procedure, that I want to be somehow generic. I would like to:

  1. pass a table name as a varchar parameter
  2. use EXE
3条回答
  •  没有蜡笔的小新
    2021-01-17 07:44

    You probably can't do this (at least not usefully).

    You could construct an entire anonymous PL/SQL block

    v_plsql := 'DECLARE ' ||
               '  l_row ' || p_table_name || '%rowtype; ' ||
               'BEGIN ' ||
               '  SELECT * ' ||
               '    INTO l_row ' ||
               '    FROM ' || p_table_name ||
               '    WHERE id = ' || p_some_old_value || ';' ||
               ...
    EXECUTE IMMEDIATE v_plsql;
    

    In general, though, long before you start resorting to dynamic PL/SQL at runtime, you really want to take a step back and assess whether there isn't an easier solution to whatever problem you have. There are any number of frameworks, for example, that dynamically generate CRUD packages for each of your tables. That's using dynamic PL/SQL but it's only doing it once as part of a build rather than doing it every time you want to update data.

提交回复
热议问题