0

Let’s say I have a procedure with a user input parameter.

Create procedure Match_Record(v_match_ID)

Cursor match is
Select *
From table A
Where not exists(select ‘bbb’ from table B
                              Where a. v_match_ID = b. v_match_ID);

It won’t compile because v_match_ID is not a valid identifier.

So is execute immediate the only option here?

Ben
  • 51,770
  • 36
  • 127
  • 149
dozel
  • 127
  • 1
  • 3
  • 9
  • 2
    Are you trying to pass in a column name or a literal here? If `V_MATCH_ID` is a column name then yes, you'll need to use execute immediate (or DBMS_SQL). – Ben Dec 27 '17 at 16:15
  • @Ben yes, I’m trying to pass a column name. – dozel Dec 27 '17 at 16:15
  • I don't know what question to close this as a duplicate of because there are so many things you might be doing but here are some relevant ones: https://stackoverflow.com/q/12956627/458741, https://stackoverflow.com/q/15786953/458741, https://stackoverflow.com/q/25182858/458741 – Ben Dec 27 '17 at 16:20

1 Answers1

2

"I’m trying to pass a column name"

In which case, yes, you need to use dynamic SQL. Rather than execute immediate probably you need to open a cursor variable.

Create procedure Match_Record(v_match_ID)
    ... 
    rc sys_refcursor;
begin
    open rc for 
        'Select *
         From table A
         Where not exists(select blah from table B
                           Where a.'||v_match_ID||' = b.'|| v_match_ID||')';
APC
  • 144,005
  • 19
  • 170
  • 281