# Parallel SQL sessions in shell script

**URL:** <https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064>\
**Category:** Shell Programming and Scripting\
**Created:** [October 30, 2012, 12:09pm UTC](https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064 "2012-10-30T12:09:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![only4satish](https://community.unix.com/letter_avatar/only4satish/32/6_22734026db12803ebb3e059622ac3534.png) [@only4satish](https://community.unix.com/u/only4satish)\
**Post date:** [October 30, 2012, 12:09pm UTC](https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064/1 "2012-10-30T12:09:40Z")

</div>

i have 3 sqls , sql 1 and sql 2 shuld run in parallel , but sql 3 should run after completion f sql1 nd sql2, my code is as below, please suggest the changes

```nohighlight
 
sqlplus username1/password1@DB1 @sql >> log1 &
sqlplus username2/password2@DB2 @sql2 >> log1 &
 

```

how can i execute the sql3 after completion f sql1 & sql2

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [October 30, 2012, 12:11pm UTC](https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064/2 "2012-10-30T12:11:17Z")

</div>

You can put a wait command after sql 1 & sql 2

```nohighlight
sqlplus username1/password1@DB1 @sql >> log1 &
sqlplus username2/password2@DB2 @sql2 >> log1 &
wait
sqlplus username3/password3@DB3 @sql3 >> log3 &

```

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_22734026db12803ebb3e059622ac3534.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [October 30, 2012, 12:15pm UTC](https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064/3 "2012-10-30T12:15:17Z")

</div>

assuming what you posted works, which I am not all sure is true:

```nohighlight
sqlplus username1/password1@DB1 @sql >> log1 &
sqlplus username2/password2@DB2 @sql2 >> log1 &
wait
sqlplus username1/password1@DB1 @sql2 >> log1 &

```

PS:  
each sqlplus line should look like this

```nohighlight
(
sqlplus username1/password1@DB1 <<EOF >> log1 
   @sql1
   exit
EOF
) &

```

It is called a here document.

---

<div class="post-metadata">

**Author:** ![only4satish](https://community.unix.com/letter_avatar/only4satish/32/6_22734026db12803ebb3e059622ac3534.png) [@only4satish](https://community.unix.com/u/only4satish)\
**Post date:** [October 31, 2012, 12:52pm UTC](https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064/4 "2012-10-31T12:52:39Z")

</div>

Hi bipinagith............if sql2 is completed and sql1 is still running, will the wait command still waits for sql1 to complete before it kicks off sql3

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [October 31, 2012, 12:55pm UTC](https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064/5 "2012-10-31T12:55:13Z")

</div>

Yes, wait command will hold until both sql1 & sql2 are completed, then only sql3 will be called.

---

<div class="post-metadata">

**Author:** ![only4satish](https://community.unix.com/letter_avatar/only4satish/32/6_22734026db12803ebb3e059622ac3534.png) [@only4satish](https://community.unix.com/u/only4satish)\
**Post date:** [October 31, 2012, 1:10pm UTC](https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064/6 "2012-10-31T13:10:47Z")

</div>

thanks, if i want to know the execution time of sql1 and sql2 and it should display at terminal.

```nohighlight
sqlplus username1/password1@DB1 @sql >> log1 &
sqlplus username2/password2@DB2 @sql2 >> log1 &
while ($? -eq 0)
do
let time=0
echo " sql still running from $time min........"
sleep 60
let time=$time+1
done
wait
sqlplus username1/password1@DB3 @sql3 >> log3 &
 

```

please do the corrections

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [October 31, 2012, 1:16pm UTC](https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064/7 "2012-10-31T13:16:03Z")

</div>

That while loop is not gonna work. You can grab the time before and after calling SQL1 & SQL2:-

```nohighlight
echo "Timestamp: `date`"
sqlplus username1/password1@DB1 @sql1 >> log1 &
sqlplus username2/password2@DB2 @sql2 >> log1 &
wait
echo "Timestamp: `date`"

```

And then get the difference for knowing execution time.

---

<div class="post-metadata">

**Author:** ![only4satish](https://community.unix.com/letter_avatar/only4satish/32/6_22734026db12803ebb3e059622ac3534.png) [@only4satish](https://community.unix.com/u/only4satish)\
**Post date:** [November 1, 2012, 7:16am UTC](https://community.unix.com/t/parallel-sql-sessions-in-shell-script/319064/8 "2012-11-01T07:16:04Z")

</div>

thanks
