<?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: execute immediate for read command in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835107#M4865950</link>
    <description>&lt;P&gt;Note that the procedure argument is sqlfilepath but fullpath is provided to xp_read_file? Assuming that is corrected, this should work with these assumptions&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P&gt;The myScrypt.sql contains valid SQL and is read (inside the procedure context). I would likely want to check that the filecontent variable length &amp;gt; 0 or debug the procedure to check the variable. Note that xp_read_file assumes that the c:\\temp\\myScript.sql is on the same machine as the database server otherwise READ_CLIENT_FILE would need to be used.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;The procedure is running with SQL SECURITY DEFINER (assuming that the myScript.sql cannot be directly run by myuser (i.e., insufficient role privileges).&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;In either case, there will be error messages that would help to explain the issue you are encountering.&lt;/P&gt;</description>
    <pubDate>Mon, 06 Nov 2023 10:01:22 GMT</pubDate>
    <dc:creator>chris_keating</dc:creator>
    <dc:date>2023-11-06T10:01:22Z</dc:date>
    <item>
      <title>execute immediate for read command</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaq-p/13835101</link>
      <description>&lt;P&gt;Is this somehow possible?&lt;/P&gt;
&lt;PRE&gt;execute immediate ('read c:\\\\temp\\\\myScript.sql');
&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Jun 2023 07:54:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaq-p/13835101</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2023-06-02T07:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: execute immediate for read command</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835102#M4865945</link>
      <description>&lt;P&gt;No, READ is a DBISQL command (like INPUT and OUTPUT) whereas EXECUTE IMMEDIATE is executed in the database engine. It's the same reason why stored procedures cannot contain OUTPUT commands.&lt;/P&gt;
&lt;P&gt;What exactly are you trying to achieve?&lt;/P&gt;
&lt;P&gt;Note, you can use &lt;A href="https://dcx.sap.com/index.html#sqla170/en/html/8172234d6ce21014a9dd9f0274387276.html"&gt;PARAMETERS&lt;/A&gt; within DBISQL scripts and therefore parametrize the statements within command files, if that's what you up to.&lt;/P&gt;
&lt;P&gt;And of course, within your SQL code, you can build command lines for DBISQL and any script files and then use xp_cmdshell to run them...&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 08:08:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835102#M4865945</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2023-06-02T08:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: execute immediate for read command</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835103#M4865946</link>
      <description>&lt;P&gt;You could use xp_read_file to supply the content to execute immediate. For example;&lt;/P&gt;
&lt;P&gt;execute immediate(xp_read_file( 'c:\\temp\\myScript.sql' ))&lt;/P&gt;
&lt;P&gt;This assumes that the myScript.sql is accessible on the same machine as the database server. If not, you could use the READ_CLIENT_FILE procedure but that would require a more involved setup.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Jun 2023 13:39:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835103#M4865946</guid>
      <dc:creator>chris_keating</dc:creator>
      <dc:date>2023-06-03T13:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: execute immediate for read command</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835104#M4865947</link>
      <description>&lt;P&gt;The point is:&lt;/P&gt;
&lt;P&gt;I want to encapsulate the read command inside a procedure, so that a user (not dba) can install updates (read sql scripts in database):&lt;/P&gt;
&lt;P&gt;Using &lt;STRONG&gt;xp_read_file&lt;/STRONG&gt; can I get the content of my sql script, but again, I dont hava any means to execute it.&lt;/P&gt;
&lt;P&gt;I tried something like this:&lt;/P&gt;
&lt;PRE&gt;create or replace procedure dba.installupdates(sqlfilepath varchar(100))
begin
declare filecontent long varchar;
set filecontent = xp_read_file (fullpath);
execute immediate (filecontent);
end;
grant execute on dba.installupdates to myuser
&lt;/PRE&gt;

&lt;P&gt;and then I try to install the update so:&lt;/P&gt;
&lt;PRE&gt;setuser myuser;
call dba.installupdates('c:\\\\temp\\\\myscript.sql');
&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Nov 2023 05:50:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835104#M4865947</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2023-11-06T05:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: execute immediate for read command</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835105#M4865948</link>
      <description>&lt;P&gt;If you want to execute a given SQL script against a SQL Anywhere database, you can certainly use DBISQL (or the older dbisqlc) with the script as parameter. Within your procedure, you could build a fitting DBISQL(c) command line and execute that via xp_cmdshell (and check the return value!).&lt;/P&gt;
&lt;P&gt;You might also consider the ALTER DATABASE UPGRADE SCRIPT FILE ... statement to execute user-defined updates.&lt;/P&gt;
&lt;P&gt;Nevertheless, running a user-supplied script (or a script a user could modify before it is "automatically" executed with DBA privilege) raises some security concerns...&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;FWIW, in case you use SQL Remote or MobiLink, schema updates on remotes would usually be done via SQL Remote's PASSTHROUGH mode resp. ML's ALTER DATABASE SCHEMA FROM FILE.. faclility.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 06:14:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835105#M4865948</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2023-11-06T06:14:34Z</dc:date>
    </item>
    <item>
      <title>Re: execute immediate for read command</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835106#M4865949</link>
      <description>&lt;P&gt;You said you tried that, so what is the result?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 06:34:25 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835106#M4865949</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2023-11-06T06:34:25Z</dc:date>
    </item>
    <item>
      <title>Re: execute immediate for read command</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835107#M4865950</link>
      <description>&lt;P&gt;Note that the procedure argument is sqlfilepath but fullpath is provided to xp_read_file? Assuming that is corrected, this should work with these assumptions&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P&gt;The myScrypt.sql contains valid SQL and is read (inside the procedure context). I would likely want to check that the filecontent variable length &amp;gt; 0 or debug the procedure to check the variable. Note that xp_read_file assumes that the c:\\temp\\myScript.sql is on the same machine as the database server otherwise READ_CLIENT_FILE would need to be used.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;The procedure is running with SQL SECURITY DEFINER (assuming that the myScript.sql cannot be directly run by myuser (i.e., insufficient role privileges).&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;In either case, there will be error messages that would help to explain the issue you are encountering.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 10:01:22 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835107#M4865950</guid>
      <dc:creator>chris_keating</dc:creator>
      <dc:date>2023-11-06T10:01:22Z</dc:date>
    </item>
    <item>
      <title>Re: execute immediate for read command</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835108#M4865951</link>
      <description>&lt;P&gt;Thanks for the reply and hints.&lt;/P&gt;
&lt;P&gt;It works now, The problem was that my sql file was coded as UTF-16, I recoded it as UTF-8. 
Is there a way to make xp_read_file able to read UTF-16 files without disruption?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 15:44:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835108#M4865951</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2023-11-06T15:44:19Z</dc:date>
    </item>
    <item>
      <title>Re: execute immediate for read command</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835109#M4865952</link>
      <description>&lt;P&gt;You can use csconvert for this. You will need to know whether the source file  is big-endian or little-endian. For example, toe convert from utf16 little-endian to utf8, &lt;/P&gt;
&lt;P&gt;set stmt = xp_read_file( fullpath );
set stmt = csconvert( stmt, 'utf-8', 'utf-16le' );&lt;/P&gt;
&lt;P&gt;Change &lt;EM&gt;le&lt;/EM&gt; to 
&lt;EM&gt;be&lt;/EM&gt; if it is big-endian.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 16:43:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-immediate-for-read-command/qaa-p/13835109#M4865952</guid>
      <dc:creator>chris_keating</dc:creator>
      <dc:date>2023-11-06T16:43:58Z</dc:date>
    </item>
  </channel>
</rss>

