Search code examples
oracle-databasestored-proceduresplsqlrowidora-00904

ROWID as Parameters in PL/SQL


I am trying to create a helper stored proc to save on repeated code.

I wrote the following stored procedure that takes the table name, status_id, and ROWID.

PROCEDURE sp_update_stage_status(p_table_name IN VARCHAR2,
                                 p_status_id IN NUMBER,
                                 p_rowid IN ROWID)
AS
BEGIN       
  execute immediate 'UPDATE ' || p_table_name
                 || ' SET STATUS_ID = ' || p_status_id 
                 || ' WHERE ROWID = ' || p_rowid;       
END;

However whenever I execute it I get the following.

ORA-00904: "AAATQEAAEAAAAHEAAB": invalid identifier
ORA-06512: at "OBR_DEV.PKG_COMMON", line 32
ORA-06512: at "OBR_DEV.PKG_DIRECTORY", line 449

What am I doing wrong here?


Solution

  • You're dropping the contents of rowid in directly without quoting it.

    Your query became WHERE ROWID = AAATQEAAEAAAAHEAAB which is comparing the rowid column to the AAATQEAAEAAAAHEAAB column.
    It should be WHERE ROWID = 'AAATQEAAEAAAAHEAAB'. Add some quotes to your dynamic SQL and you should be okay.

    Or better yet, use bind variables and don't worry about quoting:

    EXECUTE IMMEDIATE 
      'UPDATE ' || p_table_name || ' SET status_id = :status WHERE rowid = :seek_rowid'
      USING p_status_id, p_rowid;