<?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>Question Re: %TEMP% in SQL File? in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835656#M4866499</link>
    <description>&lt;P&gt;Gosh, it would help a lot if I read the question before answering, wouldn't it?&lt;/P&gt;
&lt;P&gt;You may find the ISQL READ and PARAMETERS statements useful.&lt;/P&gt;
&lt;P&gt;If you code your script like this, and put it in a file called (for example) logging.sql...&lt;/P&gt;
&lt;PRE&gt;PARAMETERS path;

START LOGGING '{path}\\sql.log';

CREATE TABLE t ( c INTEGER );

STOP LOGGING;

INPUT INTO DBA.DB_UPDATE_LOG (TEXT) FROM '{path}\\sql.log';

COMMIT; 
&lt;/PRE&gt;

&lt;P&gt;and then execute it from a command line that looks like this...&lt;/P&gt;
&lt;PRE&gt;"%SQLANY12%\\bin32\\dbisql.com" -c "ENG=ddd12;DBN=ddd12;UID=dba;PWD=sql" READ ENCODING Cp1252 "logging.sql" [%TEMP%]

PAUSE
&lt;/PRE&gt;

&lt;P&gt;the command window will look something like this...&lt;/P&gt;
&lt;PRE&gt;C:\\projects\\$SA_templates\\run\\dbisql&amp;gt;"C:\\Program Files\\SQL Anywhere 12\\bin32\\dbi
sql.com" -c "ENG=ddd12;DBN=ddd12;UID=dba;PWD=sql" READ ENCODING Cp1252 "logging.
sql" [C:\\Users\\Breck\\AppData\\Local\\Temp]
Importing into table "DBA"."DB_UPDATE_LOG"
1 rows read into table "DBA"."DB_UPDATE_LOG"
Execution time: 0.093 seconds

C:\\projects\\$SA_templates\\run\\dbisql&amp;gt;PAUSE
Press any key to continue . . .
&lt;/PRE&gt;

&lt;P&gt;and the table will look like this...&lt;/P&gt;
&lt;PRE&gt;SELECT * FROM DB_UPDATE_LOG;

text
'CREATE TABLE t ( c INTEGER );'
&lt;/PRE&gt;</description>
    <pubDate>Thu, 16 Aug 2012 13:35:26 GMT</pubDate>
    <dc:creator>Breck_Carter</dc:creator>
    <dc:date>2012-08-16T13:35:26Z</dc:date>
    <item>
      <title>%TEMP% in SQL File?</title>
      <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaq-p/13835653</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;
&lt;P&gt;I was wondering if there is a way to use "OS" environment variable in an sql file.&lt;/P&gt;
&lt;P&gt;Im trying to to something like this -&amp;gt;&lt;/P&gt;
&lt;PRE&gt;START LOGGING '%temp%\\sql.log';

UPDATE...;
CREATE... ;
ALTER... ;

STOP LOGGING;

INPUT INTO DBA.DB_UPDATE_LOG (TEXT) FROM '%temp%\\sql.log';

COMMIT; 
&lt;/PRE&gt;

&lt;P&gt;Works fine, if i use a path without the %temp% variable. I want to avoid "access denied" errors while writing the log file.&lt;/P&gt;
&lt;P&gt;Regards
Stefan&lt;/P&gt;</description>
      <pubDate>Thu, 16 Aug 2012 08:57:11 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaq-p/13835653</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-08-16T08:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: %TEMP% in SQL File?</title>
      <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835654#M4866497</link>
      <description>&lt;P&gt;AFAIK, there's no builtinfunction to get the value of an environment variable in SQL Anywhere. And the feature you're asking for deals with a particular ISQL command, which would be executed outside the database server - so this might be a reasonable product enhancement suggestion.&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;For a general access to environment variables in SQL code, here is a code snippet to do this via an OS SET command re-directed to a file, which is then read via xp_read_file into a local variable. Note that in order to write to a file, you will also have access to a particular directory.&lt;/P&gt;
&lt;P&gt;CAVEAT: Error handling has been omitted... &lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="n"&gt;begin&lt;/SPAN&gt;
   &lt;SPAN class="n"&gt;declare&lt;/SPAN&gt; &lt;SPAN class="n"&gt;strTempDir&lt;/SPAN&gt; &lt;SPAN class="n"&gt;varchar&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;255&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
   &lt;SPAN class="n"&gt;call&lt;/SPAN&gt; &lt;SPAN class="n"&gt;xp_cmdshell&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="s"&gt;'SET TEMP &amp;gt; C:\\\\TempPath.txt'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'no_output'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
   &lt;SPAN class="n"&gt;set&lt;/SPAN&gt; &lt;SPAN class="n"&gt;strTempDir&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="n"&gt;xp_read_file&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="s"&gt;'C:\\\\TempPath.txt'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
   &lt;SPAN class="n"&gt;set&lt;/SPAN&gt; &lt;SPAN class="n"&gt;strTempDir&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="n"&gt;trim&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="nb"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;strTempDir&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="n"&gt;locate&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;strTempDir&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'='&lt;/SPAN&gt;&lt;SPAN class="p"&gt;)&lt;/SPAN&gt;&lt;SPAN class="o"&gt;+&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;1&lt;/SPAN&gt;&lt;SPAN class="p"&gt;));&lt;/SPAN&gt;
   &lt;SPAN class="nb"&gt;select&lt;/SPAN&gt; &lt;SPAN class="n"&gt;strTempDir&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;end&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 16 Aug 2012 09:36:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835654#M4866497</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2012-08-16T09:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: %TEMP% in SQL File?</title>
      <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835655#M4866498</link>
      <description>&lt;P&gt;FWIW, it is also possible to call the Windows API entry point GetEnvironmentVariable().&lt;/P&gt;
&lt;P&gt;CAVEAT EMPTOR: The following snippet of C code (scroll down to see a call from SQL) shows a function from an old archive. The function is no longer used, and it was written only for 32-bit Windows... so, no guarantees &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;//----------------------------------------------------------
// get_environment_variable
//----------------------------------------------------------

__declspec(dllexport) _VOID_ENTRY get_environment_variable ( 
   an_extfn_api *api, void *arg_handle ) {

// For information about GetEnvironmentVariable see: 
   // &lt;A href="http://msdn2.microsoft.com/en-us/library/ms683188.aspx

an_extfn_value" target="test_blank"&gt;http://msdn2.microsoft.com/en-us/library/ms683188.aspx

an_extfn_value&lt;/A&gt;  api_name;
   an_extfn_value  api_value;
   an_extfn_value  api_return_code;
   an_extfn_value  api_diagnostic_code;
   an_extfn_value  api_diagnostic_string;

char *          name; 
   char *          value; 
   DWORD           return_code; 
   DWORD           diagnostic_code; 
   char *          diagnostic_string;

if ( !api -&amp;gt; get_value ( arg_handle, 1, &amp;amp;api_name ) 
         || api_name.data == NULL ) {
      return;
   }

name = (char *) api_name.data;

value = (char *) malloc( 32766 );
   strcpy_s ( value, 32766, "" );

return_code        = 0;     
   diagnostic_code    = 0;     
   diagnostic_string  = (char *) malloc( 255 );
   strcpy_s ( diagnostic_string, 255, "" );

return_code = GetEnvironmentVariable ( 
      (LPCTSTR) name,
      (LPTSTR) value,
      32767 );

if ( return_code &amp;lt;= 0 ) {
      diagnostic_code = 1;
      strcpy_s ( diagnostic_string, 255, 
         "GetEnvironmentVariable failed" );
   }

// CHECK THE ARGUMENT NUMBERS IN THE SET_VALUE CALLS...

api_value.type      = DT_VARCHAR;
   api_value.data      = value;
   api_value.piece_len = ( a_sql_uint32 )( strlen ( value ) );
   api_value.len.total_len = ( a_sql_uint32 )( strlen ( value ) );
   api -&amp;gt; set_value ( arg_handle, 2, &amp;amp;api_value, 0 );

api_return_code.type = DT_INT;
   api_return_code.data = &amp;amp;return_code;
   api -&amp;gt; set_value ( arg_handle, 3, &amp;amp;api_return_code, FALSE );

api_diagnostic_code.type = DT_INT;
   api_diagnostic_code.data = &amp;amp;diagnostic_code;
   api -&amp;gt; set_value ( arg_handle, 4, &amp;amp;api_diagnostic_code, FALSE );

api_diagnostic_string.type = DT_VARCHAR;
   api_diagnostic_string.data = diagnostic_string;
   api_diagnostic_string.piece_len 
      = ( a_sql_uint32 )( strlen ( diagnostic_string ) );
   api_diagnostic_string.len.total_len 
      = ( a_sql_uint32 )( strlen ( diagnostic_string ) );
   api -&amp;gt; set_value ( arg_handle, 5, &amp;amp;api_diagnostic_string, 0 );

free ( value );
   free ( diagnostic_string );

} // get_environment_variable

//------------------------------------------------------------------------------------------------

/* For unit testing...

BEGIN
   DROP PROCEDURE rroad_get_environment_variable;
   EXCEPTION WHEN OTHERS THEN
END;

CREATE PROCEDURE rroad_get_environment_variable (
   IN    name               VARCHAR ( 255 ),
   OUT   value              VARCHAR ( 32766 ),
   OUT   return_code        INTEGER,    -- OK if &amp;gt; 0
   OUT   diagnostic_code    INTEGER,
   OUT   diagnostic_string  VARCHAR ( 255 ) )
EXTERNAL NAME 'get_environment_variable@C:\\\\projects\\\\foxhound\\\\rroad9\\\\Release\\\\rroad1.dll';

BEGIN
DECLARE @name              VARCHAR ( 255 );
DECLARE @value             VARCHAR ( 32766 );
DECLARE @return_code       INTEGER;
DECLARE @diagnostic_code   INTEGER;
DECLARE @diagnostic_string VARCHAR ( 255 );

SET @name = 'SQLANY11';

CALL rroad_get_environment_variable ( 
   @name, 
   @value, 
   @return_code, 
   @diagnostic_code, 
   @diagnostic_string );

SELECT @name, 
       @value,  
       @return_code,  
       @diagnostic_code,  
       @diagnostic_string;

-- @name,@value,@return_code,@diagnostic_code,@diagnostic_string
-- 'SQLANY11','C:\\\\Program Files\\\\SQL Anywhere 11',32,0,''

SET @name = 'UNKNOWN';

CALL rroad_get_environment_variable ( 
   @name, 
   @value, 
   @return_code, 
   @diagnostic_code, 
   @diagnostic_string );

SELECT @name, 
       @value,  
       @return_code,  
       @diagnostic_code,  
       @diagnostic_string;

-- @name,@value,@return_code,@diagnostic_code,@diagnostic_string
-- 'UNKNOWN','',0,1,'GetEnvironmentVariable failed'

END;

*/
&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Aug 2012 09:54:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835655#M4866498</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2012-08-16T09:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: %TEMP% in SQL File?</title>
      <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835658#M4866501</link>
      <description>&lt;P&gt;nice solutions, 
but i can't use them, because i need the variable in "start logging" and in the "input" statement, which are both interactive sql "only" statements . &lt;/P&gt;
&lt;P&gt;I already tried the following&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="n"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN class="n"&gt;VARIABLE&lt;/SPAN&gt; &lt;SPAN class="n"&gt;filename&lt;/SPAN&gt; &lt;SPAN class="n"&gt;varchar&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;255&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;

&lt;SPAN class="n"&gt;set&lt;/SPAN&gt; &lt;SPAN class="n"&gt;filename&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'c:\\temp\\log.log'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="n"&gt;start&lt;/SPAN&gt; &lt;SPAN class="n"&gt;logging&lt;/SPAN&gt; &lt;SPAN class="n"&gt;filename&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;but that creates a log file named "filename" &lt;/P&gt;</description>
      <pubDate>Thu, 16 Aug 2012 10:42:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835658#M4866501</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-08-16T10:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: %TEMP% in SQL File?</title>
      <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835656#M4866499</link>
      <description>&lt;P&gt;Gosh, it would help a lot if I read the question before answering, wouldn't it?&lt;/P&gt;
&lt;P&gt;You may find the ISQL READ and PARAMETERS statements useful.&lt;/P&gt;
&lt;P&gt;If you code your script like this, and put it in a file called (for example) logging.sql...&lt;/P&gt;
&lt;PRE&gt;PARAMETERS path;

START LOGGING '{path}\\sql.log';

CREATE TABLE t ( c INTEGER );

STOP LOGGING;

INPUT INTO DBA.DB_UPDATE_LOG (TEXT) FROM '{path}\\sql.log';

COMMIT; 
&lt;/PRE&gt;

&lt;P&gt;and then execute it from a command line that looks like this...&lt;/P&gt;
&lt;PRE&gt;"%SQLANY12%\\bin32\\dbisql.com" -c "ENG=ddd12;DBN=ddd12;UID=dba;PWD=sql" READ ENCODING Cp1252 "logging.sql" [%TEMP%]

PAUSE
&lt;/PRE&gt;

&lt;P&gt;the command window will look something like this...&lt;/P&gt;
&lt;PRE&gt;C:\\projects\\$SA_templates\\run\\dbisql&amp;gt;"C:\\Program Files\\SQL Anywhere 12\\bin32\\dbi
sql.com" -c "ENG=ddd12;DBN=ddd12;UID=dba;PWD=sql" READ ENCODING Cp1252 "logging.
sql" [C:\\Users\\Breck\\AppData\\Local\\Temp]
Importing into table "DBA"."DB_UPDATE_LOG"
1 rows read into table "DBA"."DB_UPDATE_LOG"
Execution time: 0.093 seconds

C:\\projects\\$SA_templates\\run\\dbisql&amp;gt;PAUSE
Press any key to continue . . .
&lt;/PRE&gt;

&lt;P&gt;and the table will look like this...&lt;/P&gt;
&lt;PRE&gt;SELECT * FROM DB_UPDATE_LOG;

text
'CREATE TABLE t ( c INTEGER );'
&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Aug 2012 13:35:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835656#M4866499</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2012-08-16T13:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: %TEMP% in SQL File?</title>
      <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835659#M4866502</link>
      <description>&lt;P&gt;Thanks this helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Aug 2012 14:23:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835659#M4866502</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-08-16T14:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: %TEMP% in SQL File?</title>
      <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835660#M4866503</link>
      <description>&lt;P&gt;Yes, ISQL parameters - they came to my mind, too:)&lt;/P&gt;
&lt;P&gt;&lt;A href="https://sqlanywhere-forum.sap.com/users/728/stefanz/"&gt;@StefanZ&lt;/A&gt;: Feel free to accept that as the "correct" answer - for a HOWTO, see the FAQ or this &lt;A href="http://sqlanywhere-forum.sap.com/questions/7748"&gt;question&lt;/A&gt;...&lt;/P&gt;</description>
      <pubDate>Fri, 17 Aug 2012 03:07:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835660#M4866503</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2012-08-17T03:07:43Z</dc:date>
    </item>
    <item>
      <title>Re: %TEMP% in SQL File?</title>
      <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835657#M4866500</link>
      <description>&lt;P&gt;Just for the record:&lt;/P&gt;
&lt;P&gt;v16 has introduced the &lt;A href="http://dcx.sybase.com/index.html#sa160/en/dbreference/xp-getenv-system-procedure.html"&gt;&lt;STRONG&gt;xp_get_env()&lt;/STRONG&gt; system procedure&lt;/A&gt; that returns the value of an environment variable.&lt;/P&gt;
&lt;P&gt;(As discussed, it would not help in the particular question as that requires a "DBISQL variable", which is already available via PARAMETERs, as Breck has pointed out.)&lt;/P&gt;</description>
      <pubDate>Tue, 10 Nov 2015 02:25:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835657#M4866500</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2015-11-10T02:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: %TEMP% in SQL File?</title>
      <link>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835661#M4866504</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;AFAIK, there's no builtinfunction to get the value of an environment variable in SQL Anywhere.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That has changed with v16, see my other answer:)&lt;/P&gt;</description>
      <pubDate>Tue, 10 Nov 2015 02:26:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/temp-in-sql-file/qaa-p/13835661#M4866504</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2015-11-10T02:26:53Z</dc:date>
    </item>
  </channel>
</rss>

