How to use a variable in insert query?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use a variable in insert query?
# 1  
Old 08-05-2010
How to use a variable in insert query?

My script contains as follows,

Code:
VALUE=`sqlplus un/pwd <<EOF > OB.txt
set pagesize 0 feedback off verify off heading off echo off
select max(1) from table1;
exit;
EOF`

insert into table2 values(1, 'The max value is $value',...);

i need the value of VALUE to be inserted after 'The max value is '.
please help

Last edited by pludi; 08-05-2010 at 08:05 AM.. Reason: code tags, please...
# 2  
Old 08-05-2010
Quote:
Originally Posted by savithavijay
My script contains as follows,

Code:
VALUE=`sqlplus un/pwd <<EOF > OB.txt
set pagesize 0 feedback off verify off heading off echo off
select max(1) from table1;
exit;
EOF`
 
insert into table2 values(1, 'The max value is $value',...);

i need the value of VALUE to be inserted after 'The max value is '.
...
The INSERT statement is placed incorrectly:

Code:
VALUE=`sqlplus un/pwd <<EOF > OB.txt
set pagesize 0 feedback off verify off heading off echo off
select max(1) from table1;
exit;
EOF`
 
insert into table2 values(1, 'The max value is $value',...); <= at this point, you are out of Oracle, and in the shell. The shell will balk at this SQL statement.

You want something like this in your shell script:

Code:
sqlplus un/pwd <<EOF > OB.txt
set pagesize 0 feedback off verify off heading off echo off
declare
  n number;
begin
  select max(1) into n from table1; -- I really don't think you want to fetch "max(1)", which always returns 1. Maybe it's "max(<column_name>)" ?
  insert into table2 values (1, 'The max value is '||n,...);
end;
/
exit;
EOF

Or you could make it more compact like so -

Code:
sqlplus un/pwd <<EOF > OB.txt
set pagesize 0 feedback off verify off heading off echo off
insert into table2
select 1, 'The max value is '||max(1), ...
from table1;
exit;
EOF

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 08-06-2010
hi tyler

Thanks so much..It worked!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

From sql Insert Query to XML format

Hi How do I translate Let say Cat inserts.sql gives Insert into PM9_TAXATION_ROUNDING (STATE_GECODE, TAX_TYPE, TAX_AUTHORITY, SYS_CREATION_DATE, SYS_UPDATE_DATE, APPLICATION_ID, DL_SERVICE_CODE, ROUNDING_METHOD) Values ('xx', 'xx', 'x', TO_DATE('10/26/2012 13:01:20',... (3 Replies)
Discussion started by: anuj87in
3 Replies

2. Shell Programming and Scripting

Insert query with shell variable with AWK

Hi, I'm a first timer with Unix so pardon my ignorance. I'm trying to read a comma separated file from the same folder where the script is and insert the value in a DB2 table. I'm using AWK for the same. I'm getting `)' not expected error. I'm not sure but for me it doesn't look like detailed... (8 Replies)
Discussion started by: Kabira Speaking
8 Replies

3. Programming

insert query help

Hello i want help to load data from file into mysql DB this part i know how to do but during loading i want to combine 2 fields into 1 field and insert into db as primary key in new column thanks advice how to do so (5 Replies)
Discussion started by: mogabr
5 Replies

4. UNIX for Dummies Questions & Answers

insert variable into awk

I'm trying to insert a filename into awk. filename="12345.wmv" I have tried this: awk '/$filename/{print $0}' infile and this: awk -v fn=$filename '/$fn/{print $0}' infile How do I insert the variable into awk? (1 Reply)
Discussion started by: locoroco
1 Replies

5. Shell Programming and Scripting

Forming an insert query using awk

Hi, I'm trying to form an insert sql query using shell programming. I have table named company with four columns 'company name', 'company id', 'company code' and 'last change id' I have to read the company name, company code and last change id from a file delimited by | which has around 10... (4 Replies)
Discussion started by: rakesh_s
4 Replies

6. Programming

SQL : Fine tune Insert by query

i would like to know how can i fine tune the following query since the cost of the query is too high .. insert into temp temp_1 select a,b,c,d from xxxx .. database used is IDS.. (1 Reply)
Discussion started by: expert
1 Replies

7. Shell Programming and Scripting

need to create a insert query for a file

Hi Guys, I need to create a insert query for the below file Fri Sep 4 06:25:51 2009 ACTION : 'CREATE INDEX S100S_DC.PLInsuranceReportRules_testI1 ON S100S_DC.PLInsuranceReportRules_test1(ENTITY_KEY)' DATABASE USER: '/' PRIVILEGE : SYSDBA CLIENT USER: oracle CLIENT TERMINAL: pts/3... (6 Replies)
Discussion started by: mac4rfree
6 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

add the output of a query to a variable to be used in another query

I would like to use the result of a query in another query. How do I redirect/add the output to another variable? $result = odbc_exec($connect, $query); while ($row = odbc_fetch_array($result)) { echo $row,"\n"; } odbc_close($connect); ?> This will output hostnames: host1... (0 Replies)
Discussion started by: hazno
0 Replies

10. Shell Programming and Scripting

how to insert text using variable

Hi i have a dilemma I need to write a script that takes two arguments. The first being a line of text, the second being a newly created file. The script should take the first argument and insert it into the very top ( the first line) of the file named in your second argument. Note! The... (1 Reply)
Discussion started by: mopimp
1 Replies
Login or Register to Ask a Question