# Sql dynamic table / dynamic inserts

**URL:** <https://community.unix.com/t/sql-dynamic-table-dynamic-inserts/250089>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [September 25, 2009, 3:37pm UTC](https://community.unix.com/t/sql-dynamic-table-dynamic-inserts/250089 "2009-09-25T15:37:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![magedfawzy](https://community.unix.com/letter_avatar/magedfawzy/32/6_449c533f586939b6e5f246632a46421f.png) [@magedfawzy](https://community.unix.com/u/magedfawzy)\
**Post date:** [September 25, 2009, 3:37pm UTC](https://community.unix.com/t/sql-dynamic-table-dynamic-inserts/250089/1 "2009-09-25T15:37:58Z")

</div>

I have a file that reads File (X.txt)

Contents of record 1:  
rdrDESTINATION\_ADDRESS (String) "91 971502573813"  
rdrDESTINATION\_IMSI (String) "000000000000000"  
rdrORIGINATING\_ADDRESS (String) "d0 movies"  
rdrORIGINATING\_IMSI (String) "000000000000000"  
rdrTRAFFIC\_EVENT\_TIME (String) "09090801212416"  
rdrTRAFFIC\_EVENT\_TYPE (Long) 8

File (Y.txt)

Contents of record 2:  
rdrDESTINATION\_ADDRESS (String) "91 9715023423813"  
rdrDESTINATION\_IMSI (String) "00000002340000"  
rdrORIGINATING\_ADDRESS (String) "d0 etisalat"  
rdrORIGINATING\_IMSI (String) "000000000000000"

I want to create a dynamic table / insert of values into this table..

```nohighlight
tmpdir=/tmp/records ; mkdir -p $tmpdir ;
tmpfile=/tmp/records/dump.$$
TABLE_COLUMNS=$tmpdir/columns.$$
TABLE_NAME=DUMP_$$
sqlload=$tmpdir/records/sqldump.sql

cat X.txt Y.txt| grep -i rdr | sort -ru > $TABLE_COLUMNS 

echo "CREATE TABLE $TABLE_NAME " > $tmpdir/create_dump_tbl.sql
while read line ; do
echo "$line VARCHAR2(50 BYTE)," >> $tmpdir/create_dump_tbl.sql
TABLE_COL=`echo $TABLE_COL "," $line`
done < $TABLE_COLUMNS

## above has a flaw ..for last line of " , " but anyway, not a big deal

## My Real issue is create an insert dynamically"
for field in `cat X.txt Y.txt`
do
echo "Insert into $TABLE_NAME ($TABLE_COL) VALUES $ " >> $sqlload
done

```

Note that File X.txt has 6 columns  
and File Y.txt has 4 columns,  
so how do i construct the statement, putting nulls in the 2 columns that are empty. (i.e. rdrTRAFFIC\_EVENT\_TIME rdrTRAFFIC\_EVENT\_TYPE )

Note: the columns from the File (x.txt & y.txt) do not appear in order

Thanks
