Sponsored Content
Top Forums Shell Programming and Scripting Generate sql statement using shell scripting Post 302965740 by RudiC on Tuesday 2nd of February 2016 01:01:54 PM
Old 02-02-2016
It's a bit difficult to remove chars from a line already printed, so I'd propose to do the whole thing in one single line. Would this do?
Code:
awk -F, -v SQ="'" '
FNR == NR       {if (!/^#/) C[NR] = $0
                 next
                }
                {DL = ""
                 printf "UPDATE EMP SET"
                 for (c in C)   {printf "%s %s = %s%s%s", DL, C[c], SQ, $c, SQ
                                 DL = FS
                                }
                 printf  " WHERE empno = %s;\n", $1
                 print "commit;"
                }
' file[12]
UPDATE EMP SET ename = 'KING', job = 'PRESIDENT', salary = '2000', deptno = '10' WHERE empno = 7839;
commit;
UPDATE EMP SET ename = 'BLAKE', job = 'ANALYST', salary = '1000', deptno = '13' WHERE empno = 7840;
commit;
UPDATE EMP SET ename = 'CLARK', job = 'PROGRAMMER', salary = '500', deptno = '13' WHERE empno = 7841;
commit;
UPDATE EMP SET ename = 'JONES', job = 'PROGRAMMER', salary = '550', deptno = '11' WHERE empno = 7842;
commit;
UPDATE EMP SET ename = 'SCOTT', job = 'ANALYST', salary = '550', deptno = '11 ' WHERE empno = 7843;
commit;

---------- Post updated at 19:01 ---------- Previous update was at 19:00 ----------

I think SQL does accept one line UPDATE statements, doesn't it?

Last edited by RudiC; 02-02-2016 at 02:13 PM..
This User Gave Thanks to RudiC For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sql with shell scripting

Hi, I want to extract data from database table to a flat file using shell scripting. For loading data from file to a table, I had used SQL Loader. How can I do the vice versa using shell scripting. Thanks in advance (3 Replies)
Discussion started by: sendhil
3 Replies

2. Shell Programming and Scripting

using SELECT sql statement in shell script

Hi there I have a database on a remote box and i have been using shell script to insert data into it for example, i could have a script that did this SN=123456 n=server1 m=x4140 sql="UPDATE main SET hostname='$n',model='$m' WHERE serial='$SN';" echo $sql |/usr/sfw/bin/mysql -h... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

3. Shell Programming and Scripting

generate tabular output from an input text file in unix shell scripting

Hi, I have the output (as below) which i want it to be in a table. For e.g. space utilization in PSE on path /logs is 0% space utilization in PSE on path /logs/tuxedo/tuxlsp is 16% space utilization in PSE on path /ldvarlsp/lsp/log is 37% space utilization in PSE on path /home is 6%... (7 Replies)
Discussion started by: pkbond
7 Replies

4. Shell Programming and Scripting

Unix shell command output to a sql statement

Can i do this Say one command sed 's/:*/ /g' $summf is returning C1234 C2345 C3434 some no of rows, now this ouput i have to insert it into a DB table how do i do this?? (2 Replies)
Discussion started by: depakjan
2 Replies

5. Linux

Shell Script to generate Dynamic Param file Using SQL Plus Quey

Hi All, Can anyone give me Shell script sample script to generate Param file by Reading Values from SQL Plus query and it should assign those values to variables like.. $$SChema_Name='ORCL' Thanks in Advance... Srav... (4 Replies)
Discussion started by: Sravana Kumar
4 Replies

6. UNIX for Dummies Questions & Answers

BASH Shell Scripting: If, Then Statement

I'm having trouble trying to create a BASH shell script. I want the user to input a command "cat file_name.c" and then the shell script will delete all comments "/* */" from file_name.c else exit. So far I have this: #!/bin/bash read "cat file" // User will input command cat... (7 Replies)
Discussion started by: inkjoy00
7 Replies

7. Shell Programming and Scripting

Korn shell script - SQL statement challenges

Hi scripting experts. I have some coding challenges that I'm hoping you can help me out. I have one file#1 that contains the following sql statement that spans over multiple lines: sql Select /*+ use_has(a,b) */ * from customer a, customer_address b where a.id = b.id... (1 Reply)
Discussion started by: pchang
1 Replies

8. Shell Programming and Scripting

How to create SQL statement out of data using shell script?

Table TAB1 contains following example data (its a tree sitting in table data format & its driven based CHILD & PARENT column pick the RULE condition to generate the below SQL: CHILD PARENT SS MID MNM VNM RULE FLG 1 ? S1 ? ? V1 rule004 I 2 1 S1 ? ? V1 0 Z 3 1 S1 ? ? V1 1 Z ... (6 Replies)
Discussion started by: gksenthilkumar
6 Replies

9. Shell Programming and Scripting

Using shell to generate case statement

Hi Gurus, I have a very weird requirement and have no clue to resolve the issue. please help me get out this difficulty below two tables, table1 contains the column name. D means this column used for the rule. for example: rule 0 is all columns have value, rule1 is col3 and col7 have no value.... (2 Replies)
Discussion started by: Torhong
2 Replies

10. Shell Programming and Scripting

Shell scripting with case statement

Foe example we have three environments int,qa and prod.Each environment has some number of servers. int=Server1,Server2,Server3 qa=Server4,Server5,Server6 prod=Server7,Server8,Server9 echo "Enter the Environment i.e int,qa,prod" read env case $env in int) ## Need command where all the... (9 Replies)
Discussion started by: nareshreddy443
9 Replies
OCI_ROLLBACK(3) 														   OCI_ROLLBACK(3)

oci_rollback - Rolls back the outstanding database transaction

SYNOPSIS
bool oci_rollback (resource $connection) DESCRIPTION
Reverts all uncommitted changes for the Oracle $connection and ends the transaction. It releases all locks held. All Oracle SAVEPOINTS are erased. A transaction begins when the first SQL statement that changes data is executed with oci_execute(3) using the OCI_NO_AUTO_COMMIT flag. Further data changes made by other statements become part of the same transaction. Data changes made in a transaction are temporary until the transaction is committed or rolled back. Other users of the database will not see the changes until they are committed. When inserting or updating data, using transactions is recommended for relational data consistency and for performance reasons. PARAMETERS
o $connection - An Oracle connection identifier, returned by oci_connect(3), oci_pconnect(3) or oci_new_connect(3). RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 oci_rollback(3) example <?php // Insert into several tables, rolling back the changes if an error occurs $conn = oci_connect('hr', 'welcome', 'localhost/XE'); $stid = oci_parse($conn, "INSERT INTO mysalary (id, name) VALUES (1, 'Chris')"); // The OCI_NO_AUTO_COMMIT flag tells Oracle not to commit the INSERT immediately // Use OCI_DEFAULT as the flag for PHP <= 5.3.1. The two flags are equivalent $r = oci_execute($stid, OCI_NO_AUTO_COMMIT); if (!$r) { $e = oci_error($stid); trigger_error(htmlentities($e['message']), E_USER_ERROR); } $stid = oci_parse($conn, 'INSERT INTO myschedule (startday) VALUES (12)'); $r = oci_execute($stid, OCI_NO_AUTO_COMMIT); if (!$r) { $e = oci_error($stid); oci_rollback($conn); // rollback changes to both tables trigger_error(htmlentities($e['message']), E_USER_ERROR); } // Commit the changes to both tables $r = oci_commit($conn); if (!r) { $e = oci_error($conn); trigger_error(htmlentities($e['message']), E_USER_ERROR); } ?> Example #2 Rolling back to a SAVEPOINT example <?php $stid = oci_parse($conn, 'UPDATE mytab SET id = 1111'); oci_execute($stid, OCI_NO_AUTO_COMMIT); // Create the savepoint $stid = oci_parse($conn, 'SAVEPOINT mysavepoint'); oci_execute($stid, OCI_NO_AUTO_COMMIT); $stid = oci_parse($conn, 'UPDATE mytab SET id = 2222'); oci_execute($stid, OCI_NO_AUTO_COMMIT); // Use an explicit SQL statement to rollback to the savepoint $stid = oci_parse($conn, 'ROLLBACK TO SAVEPOINT mysavepoint'); oci_execute($stid, OCI_NO_AUTO_COMMIT); oci_commit($conn); // mytab now has id of 1111 ?> NOTES
Note Transactions are automatically rolled back when you close the connection, or when the script ends, whichever is soonest. You need to explicitly call oci_commit(3) to commit the transaction. Any call to oci_execute(3) that uses OCI_COMMIT_ON_SUCCESS mode explicitly or by default will commit any previous uncommitted transaction. Any Oracle DDL statement such as CREATE or DROP will automatically commit any uncommitted transaction. SEE ALSO
oci_commit(3), oci_execute(3). PHP Documentation Group OCI_ROLLBACK(3)
All times are GMT -4. The time now is 10:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy