是否可以在数组

时间:2017-01-06 20:27:41

标签: sql db2

所以有点背景故事是我正在使用一个程序,它使用SQL过滤器对货运单进行分类/评级。问题是它只能是256个字符。所以我试图做的是使用内置的select语句组,如各种邮政编码的费率。

select * from tlorder
where blank = blank
and.......

这是默认的SQL所以我添加了以下内容:Q_INT是一个自定义存储过程。

Q_INT(start_zone) between 55001 and 56799 
and (Q_INT(end_zone) (between 55001 AND 56799
OR Q_INT(end_zone) between 58001 AND 58299
OR Q_INT(end_zone) between 57001 AND 57299
or Q_INT(end_zone) between 50301 AND 50399
or Q_INT(end_zone) between 51001 AND 51399
or Q_INT(end_zone) between 54001 AND 54099
or Q_INT(end_zone) between 54601 AND 54899)

这只能工作很长时间有没有办法设置2个数组并在end_zone部分使用它?像...

and Q_INT(end_zone) between Array[1,12,23] and Array[11,22,33]

感谢。

2 个答案:

答案 0 :(得分:0)

您可以使用它(在DB2 System i V7r1上测试)

 select * 
 from yourtable f1
 inner join table(
                   values 
                   (55001, 56799), 
                   (58001, 58299),
                   (57001, 57299),
                   (50301, 50399),
                   (51001, 51399),
                   (54001, 54099),
                   (54601, 54899)
             ) f2(minval, maxval)
 on Q_INT(f1.end_zone) between f2.minval and f2.maxval
 where Q_INT(f1.start_zone) between 55001 and 56799 

答案 1 :(得分:-1)

为了提高效率,我首先要做的是创建一个带有数字范围的表变量(protip:理想情况下在数据库中有一个永久的Integers表而不是这个):

declare @ints table (
    number int not null primary key
    )

insert @ints
select row_number() over (order by pk_column)
from some_table_with_large_number_of_rows

要做的第二件事是创建一个计算出Q_INT(end_zone)的CTE,这样你就不会多次调用该函数:

;with cte as (
    select *,
        Q_INT(start_zone) as qintsz,
        Q_INT(end_zone) as qintez
    from tlorder
    where blank = blank
    and.......
)

然后,瞧:

select *
from cte
inner join @ints as a
on cte.qintsz = a.number
inner join @ints as b
on cte.qintez = b.number
where a.number between 55001 and 56799
and (
    b.number between 58001 AND 58299
    or
    b.number between 57001 AND 57299
    or 
    b.number between 50301 AND 50399
    or 
    b.number between 51001 AND 51399
    or 
    b.number between 54001 AND 54099
    or 
    b.number between 54601 AND 54899
    )