问题
I'm trying to run a query with NOT IN clause like:
SELECT * FROM table WHERE column NOT IN (?,?,...) (>1000 items)
and I'm getting ORA-01795: maximum number of expressions in a list is 1000 error.
In my script I'm doing something like:
my $lparam = join ', ' => ('?') x @ids;
$lquery = "SELECT * FROM table WHERE column NOT IN ($lparam)";
$lcsr = $zdb->prepare($lquery);
$lcsr->execute( @ids );
I want to split the NOT IN clause to something like where (A not in (a,b,c) AND A not in (d,e,f)) ...
How can we achieve this?
回答1:
Here you go, adding triples and counting them.
my $count = 0;
$lquery = "SELECT * FROM table WHERE (A ";
while (@ids -$count > 3) {
$lquery .= "NOT in (?, ?, ?) AND A ";
$count += 3;
}
my $lparam = join ', ' => ('?') x (@ids - $count);
$lquery .= "NOT IN ($lparam))";
回答2:
You can go with the IN
list using a combination of the column as follows:
SELECT * FROM table WHERE (column,1) NOT IN ((?,1),(?,1),...) (>1000 items)
Here, 1 is used as the second column. And you can give more than 1000 values in the IN
clause list. It is a workaround of skipping the limit.
来源:https://stackoverflow.com/questions/62507305/ora-01795-maximum-number-of-expressions-in-a-list-is-1000-error-in-perl-script