<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Efficy Overflow Q&amp;A - Recent questions tagged linked-server</title>
<link>https://overflow.efficy.io/?qa=tag/linked-server</link>
<description>Powered by Question2Answer</description>
<item>
<title>Executing sql update statement to linked server</title>
<link>https://overflow.efficy.io/?qa=1003/executing-sql-update-statement-to-linked-server</link>
<description>&lt;p&gt;Hi all,&lt;/p&gt;

&lt;p&gt;If you would ever need to update data that you can access through a linked server in sql server.&lt;/p&gt;

&lt;p&gt;My example, I have a linked server over odbc to an other database.&lt;br&gt;
After importing the data I have to mark the data as imported.&lt;/p&gt;

&lt;p&gt;I tried several solutions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Database.ExecSQL in which I provided in the sql the full path to the linked server&lt;/li&gt;
&lt;li&gt;Database.ExecSQL with the OPENQUERY operator&lt;/li&gt;
&lt;li&gt;Executing a stored procedure which would update the values&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of them was working.&lt;/p&gt;

&lt;p&gt;As last option I used the Database.ExecuteODBCQuery&lt;/p&gt;

&lt;p&gt;This one was working but it is important to include this function in a try catch statement as it will always throw an error as it is expecting a dataset back.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;ExecuteODBCQuery(var QueryHandle: integer; ContextHandle:&lt;br&gt;
  TContextHandle; Connection, SQL: string; ForceRefresh, GetAllRecords:&lt;br&gt;
  boolean; StoreID: integer): TDataSet&lt;br&gt;
  More information on this function can be found in EDN&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Code example on how to use the function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    function executeOdbcQuery(sql, storeId, OdbcConnectionString){
  var QueryHandle = 1;
  var ODBCContext = Database.OpenTemporaryContext;
  try {
    if(sql.indexOf('select') === -1){
      Database.ExecuteODBCQuery(QueryHandle, ODBCContext, OdbcConnectionString, sql, false, false, storeId);
    }else{
      var ds = Database.ExecuteODBCQuery(QueryHandle, ODBCContext, OdbcConnectionString, sql, false, false, storeId);
      return ds;
    }
  }catch(ex){
    database.log(ex.message);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Calling the function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var sql = &quot;insert update query here&quot;;
    OdbcConnectionString=INSERTODBCCONNECTIONSTRING

 executeOdbcQuery(sql,4,OdbcConnectionString)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I hope this helps someone in the future.&lt;/p&gt;

&lt;p&gt;Kind regards,&lt;/p&gt;

&lt;p&gt;Brosens Tom&lt;/p&gt;
</description>
<category>WorkFlow / Serverscript</category>
<guid isPermaLink="true">https://overflow.efficy.io/?qa=1003/executing-sql-update-statement-to-linked-server</guid>
<pubDate>Wed, 09 Dec 2015 13:31:48 +0000</pubDate>
</item>
</channel>
</rss>