简体   繁体   English

将两个或多个参数传递给 Oracle SQL

[英]Passing two or more parameters to Oracle SQL

I'm trying to define a list of values (id numbers) for two parameters and pass them to SQL that's querying Oracle tables.我正在尝试为两个参数定义一个值列表(id 编号)并将它们传递给正在查询 Oracle 表的 SQL。

This code works as long as I only include one id and one parameter只要我只包含一个 id 和一个参数,此代码就可以工作

idlist = ['SLCTD']
in_vars = ','.join(':%d' % i for i in range(len(idlist)))
query = """
select PEBEMPL_ECLS_CODE 
from PEBEMPL
inner join SPRIDEN on spriden_pidm = pebempl_pidm 
where SPRIDEN_CHANGE_IND is null
and SPRIDEN_ID in (%s)
""" % in_vars
df = pd.read_sql(query, connection, params=idlist)

Now have tried the following with two parameters and failed:现在尝试了以下两个参数并失败了:

idlist = [735,'SLCTD']
in_vars = ','.join(':%d' % i for i in range(len(idlist)))
query = """
select PEBEMPL_ECLS_CODE 
from PEBEMPL
inner join SPRIDEN on spriden_pidm = pebempl_pidm 
where SPRIDEN_CHANGE_IND is null
and PLANNING_WEEK in (%s)
and SPRIDEN_ID in (%s)
""" % in_vars
df = pd.read_sql(query, connection, params=idlist)

Please advise请指教

Oracle has the following: Oracle 具有以下特性:

If you execute the script from tty you need to do:如果您从 tty 执行脚本,您需要执行以下操作:

sqlplus $USER/$PASSWD @file.sql param_1 param_2

Into the file.sql you may have:进入文件.sql 你可能有:

SELECT '$1', '$2' FROM DUAL;

Where $1 is the first parameter and $2 the second, if you want more, you can put as many parameters as you want.其中 $1 是第一个参数,$2 是第二个参数,如果你想要更多,你可以放任意多的参数。

If you are calling your script from python.如果您从 python 调用您的脚本。 You must to:您必须:

python3 file.py param_1 param_2 ... param_n

into file.py进入文件.py

import sys
sys.argv[1] --> param_1
sys.argv[2] --> param_2
sys.argv[n] --> param_n

For the string formatting issue, try something like this with Python 3:对于字符串格式问题, 尝试使用 Python 3 进行类似操作:

idlist = ['SLCTD']
in_vars = ','.join(':bv%d' % i for i in range(len(idlist)))
query = """
select PEBEMPL_ECLS_CODE 
from PEBEMPL
inner join SPRIDEN on spriden_pidm = pebempl_pidm 
where SPRIDEN_CHANGE_IND is null
and PLANNING_WEEK in ({})
and SPRIDEN_ID in ({})
""".format(in_vars,in_vars)

print(query)

The output is: output 是:

select PEBEMPL_ECLS_CODE 
from PEBEMPL
inner join SPRIDEN on spriden_pidm = pebempl_pidm 
where SPRIDEN_CHANGE_IND is null
and PLANNING_WEEK in (:bv0)
and SPRIDEN_ID in (:bv0)

Since you are reusing bind variable "names" (the bv0), you will need to do 'bind by name' when you execute, which I will leave as an exercise for you to do.由于您正在重用绑定变量“名称”(bv0),因此您需要在执行时执行“按名称绑定”,我将留给您做练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM