Sponsored Content
Top Forums Shell Programming and Scripting Shell script to run sql query having a long listing of parameters Post 302814047 by vsachan on Tuesday 28th of May 2013 04:10:06 PM
Old 05-28-2013
Quote:
Originally Posted by Corona688
The way to avoid too many parameters is not to fiddle and fidget until it accepts too many parameters, as too many parameters will be too many parameters no matter how you cut it. The way to avoid too many parameters is to not use too many parameters.

Perhaps you can avoid reading the file into a shell variable at all:

Code:
( printf "%s" "select name from table_name where cardnum in ('"
        cat parameters.txt
        printf "%s\n" "')" ) | databasecommand

Hi Corona,
Thanks for the suggestion..but still it is not working...
the input text file is like below:
Code:
1234
7654
9654
5412
7653
.
.
.
9873
6513

I need to run sql query inside a script and then store the result in a file..I am doing it like below:

The command below will make the file abc_csv.txt like below...
'1234','7654','9654'......And it will feed as input to the query. Smilie

Code:
cat abc.txt|tr '\n' ','|sed "s/^/'/"|sed "s/,$/'/"|sed "s/,/','/g">abc_csv.txt
pqr=`cat abc_csv.txt`

sqlplus -s user/password@dbschema<<EOF
alter session set db_file_multiblock_read_count=128;
alter session set NLS_DATE_FORMAT='yyyy-mm-dd HH24:mi:ss';
set newpage none feed off
set recsep off
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET LINESIZE 999
SET PAGESIZE 0

set feedback off trimspool on linesize 300
spool sql_out;
SELECT name from table_name where cardnum in ($pqr)
/
spool off;
exit;
EOF

I am able to execute this shell script if there is small data in abc.txt file...but if the file is large ..it is not working

Last edited by Scott; 05-28-2013 at 07:09 PM.. Reason: Code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Passing Parameters For Directory Listing

i have this basic piece of code that i am trying to debug to accept input parameter to be able to display a directory listing of files. cd /u02/app/eatv/dev/out CURDIR=`pwd` echo directory listing of $CURDIR echo if ; then ls -latr else ls -latr $1 fi basically if the script... (9 Replies)
Discussion started by: wtolentino
9 Replies

2. Shell Programming and Scripting

Dynamic SQL query based on shell script parameters

Hi, I need a script that will run a dynamic Oracle SQL. Dynamic meaning the SQL statement depends on the parameter. For instance, something like this: #!/bin/ksh -x # Set environment . /home/mine/set_vars sqlplus $LOGINID <<! >> /home/mine/log.txt select count(1) from $1 where... (2 Replies)
Discussion started by: laiko
2 Replies

3. Shell Programming and Scripting

run sql query via perl script

Hello, If I run this command on the server it works. # dbc "update config set radio_enabled = 0;" how can I execute the same command in perl. I have defined the dbc path. Can any one please correct the last line. #!/usr/bin/perl #database path $dbc='/opt/bin/psql -Userver... (0 Replies)
Discussion started by: sureshcisco
0 Replies

4. Shell Programming and Scripting

specified path name is too long passing parameters to awk via shell script

Hello, I have this shell script that runs awk code by passing in parameters however now it doesn't work anymore with the parameters and I don't know why. It removes duplicates from an input file based on a part of the last field and a key column. It removes the record with the older datetime... (0 Replies)
Discussion started by: script_op2a
0 Replies

5. UNIX for Dummies Questions & Answers

shell script for long listing of groupnames

Hello, When listing the file systems (using ls -ltr) , if the group names are longer the group name is getting truncated. Can someone help with the script which would display the truncated group name? I appreciate if someone could help in this regard. (3 Replies)
Discussion started by: mike12
3 Replies

6. Shell Programming and Scripting

Need help to run sql query from a script..which takes input from a file

I need to run sql script from shell script which takes the input from a file and contents of file will be like : 12345 34567 78657 and query will be like : select seq_nbr from bus_event where event_nbr='12345'; select seq_nbr from bus_event where event_nbr='34567'; select seq_nbr... (1 Reply)
Discussion started by: rkrish
1 Replies

7. Shell Programming and Scripting

How to run a SQL select query in Oracle database through shell script?

I need to run a SQL select query in Oracle database and have to capture the list of retrieved records in shell script. Also i would like to modify the query for certain condition and need to fetch it again. How can i do this? Is there a way to have a persistent connection to oracle database... (9 Replies)
Discussion started by: vel4ever
9 Replies

8. Shell Programming and Scripting

Run SQL thru shell script: how to get a new line when run sql query?

Hi, this's Pom. I'm quite a new one for shell script but I have to do sql on shell script to query some information from database. I found a concern to get a new line...When I run my script, it retrieves all data as wondering but it's shown in one line :( What should I do? I'm not sure that... (2 Replies)
Discussion started by: Kapom
2 Replies

9. UNIX for Dummies Questions & Answers

Script to run sql query.

Please read How To Ask Questions The Smart Way (1 Reply)
Discussion started by: balu_279013
1 Replies

10. Shell Programming and Scripting

Run sql query in shell script and output data save as delimited text

I want to run sql query in shell script and output data save as delimited text (delimited text would be comma) Code: SPOOL_FILE=/pgedw/dan.txt SQL=/pgedw/dan.sql sqlplus -s username/password@myhost:port/servicename <<EOF set head on set COLSEP , set linesize 32767 SET TRIMSPOOL ON SET... (8 Replies)
Discussion started by: Jaganjag
8 Replies
DB2_BIND_PARAM(3)							 1							 DB2_BIND_PARAM(3)

db2_bind_param - Binds a PHP variable to an SQL statement parameter

SYNOPSIS
bool db2_bind_param (resource $stmt, int $parameter-number, string $variable-name, [int $parameter-type], [int $data-type], [int $precision = -1], [int $scale]) DESCRIPTION
Binds a PHP variable to an SQL statement parameter in a statement resource returned by db2_prepare(3). This function gives you more con- trol over the parameter type, data type, precision, and scale for the parameter than simply passing the variable as part of the optional input array to db2_execute(3). PARAMETERS
o $stmt - A prepared statement returned from db2_prepare(3). o $parameter-number - Specifies the 1-indexed position of the parameter in the prepared statement. o $variable-name - A string specifying the name of the PHP variable to bind to the parameter specified by $parameter-number. o $parameter-type - A constant specifying whether the PHP variable should be bound to the SQL parameter as an input parameter ( DB2_PARAM_IN), an output parameter ( DB2_PARAM_OUT), or as a parameter that accepts input and returns output ( DB2_PARAM_INOUT). To avoid memory overhead, you can also specify DB2_PARAM_FILE to bind the PHP variable to the name of a file that contains large object (BLOB, CLOB, or DBCLOB) data. o $data-type - A constant specifying the SQL data type that the PHP variable should be bound as: one of DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG . o $precision - Specifies the precision with which the variable should be bound to the database. This parameter can also be used for retrieving XML output values from stored procedures. A non-negative value specifies the maximum size of the XML data that will be retrieved from the database. If this parameter is not used, a default of 1MB will be assumed for retrieving the XML output value from the stored procedure. o $scale - Specifies the scale with which the variable should be bound to the database. RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 Binding PHP variables to a prepared statement The SQL statement in the following example uses two input parameters in the WHERE clause. We call db2_bind_param(3) to bind two PHP variables to the corresponding SQL parameters. Notice that the PHP variables do not have to be declared or assigned before the call to db2_bind_param(3); in the example, $lower_limit is assigned a value before the call to db2_bind_param(3), but $upper_limit is assigned a value after the call to db2_bind_param(3). The variables must be bound and, for parameters that accept input, must have any value assigned, before calling db2_execute(3). <?php $sql = 'SELECT name, breed, weight FROM animals WHERE weight > ? AND weight < ?'; $conn = db2_connect($database, $user, $password); $stmt = db2_prepare($conn, $sql); // We can declare the variable before calling db2_bind_param() $lower_limit = 1; db2_bind_param($stmt, 1, "lower_limit", DB2_PARAM_IN); db2_bind_param($stmt, 2, "upper_limit", DB2_PARAM_IN); // We can also declare the variable after calling db2_bind_param() $upper_limit = 15.0; if (db2_execute($stmt)) { while ($row = db2_fetch_array($stmt)) { print "{$row[0]}, {$row[1]}, {$row[2]} "; } } ?> The above example will output: Pook, cat, 3.2 Rickety Ride, goat, 9.7 Peaches, dog, 12.3 Example #2 Calling stored procedures with IN and OUT parameters The stored procedure match_animal in the following example accepts three different parameters: o an input (IN) parameter that accepts the name of the first animal as input o an input-output (INOUT) parameter that accepts the name of the second animal as input and returns the string TRUE if an animal in the database matches that name o an output (OUT) parameter that returns the sum of the weight of the two identified animals In addition, the stored procedure returns a result set consisting of the animals listed in alphabetic order starting at the animal corresponding to the input value of the first parameter and ending at the animal corresponding to the input value of the second parameter. <?php $sql = 'CALL match_animal(?, ?, ?)'; $conn = db2_connect($database, $user, $password); $stmt = db2_prepare($conn, $sql); $name = "Peaches"; $second_name = "Rickety Ride"; $weight = 0; db2_bind_param($stmt, 1, "name", DB2_PARAM_IN); db2_bind_param($stmt, 2, "second_name", DB2_PARAM_INOUT); db2_bind_param($stmt, 3, "weight", DB2_PARAM_OUT); print "Values of bound parameters _before_ CALL: "; print " 1: {$name} 2: {$second_name} 3: {$weight} "; if (db2_execute($stmt)) { print "Values of bound parameters _after_ CALL: "; print " 1: {$name} 2: {$second_name} 3: {$weight} "; print "Results: "; while ($row = db2_fetch_array($stmt)) { print " {$row[0]}, {$row[1]}, {$row[2]} "; } } ?> The above example will output: Values of bound parameters _before_ CALL: 1: Peaches 2: Rickety Ride 3: 0 Values of bound parameters _after_ CALL: 1: Peaches 2: TRUE 3: 22 Results: Peaches, dog, 12.3 Pook, cat, 3.2 Rickety Ride, goat, 9.7 Example #3 Inserting a binary large object (BLOB) directly from a file The data for large objects are typically stored in files, such as XML documents or audio files. Rather than reading an entire file into a PHP variable, and then binding that PHP variable into an SQL statement, you can avoid some memory overhead by binding the file directly to the input parameter of your SQL statement. The following example demonstrates how to bind a file directly into a BLOB column. <?php $stmt = db2_prepare($conn, "INSERT INTO animal_pictures(picture) VALUES (?)"); $picture = "/opt/albums/spook/grooming.jpg"; $rc = db2_bind_param($stmt, 1, "picture", DB2_PARAM_FILE); $rc = db2_execute($stmt); ?> SEE ALSO
db2_execute(3), db2_prepare(3). PHP Documentation Group DB2_BIND_PARAM(3)
All times are GMT -4. The time now is 02:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy