{"id":24,"date":"2009-09-30T10:42:44","date_gmt":"2009-09-30T15:42:44","guid":{"rendered":"http:\/\/anvil-of-time.com\/2009\/09\/30\/msg-7391-in-sql-server\/"},"modified":"2021-03-08T10:24:55","modified_gmt":"2021-03-08T15:24:55","slug":"msg-7391-in-sql-server","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/ms-sql-server\/msg-7391-in-sql-server\/","title":{"rendered":"Msg 7391 in SQL Server"},"content":{"rendered":"<p>So I&#8217;m setting up a stored procedure in SQL Server 2000 to access some data on an Oracle database. The DBAs have set up the linked server in SQL Server to the Oracle source with the name &#8216;DTEST&#8217;. I&#8217;m going to need to use the OPENQUERY method since I need the statement to execute in Oracle.<\/p>\n<p>Here is my first attempt:<\/p>\n<pre name=\"code\" class=\"SQL\">SET @sql_str =\n&#039;SELECT DISTINCT RS.POLICY_NUMBER,\n, MAX(RS.POLICYHOLDER_NAME)\n, MAX(RS.POLICYHOLDER_FEDERAL_TAX_ID)\n, MAX(RS.GOVERNING_CLASS_CODE)\nFROM EER.POLICYHOLDER_RISK_STATE RS\nJOIN EER.POLICY_TRANSACTION_FACT TF ON RS.POLICYHOLDER_RISK_ST_KEY = TF.POLICYHOLDER_RISK_ST_KEY\nJOIN EER.POLICY PD ON TF.POLICY_KEY = PD.POLICY_KEY\nWHERE (PD.POLICY_EFF_DATE - 60 &lt;= SYSDATE AND\nPD.POLICY_EXP_DATE + 60 &gt;= SYSDATE AND\nPD.FINAL_TERM_DATE IS NULL)\nGROUP BY RS.POLICY_NUMBER&#039;\n\nSET @sql_str = N&#039;select * from OPENQUERY(&#039;+@env+&#039;, &#039;&#039;&#039; + REPLACE(@sql_str, &#039;&#039;&#039;&#039;, &#039;&#039;&#039;&#039;&#039;&#039;) + &#039;&#039;&#039;)&#039;\nINSERT INTO dbo.ValidPolicyHolders\nexec (@sql_str)<\/pre>\n<p>@env is passed to the procedure, in this case it is &#8216;DTEST&#8217;<\/p>\n<p>Compile the procedure and try to execute it and I get:<\/p>\n<p>OLE DB error trace [OLE\/DB Provider &#8216;MSDAORA&#8217; ITransactionJoin::JoinTransaction returned 0x8004d01b].<br \/>\nMsg 7391, Level 16, State 1, Line 1<br \/>\nThe operation could not be performed because the OLE DB provider &#8216;MSDAORA&#8217; was unable to begin a distributed transaction.<\/p>\n<p>After a bunch of internet searching and checking by the DBAs I try this:<\/p>\n<pre name=\"code\" class=\"SQL\">SET @sql_str = &#039;INSERT INTO dbo.ValidPolicyHolders\nselect * from OPENQUERY(&#039;+@env+&#039;, &#039;&#039;&#039; + REPLACE(@sql_str, &#039;&#039;&#039;&#039;, &#039;&#039;&#039;&#039;&#039;&#039;) + &#039;&#039;&#039;)&#039;\n\nexec (@sql_str)<\/pre>\n<p>And the procedure now performs as expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I&#8217;m setting up a stored procedure in SQL Server 2000 to access some data on an Oracle database. The DBAs have set up the linked server in SQL Server to the Oracle source with the name &#8216;DTEST&#8217;. I&#8217;m going to need to use the OPENQUERY method since I need the statement to execute in&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[15],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/24"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":5,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":1718,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/24\/revisions\/1718"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}