Alternate ways to use SQL query in Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alternate ways to use SQL query in Shell Script
# 1  
Old 07-22-2014
Alternate ways to use SQL query in Shell Script

Hello,

A couple weeks ago I got some great help from a few of you regarding reading an sql query into a shell script. That post can be viewed here. Thanks to all who posted.

The issue I have now is everytime I run the query I get the following error

Code:
SP2-0027: Input is too long (> 2499 characters) - line ignored

My question to you fine people is; Have you come across something like this before and is there an alternate way to read the query into the shell script? I've tried modifying the sql query by adding breaks and carriage returns, however, the issue persists.

My shell script looks like this:

Code:
#!/bin/bash

while read line
do
   sqlquery="$sqlquery $line"
done < /query.sql

sqlplus <<-SQL
   $user/$pass@$sid
   $sqlquery
SQL

# 2  
Old 07-22-2014
I suspect it means what it says -- you are feeding the database queries too long for it to process.

I don't think there's going to be a magic way to get around this limit just by juggling the shell a bit. The limit is in the database. The obvious thing to do would be to give it shorter queries.

Perhaps you can simplify it somehow, squeeze out some extra whitespace, break it into multiple queries which get combined later through the use of a temporary table, etc, etc.
# 3  
Old 07-22-2014
Can you not do something like this:-
Code:
sqlplus <<-EOSQL
   $user/$pass@$sid
   @/path/to/query.sql
EOSQL

This should read the query.sql file as a deck of SQL to run, a bit like when installing/upgrading you do @catproc or perhaps @utllockt to show database locks.



I hope that this helps,
Robin
# 4  
Old 07-22-2014
Will that cause sqlplus to have a larger buffer, though? Too long a line may be too long a line no matter where you shoehorn it.
# 5  
Old 07-22-2014
Quote:
Originally Posted by rbatte1
Can you not do something like this:-
Code:
sqlplus <<-EOSQL
   $user/$pass@$sid
   @/path/to/query.sql
EOSQL

This should read the query.sql file as a deck of SQL to run, a bit like when installing/upgrading you do @catproc or perhaps @utllockt to show database locks.



I hope that this helps,
Robin
Thanks Robin. I agree with Corona, however, modifying the script to include...

Code:
@/path/to/query.sql

...actually made a difference in the output. Now I'm getting the error below, but I think I could find this in the query.

SP2-0552: Bind variable "0" not declared
# 6  
Old 07-23-2014
The error message you see come from the database client, not from the database itself. If you resolve the bind variable issue from your last post you'll run into the first error again.
Oracles maximum query length was 64k in 8i and I do not think that was lowered in newer versions.
If you can not shorten the query statement below the sqlplus-limit using something different to connect to the database may help. Perl for example has libraries to connect to databases and is easy to use (not sure if there are size limits there though).
# 7  
Old 07-23-2014
ok. I guess it's back to the drawing board. I'm not a SQL guru and the query is not mine. This query runs fine when executed from Business Objects, but gives these errors when executing using sqlplus. I guess it's sqlplus that has the limitations.

Thanks to all who chimed in. I'll post back what I've done once I've found a viable solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to execute sql query.

Hi Experts, Need your support. Not able to use sql query alias in shell script. Could you please help me in finding right way to use alias with sql query in shell script. Below is the code i am using. #!/bin/bash sqlplus -s abc/abc@abc << EOF> bcd.csv set trimspool on select zone_id... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

How do I read sql query into shell script?

Hello All, I'm trying to put together a shell script that will: 1. connect to an oracle database 2. execute a query 3. save the output to a csv file I know that I can execute the sqlplus -s user/pass @dbsid and get logged in. What I would like to do is have my query in a separate text... (9 Replies)
Discussion started by: bbbngowc
9 Replies

3. Shell Programming and Scripting

How to embed sql query into our shell script?

Hi I would like to embed a sql query in my shell script. Also, before any the sql query is executed, i would like to validate username and password. (1 Reply)
Discussion started by: arghadeep adity
1 Replies

4. Red Hat

Sql query through shell script

hey , i am using this code to store value of a sql query and and then use it in other query but after some time , but it is not working. please help #!/bin/bash val_1=$( sqlplus -s rte/rted2@rel76d2 << EOF setting heading off select max(stat_id) from cvt_stats; exit EOF ) nohup... (5 Replies)
Discussion started by: ramsavi
5 Replies

5. UNIX for Dummies Questions & Answers

Regarding executing sql query in shell script

Hi, I have one SQL file prepared in UNIX and one script that is executing that. In SQL i have Update and create queries. I want to introduce conditions in SQL file (in UNIX) that if either of the create or update query failes whole transaction should be rollback. I just have 1 create... (2 Replies)
Discussion started by: abhii
2 Replies

6. Shell Programming and Scripting

query sql using shell script

query sql using shell script, is it possible? my friend told me to do a file.sql and link to my shell script, but can i query sql using shell script? thanks in advance! (2 Replies)
Discussion started by: kingpeejay
2 Replies

7. Shell Programming and Scripting

executing a SQL query in shell script

Hi ALL, I need an help in connecting to oracle database, executing a select query and printing it on the screen. Can any one please write a simple code or psuedo code and let me know. select query returns multiple values( say select name from emp) Thanks in advance LM (1 Reply)
Discussion started by: lijju.mathew
1 Replies

8. Shell Programming and Scripting

Executing Sql Query Using Shell Script

HI ALL i have a requirement like this. i have to write a shell script to run a sql query. DB is oracle. once the query is run, the results of the query has to be published in a data file. can you please advice me how to go about it. i am absolutely new to shell scripts and this is a part of my job. (14 Replies)
Discussion started by: ragha81
14 Replies

9. UNIX for Advanced & Expert Users

Connecting DB in the Shell Script to do SQL Query

Any link or example to write shell script for the Connecting Oracle for Quering through SQL thanks in advance ... Cheers !! Mehul Doshi (3 Replies)
Discussion started by: mehuldoshi
3 Replies

10. UNIX for Dummies Questions & Answers

Executing a SQL query from a shell script

I cannot figure out how to run a SQL script, or just a sqlplus query, from a shell script (bash or ksh). Basically, I need to su - oracle from root and run a query, then test the exit status. (3 Replies)
Discussion started by: 98_1LE
3 Replies
Login or Register to Ask a Question