From sql Insert Query to XML format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting From sql Insert Query to XML format
# 1  
Old 04-18-2013
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', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('10/26/2012 13:01:20', 'MM/DD/YYYY HH24:MI:SS'), 'FCPM9 ', 'v750 ', 'A');


Need to Translate this to


<Sqd CreDate="2012-10-26 13:02:05" UpdDate="2012-10-26 13:02:05" ModVer="v750" Customizable="true">
<SqdCol ColRef="STATE_GECODE">
<Value>&apos;xx&apos;</Value>
</SqdCol>
<SqdCol ColRef="TAX_TYPE">
<Value>&apos;xx&apos;</Value>
</SqdCol>
<SqdCol ColRef="TAX_AUTHORITY">
<Value>&apos;x&apos;</Value>
</SqdCol>
<SqdCol ColRef="SYS_CREATION_DATE">
<Value>TO_DATE('2012-10-26 13:01:20', 'YYYY-MM-DD HH24:MI:SS')</Value>
</SqdCol>
<SqdCol ColRef="SYS_UPDATE_DATE">
<Value>TO_DATE('2012-10-26 13:01:20', 'YYYY-MM-DD HH24:MI:SS')</Value>
</SqdCol>
<SqdCol ColRef="APPLICATION_ID">
<Value>&apos;FCPM9&apos;</Value>
</SqdCol>
<SqdCol ColRef="DL_SERVICE_CODE">
<Value>&apos;v750&apos;</Value>
</SqdCol>
<SqdCol ColRef="ROUNDING_METHOD">
<Value>&apos;A&apos;</Value>
</SqdCol>
</Sqd>



In Unix.




PS - Table name , / Inserts are subject to change


So what I want is something like this



<Sqd CreDate="$date" UpdDate="$date " ModVer="v750" Customizable="true">
<SqdCol ColRef="$COL1">
<Value$Val1;</Value>
</SqdCol>
<SqdCol ColRef="$COL2">
<Value>$Val2;</Value>
</SqdCol>
.
.
.
<SqdCol ColRef="$COLn">
<Value>$Valn;</Value>
</SqdCol>
</Sqd>



Best Regards

Anuj Krishnakant Puranik
# 2  
Old 04-18-2013
It's good you use code tags. But put the whole section in one tag. You don't need a separate tag for each line. It makes it hard to read...
# 3  
Old 04-18-2013
Here is a gawk code based on some assumptions, feel free to modify it as per your requirement:
Code:
DT=$( date +"%Y-%m-%d %H:%M:%S" )
gawk -F',' -v dt="$DT" '
        BEGIN {
                        IGNORECASE = 1
        }

        NR == 1 {
                        table_name = $3
        }

        NR > 1 {
                        for ( i = 1; i <= NF; i++ )
                        {
                                if ( $i ~ /values/ )
                                        v = 1
                                if ( ($i ~ /\(/ || $i ~ /\)/) && !(v) )
                                        gsub ( /\(|\)/, X, $i )
                                sub(/^[ \t]+/, X, $i)
                                if ( !(v) && $i !~ /^$/ )
                                        C[++m] = $i
                                if ( v && $i !~ /values/ )
                                {
                                        if ( $i ~ /\(/ && $( i + 1 ) ~ /\)/ )
                                        {
                                                j = $i "," $( i + 1 )
                                                V[++n] = j
                                                ++i
                                        }
                                        else if ( $i !~ /^$/ )
                                        {
                                                if ( $i ~ /\(/ || $i ~ /\)/ || $i ~ /\;/ )
                                                        gsub ( /\(|\)|\;/, X, $i )
                                                V[++n] = $i
                                        }
                                }
                        }
        }

        END {
                print "<Sqd CreDate=\"" dt "\" UpdDate=\"" dt "\" ModVer=\"v750\" Customizable=\"true\">"
                for ( k = 1; k <= m; k++ )
                {
                        print "<SqdCol ColRef=\"" C[k] "\">"
                        print "<Value>" V[k] "</Value>"
                        print "</SqdCol>"
                }
                print "</Sqd>"
        }

' sql_file

This User Gave Thanks to Yoda For This Post:
# 4  
Old 05-08-2013
Thanks Yoda , it worked just fine,

Heres the output i got
Code:
<Sqd CreDate="2013-05-08 01:10:42" UpdDate="2013-05-08 01:10:42" ModVer="v750" Customizable="true">
<SqdCol ColRef="STATE_GECODE">
<Value>'xx'</Value>
</SqdCol>
<SqdCol ColRef="TAX_TYPE">
<Value>'xx'</Value>
</SqdCol>
<SqdCol ColRef="TAX_AUTHORITY">
<Value>'x'</Value>
</SqdCol>
<SqdCol ColRef="SYS_CREATION_DATE">
<Value>TO_DATE('10/26/2012 13:01:20', 'MM/DD/YYYY HH24:MI:SS')</Value>
</SqdCol>
<SqdCol ColRef="SYS_UPDATE_DATE">
<Value>TO_DATE('10/26/2012 13:01:20', 'MM/DD/YYYY HH24:MI:SS')</Value>
</SqdCol>
<SqdCol ColRef="APPLICATION_ID">
<Value>'FCPM9 '</Value>
</SqdCol>
<SqdCol ColRef="DL_SERVICE_CODE">
<Value>'v750 '</Value>
</SqdCol>
<SqdCol ColRef="ROUNDING_METHOD">
<Value>'A'</Value>
</SqdCol>
</Sqd>


Last edited by Franklin52; 05-08-2013 at 05:28 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

2. 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

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. 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

5. Shell Programming and Scripting

How to use a variable in insert query?

My script contains as follows, 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... (2 Replies)
Discussion started by: savithavijay
2 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

Convertion of Date Format using SQL query in a shell script

When I write Select date_field from TableA fetch first row only I am getting the output as 09/25/2009. I want to get the output in the below format 2009-09-25 i.e., MM-DD-YYYY. Please help (7 Replies)
Discussion started by: dinesh1985
7 Replies

8. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

9. 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

10. Shell Programming and Scripting

How to Format the result driven from a SQL Query

Hi All, I want to format the result driven from the query into neat format. For example pls find the below code, #! /bin/sh result=' sqlplus -s uname/passwrd@DBname select no,name,address,ph_no, passport_no,salary,designation from emp_table where salary>1000; exit EOF' ... (8 Replies)
Discussion started by: little_wonder
8 Replies
Login or Register to Ask a Question