title: How to connect DB2 from R?
body: <p>We have installed Data Studio 4.1.0.0 Client to access the data that is stored in DB2. We have installed DB2 11.1 64bit on our PC which has a Windows 7 64 bit.</p>
  
  <p>I need to connect to the DB2 data from 64bit R.</p>
  
  <p>We tried the following</p>
  
  <pre><code>library (RODBC)
  
  driver.name &lt;- "{IBM DB2 ODBC DRIVER}"
  db.name &lt;- "SBXSHRD"
  host.name &lt;- "XX.XXX.X.XX"
  port &lt;- "60012"
  user.name &lt;- "X20XX4"
  pwd &lt;- "SXXXXX01"
  
  #Connection String
  con.text &lt;- paste ("DRIVER =", driver.name,
                     "; Database =", db.name,
                     "; Hostname =", host.name,
                     "; Port =", port,
                     "; PROTOCOL = TCPIP",
                     "; UID =", user.name,
                     "; PWD =", pwd, sep = "")
  
  #Connect to DB2
  con1 &lt;- odbcDriverConnect (con.text)
  
  top &lt;- sqlQuery (con1,
                 "SELECT *
                 FROM ODS_CANALES_LINK.VW_OP_D_TRANSACCIONCANAL
                 where CODMES_PROC = 201708
                 FETCH FIRST 3 ROW ONLY
                 ",
                 errors = FALSE)
  </code></pre>
  
  <p>But I get the following result in r</p>
  
  <pre><code>&gt; con1 &lt;- odbcDriverConnect(con.text)
  Warning messages:
  1: In odbcDriverConnect(con.text) :
    [RODBC] ERROR: state IM004, code 0, message [Microsoft][Administrador de controladores ODBC] Error de SQLAllocHandle del controlador en SQL_HANDLE_ENV
  2: In odbcDriverConnect(con.text) : ODBC connection failed
  </code></pre>
  
  <p>here a detail of the DB2 that we have and a snapshot of what we are doing in R</p>
  
  <p><a href="https://i.stack.imgur.com/5kFax.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p><a href="https://i.stack.imgur.com/xP3Br.jpg" rel="nofollow noreferrer">enter image description here</a></p>