title: How to execute compound sql in DB2
body: <p>I am facing a challenge trying to understand DB2 sql (note, I am coming from MS SQL Server) :P.</p>
  
  <p>Here is a scenario, I have got 2 tables one has IDs and other details, second one has lot of other info corresponding to each ID.</p>
  
  <pre><code>  ID              Info for ID
  _______      ____&gt; _______
  |     |     /      |     |
  |  T1 |&lt;---------&gt; |  T2 |
  |_____|     \____&gt; |_____|
  </code></pre>
  
  <p>Coming from SQL Server, I am used to running scripts like:</p>
  
  <pre><code>Declare @ID  int
  Declare @ID1 int
  
  select @ID=ID from T1 where col1 = @ID1
  
  select * from T2 where ID = @ID
  </code></pre>
  
  <p>This all runs fine there and gives me an ID corresponding ID1 which can further be used to obtain all info about ID from T2.</p>
  
  <p>Sadly, in DB2 this explodes right in my face, I am scared if I will execute this query one more time, it will disown me forever :(.</p>
  
  <p>I did some research and wrote this (am even stuck at variable declaration).</p>
  
  <pre><code>--#SET TERMINATOR @
  BEGIN ATOMIC
  DECLARE UID char(30);
  END @
  </code></pre>
  
  <p>For others it worked great, but I am getting following error:</p>
  
  <pre><code>BEGIN ATOMIC
  DECLARE UID char(30);
  END
  
  ILLEGAL USE OF KEYWORD ATOMIC.  TOKEN  WAS EXPECTED. SQLCODE=-199, SQLSTATE=42601, DRIVER=3.63.108
  </code></pre>
  
  <p>Other Info:</p>
  
  <p>IBM DB2 for z/OS V9.1
  IBM Data Studio V3.1.1.0</p>
  
  <p>[EDIT: using DECLARE]
  Another thing I tried that did not work for:</p>
  
  <pre><code>CREATE VARIABLE UID CHAR(30) DEFAULT 'USERID'; 
  select * from testdb2.T1 A WHERE A.UID=v_UID;
  --some other activity goes here
  --and here
  DROP VARIABLE UID;
  </code></pre>
  
  <p>TIA,
  Abhinav
  <hr>
  Update on May 13th, 2016 (Friday the 13th)</p>
  
  <p>Creating a stored proc is the only way of fixing this :(</p>