% interface=en output=pdftex

\unprotect 

\def\??sq{@@sq}

\def\startSQLquery#1#2\stopSQLquery
  {\setvalue{\??sq:#1}{#2}}

\def\applySQLquery#1% last line is quit without ; 
  {\bgroup
\def\\{\rawcharacter{`\\}}%
   \long\def\next##1;##2%
     {\ifx##2\empty
        \immediate\write\scratchwrite{##1}%
        \@EA\gobbleuntil\@EA\relax
      \else
        \immediate\write\scratchwrite{##1;}%
        \@EA\next
      \fi##2}%
   \immediate\openout\scratchwrite=\bufferprefix\jobname-sql.sql
   \@EAEAEA\next\csname\??sq:#1\endcsname\empty\empty;\empty\relax
   \immediate\closeout\scratchwrite
   \executeSQLcommand
     {\bufferprefix\jobname-sql.sql}%
     {\bufferprefix\jobname-sql.log}%
   \egroup}

\def\executeSQLcommand#1#2%
%  {\executesystemcommand{c:/mysql/bin/mysql.exe < #1 > #2}}
  {\executesystemcommand{c:/mysql/bin/mysqldump.exe  -r #2 <
#1}}

\protect 

\starttext 

\startSQLquery{test}
  connect cod ;
%  select leerling into outfile "e:/tmp/test.tmp" from ws_p ;
%select data into outfile "e:/tmp/test.tmp" 
select data from ws_p ;
  quit
\stopSQLquery

\applySQLquery{test}

\typefile{e:/tmp/test.tmp}

\stoptext
