Sponsored Content
Top Forums Shell Programming and Scripting Oracle 11g script read from file in where clause (RHEL 5.7) Post 302883107 by durden_tyler on Friday 10th of January 2014 06:30:12 PM
Old 01-10-2014
Looks like you've hit a limitation of SQL*Plus. The documentation of SQL*Plus shipped with Oracle 11g R2 at this link:

SQL*Plus Limits

mentions that limit for the command-line length is 2500 characters. That explains the "SP2-0027" errors you encountered.

In your case, since the data was simply a list of comma-delimited values, you could split and align them so that each line is less than 2500 characters. A short Perl program to do that is provided below:

Code:
$
$ cat -n align.pl
     1  #!/usr/bin/perl -w
     2  use strict;
     3
     4  my ($file, $line_size) = @ARGV;                         # read input parameters
     5  open (FH, "<", $file) or die "Can't open $file: $!";
     6  chomp(my $str = <FH>);                                  # read the comma-delimited line into a variable
     7  close (FH) or die "Can't close $file: $!";
     8
     9  my $from_index = 0;                                     # set the from and to indices at the beginning
    10  my $to_index = index($str, ",", $from_index);           # of the string
    11  my $next_index;
    12  my $done = 0;
    13
    14  while ($to_index != -1) {                               # while we still have commas in the string
    15      $next_index = index($str, ",", $to_index + 1);      # determine the position of the next comma
    16      if ($next_index > $line_size - 1) {                 # and if it is higher than the line size,
    17          print substr($str, 0, $to_index + 1), "\n";     # cut the string and print it
    18          $str = substr($str, $to_index + 1);
    19          $from_index = 0;
    20      } elsif ($next_index == -1) {                       # if the next position of the comma is not found
    21          if (length($str) > $line_size) {                # and length of string is greater than the line size
    22            print substr($str, 0, $to_index + 1), "\n";   # then print till the current position of comma
    23          } else {
    24            $done = 1;                                    # otherwise print the string and we are done
    25            print $str, "\n";
    26            last;
    27          }
    28          $str = substr($str, $to_index + 1);             # reset the string so that the printed part is
    29          $from_index = 0;                                # discarded, and reset the from index as well
    30      } else {
    31          $from_index = $to_index + 1;                    # the comma position is within the line size, so
    32      }                                                   # just go to the next iteration
    33      $to_index = index($str, ",", $from_index);
    34  }
    35  if (not $done) {                                        # print only if a chunk was left out at the end
    36      print $str, "\n";
    37  }
$
$ echo "abc,def,ghi,jk,lm,n,opqr,stu,vwx,yz" > mydata.txt
$
$ cat -n mydata.txt
     1  abc,def,ghi,jk,lm,n,opqr,stu,vwx,yz
$
$ perl align.pl mydata.txt 6
abc,
def,
ghi,
jk,lm,
n,
opqr,
stu,
vwx,yz
$
$ perl align.pl mydata.txt 7
abc,
def,
ghi,jk,
lm,n,
opqr,
stu,
vwx,yz
$
$ perl align.pl mydata.txt 8
abc,def,
ghi,jk,
lm,n,
opqr,
stu,vwx,
yz
$
$ perl align.pl mydata.txt 12
abc,def,ghi,
jk,lm,n,
opqr,stu,
vwx,yz
$
$ perl align.pl mydata.txt 20
abc,def,ghi,jk,lm,n,
opqr,stu,vwx,yz
$
$

The program reads the single line from the specified file and cuts it into chunks of the specified line size.

You could then use this program to create a SQL script and run it like so:

Code:
perl align.pl aux.txt 2400 > mydata.txt
echo "select myfield1 from mytable where myfield2 IN (" > exec.sql
cat mydata.txt >> exec.sql
echo ");" >> exec.sql
echo >> exec.sql
sqlplus -s user/password@db @exec.sql

However, you will still hit the limitation if any token in the "IN list" is of length 2500 characters or higher. Most likely, SQL*Plus would not be a good choice in that case.

And as Robin mentioned earlier, you'd have to load such data into a temporary table, perhaps using sqlldr or external tables, and then run your query accordingly.
This User Gave Thanks to durden_tyler For This Post:
 

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Switching user to oracle to connect Oracle 11g DB with 'sysdba'

I need to connect my Oracle 11g DB from shell script with 'sysdba' permissions. To do this I have to switch user from 'root' to 'oracle'. I've tried the following with no success. su - oracle -c "<< EOF1 sqlplus -s "/ as sysdba" << EOF2 whenever sqlerror exit sql.sqlcode;... (2 Replies)
Discussion started by: NetBear
2 Replies

2. UNIX for Beginners Questions & Answers

ksh scripts migrating(Oracle 11G) from Solaris Sparc 10 to RHEL 7

Hi All, Now we are migrating oracle 11G from Solaris Sparc 10 to RHEL 7. We have 1000+ ksh scripts.. Could you please let em know what would be the best way to use exiting scripts in RHEL with minimal changes,. my concern was "Is it all Solaris command work in RHEL". (3 Replies)
Discussion started by: mssprince
3 Replies
All times are GMT -4. The time now is 02:57 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy