how to pass a variable in WHERE IN clause of oracle sql?

前端 未结 3 950
一向
一向 2021-01-13 06:10

Hi
I have a variable $1 which hold comma separated email addresses like john@example.com,pat@example.com . I wish to pass this variable in a where clause like

         


        
3条回答
  •  鱼传尺愫
    2021-01-13 07:03

    As Pavanred alluded to, the easiest way -- though not necessarily the best -- is to interpolate the values yourself. You don't say what your calling language is, but something like:

    sql = "SELECT something FROM whatever WHERE myColumn in (" + $1 + ")"
    

    However, this means it's very important that you have pre-checked all the values in $1 to make sure that they are either numbers, or properly escaped strings, or whatever else it is that you need to pass but cannot be raw values supplied by a user, to avoid a SQL injection.

    The other option is to make it a two-step process. First, insert the values from $1 into a temporary table, then select those values as a subquery:

    WHERE myColumn in (SELECT temp_value FROM temp_table)
    

提交回复
热议问题