2016-04-03 77 views
1

我有一个sql-select(或插入),它使用相同的两个参数多次。 有没有办法避免对列表中的每个“?,?,?,?,...”使用多个相同的参数?多次使用相同的参数

cursor.execute(statement, list) 

我可以想到两个命名参数,但没有代码注入的可能性。

在下面的例子中,每个左边的“?” RESP。对 ”?”是相同的字符串。为了得到一个结果,我在一个陈述中使用了七项计数。

select count(case (aart like "1%") and (adatum between ? and ?) when 1 then 1 else null end) as AufExt, 
     count(case (aart like "1%E") and (adatum between ? and ?) when 1 then 1 else null end) as AufExtE, 
     count(case (aart like "1%K") and (adatum between ? and ?) when 1 then 1 else null end) as AufExtK, 
     count(case (aart like "2S%") and (adatum between ? and ?) when 1 then 1 else null end) as AufInt, 
     count(case (eart like "3%") and (edatum between ? and ?) when 1 then 1 else null end) as EntExt, 
     count(case (eart like "3%K") and (edatum between ? and ?) when 1 then 1 else null end) as EntExtK, 
     count(case (eart like "2S%") and (edatum between ? and ?) when 1 then 1 else null end) as EntInt 
from tabelle 

对此问题的相关问题:它看起来好像没有索引用于“情况”。正确?

+0

simpler:'sum((eart like'1%')and(adatum between?and?))' –

+0

这个工程!谢谢 –

回答

0

你也可以用子句添加这样的:

with 
    parms as (select ? as parm1, ? as parm2) 

SELECT COUNT(情况(AART如 “1%”)和(parms.parm1和parms.parm2之间Adatum目录),当1然后1,否则返回null结束)作为AufExt ...

+0

一些“案例”使用adatum和一些edatum。为了得到一个结果,我在一个select语句中使用了这七个计数。 –

+0

sory,我很想念 –

1

documentation说:

     不跟随数字的问号将创建一个数字大于已分配的最大参数数量的参数。 [...]
?NNN      问号,接着是多个NNN保持用于NNN个参数的点。 [...]
:AAAA        冒号后跟一个标识符名称,其中包含名为AAAA的命名参数。命名参数也被编号。 [...]为避免混淆,最好避免混合命名和编号参数。

+0

这个作品!谢谢 –

相关问题