<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Accessing SQL Server temporary tables across SAS procedures in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291796#M60472</link>
    <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick﻿&lt;/a&gt;for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I couldn't work out how to share/persist a connection on SQL Server when using explicit pass-through, which would be a requirement for my application. &amp;nbsp;I learned a bit more about SQL Server temporary tables and their support in&amp;nbsp;SAS this thread, but I'm not quite there yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   connect using tmp;
   execute (
      select * into ##systables from tempdb.sys.tables;
   ) by tmp;

   select * from connection to tmp (select * from ##systables);
quit;

proc sql;
   connect using tmp;
   select * from connection to tmp (select * from ##systables);
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If anyone knows how to get the 2nd query to work, please let me know. &amp;nbsp;AFAIK, I need a way to persist the first connection, and have the 2nd query re-use that same connection.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bonus points if you can get this to work with #systables instead of ##systables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Oh yeah...SAS 9.3 on Windows Server.&lt;/P&gt;</description>
    <pubDate>Tue, 16 Aug 2016 00:23:27 GMT</pubDate>
    <dc:creator>ScottBass</dc:creator>
    <dc:date>2016-08-16T00:23:27Z</dc:date>
    <item>
      <title>Accessing SQL Server temporary tables across SAS procedures</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291162#M60316</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS 9.3 on Windows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The SAS documentation &lt;A href="http://support.sas.com/documentation/cdl/en/acreldb/65247/HTML/default/viewer.htm#p1h2yn2en7gkb8n1mba66mxgnf56.htm" target="_self"&gt;http://support.sas.com/documentation/cdl/en/acreldb/65247/HTML/default/viewer.htm#p1h2yn2en7gkb8n1mba66mxgnf56.htm&lt;/A&gt;&amp;nbsp;states:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;When you want to use temporary tables that persist across SAS procedures and DATA steps with ODBC, you must use the CONNECTION=SHARED LIBNAME option. When you do this, the temporary table is available for processing until the libref is closed.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my libname statement:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;LIBREF:  TMP
CONNECT: NOPROMPT="Driver={SQL Server Native Client 
10.0};Server=XXXXX,#####;Database=MyDatabase;Trusted_Connection=yes;"
OPTIONS: schema=tmp connection=shared&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i.e. libname &amp;amp;libref odbc &amp;amp;connect &amp;amp;options;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my test code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   connect using tmp;
   execute(
select *
into #temp
from master.sys.tables;
   ) by tmp;

   select * from connection to tmp (
      select * from tmp.#temp
   );
quit;

proc sql;
   connect using tmp;
   select * from connection to tmp (
      select * from tmp.#temp
   );
quit;

data work.foo;
   set tmp.'#temp'n;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The first step works, the&amp;nbsp;second and third ones give these errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: CLI describe error: [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name 
       '#temp'. : [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement(s) could not be 
       prepared.

ERROR: File TMP.'#temp'n.DATA does not exist.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What bit am I not understanding from "&lt;STRONG&gt;...the temporary table is available for processing until the libref is closed."&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2016 07:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291162#M60316</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2016-08-12T07:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing SQL Server temporary tables across SAS procedures</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291166#M60317</link>
      <description>&lt;P&gt;Try creating with double hash i.e. global temporary table&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Change #temp to ##temp.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;##temp would be available till conncetion is active.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2016 07:28:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291166#M60317</guid>
      <dc:creator>RahulG</dc:creator>
      <dc:date>2016-08-12T07:28:46Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing SQL Server temporary tables across SAS procedures</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291169#M60319</link>
      <description>&lt;P&gt;Have you seen this note:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/kb/48/566.html" target="_blank"&gt;http://support.sas.com/kb/48/566.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Apart from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1751"&gt;@RahulG﻿&lt;/a&gt; suggestion, check the ODBC driver version.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2016 07:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291169#M60319</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-08-12T07:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing SQL Server temporary tables across SAS procedures</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291179#M60320</link>
      <description>&lt;P&gt;Ok, I think I can answer my own question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found this link, which was helpful: &lt;A href="http://www.sascommunity.org/planet/blog/category/temp-table/" target="_self"&gt;http://www.sascommunity.org/planet/blog/category/temp-table/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I then created this test code (don't worry about the macro - it simply allocates the SQL Server libref via ODBC):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%libname_sqlsvr(libref=TMP,database=MyDatabase,options=schema=dbo connection=shared bcp=yes)
 
data tmp.'##class'n;
   set sashelp.class;
run;
 
proc sql;
   connect using tmp;
   execute(
      select *
      into ##systables
      from tempdb.sys.tables
   ) by tmp;
 
   select *
   from connection to tmp(
      select * from ##systables
   );
quit;
 
data class;
   set tmp.'##class'n;
run;
 
data systables;
   set tmp.'##systables'n;
run;
 
proc sql ;
   connect using tmp;
   create table systables as
   select *
   from connection to tmp (
      select * from ##systables
   );
   disconnect from tmp;
quit;
 
proc sql ;
   connect using tmp;
   create table class as
   select *
   from connection to tmp (
      select * from ##class
   );
   disconnect from tmp;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;Things worked as expected/desired for #class, but&amp;nbsp;not for #systables. &amp;nbsp;Even though the first SQL query against ##systables showed that ##systables&amp;nbsp;exists (for a brief moment in time), I could never access it in a subsequent step.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also did a search and replace of ## with #, then re-ran the process. &amp;nbsp;What was interesting is that, even though I never could see #class in tempdb via SSMS (because it is a different connection), I could access it from my SAS program (because the libref is keeping the connection open). &amp;nbsp;This is useful, as my temporary files could contain data that, while not super-sensitive, I'd prefer others cannot see via SSMS. &amp;nbsp;However, in this scenario, the final proc sql step failed; I can access #class via the libref, but not via explicit pass-through.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tested with both connection=shared and connection=global with the same results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then the light bulb lit: &amp;nbsp;&lt;U&gt;explicit pass-through isn't going through the SAS library&lt;/U&gt;, so any temporary tables created via explict pass-through won't be available after the connection (proc sql step) ends. &amp;nbsp;This also means that any library options, such as connection=shared, don't apply. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had SSMS open, viewing tempdb.Temporary Tables, refreshing after each SAS step. &amp;nbsp;I could see ##class, but never could see either ##systables, #systables, or #class.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me know if this analysis is flawed. &amp;nbsp;Sorry for the false alarm. &amp;nbsp;I hope this may help someone who finds this via search someday.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2016 08:13:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291179#M60320</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2016-08-12T08:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing SQL Server temporary tables across SAS procedures</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291195#M60323</link>
      <description>&lt;P&gt;Hi Scott,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I guess your analysis is principally right. Question is now: Could the libname and the pass-through connection use the same connection so that you'll always get access to the temporary data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on the example in the following link that should at least be possible for Oracle under SAS9.4&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/acreldb/63647/HTML/default/viewer.htm#a003113602.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/acreldb/63647/HTML/default/viewer.htm#a003113602.htm&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Patrick&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2016 09:52:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291195#M60323</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-08-12T09:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing SQL Server temporary tables across SAS procedures</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291796#M60472</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick﻿&lt;/a&gt;for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I couldn't work out how to share/persist a connection on SQL Server when using explicit pass-through, which would be a requirement for my application. &amp;nbsp;I learned a bit more about SQL Server temporary tables and their support in&amp;nbsp;SAS this thread, but I'm not quite there yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   connect using tmp;
   execute (
      select * into ##systables from tempdb.sys.tables;
   ) by tmp;

   select * from connection to tmp (select * from ##systables);
quit;

proc sql;
   connect using tmp;
   select * from connection to tmp (select * from ##systables);
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If anyone knows how to get the 2nd query to work, please let me know. &amp;nbsp;AFAIK, I need a way to persist the first connection, and have the 2nd query re-use that same connection.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bonus points if you can get this to work with #systables instead of ##systables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Oh yeah...SAS 9.3 on Windows Server.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Aug 2016 00:23:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-SQL-Server-temporary-tables-across-SAS-procedures/m-p/291796#M60472</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2016-08-16T00:23:27Z</dc:date>
    </item>
  </channel>
</rss>

