\documentclass{article}
\newcommand{\bs}{\ensuremath{\backslash}}
\newcommand{\vs}{\vspace{3mm}}
\usepackage{makeidx}
\usepackage[pdftex
	,pagebackref=true
	,colorlinks=true
	,linkcolor=blue
	,unicode]{hyperref}
\begin{document}
\title{SQL\TeX\\v2.0}
\date{Januari 12, 2016}
\author{Oscar van Eijk}
\maketitle
\hrulefill
\tableofcontents
\hrulefill

\section{Introduction}

SQL\TeX\ is a preprocessor to enable the use of SQL statements in \LaTeX. It is a perl script that reads
an input file containing the SQL commands, and writes a \LaTeX\ file that can be processed with your
\LaTeX\ package.

The SQL commands will be replaced by their values. It's possible to select a single field for substitution
substitution in your \LaTeX\ document, or to be used as input in another SQL command.

When an SQL command returns multiple fields and or rows, the values can only be used for substitution
in the document.

\subsection{Known limitations}

\begin{itemize}
\item The \LaTeX\ \texttt{\bs includeonly} directive is ignored; all documents included with \texttt{\bs include} will be parsed and written to the output file.
\item Currently, only 9 command- line parameters (1-9), and 10 variables (0-9) can be used in SQL statements.
\item Replace files can hold only 1,000 items.
\item In multidocument mode, only one parameter can be retrieved.
\end{itemize}

\section{Installing SQL\TeX}

Before installing SQL\TeX, you need to have it. The latest version can always be found at
\textsf{http://freeware.oveas.com/sqltex}.
The download consists of this do\-cumentation, an installation script for Unix
(\texttt{install}), and the Perl script \texttt{SQLTeX}, and a replace- file (\texttt{SQLTeX\_r.dat}) for manual installation
on non- unix platforms\footnote{on Unix, this file will be generated by the install script}.

\vs

On a Unix system, make sure the file \texttt{install} is executable by issueing
the command:\\
\texttt{bash\$ chmod +x install}\\
then execute it with:\\
\texttt{bash\$ ./install}

The script will ask in which directory SQL\TeX\ should be installed. If you are
logged in as `root', the default will be \texttt{/usr/local/bin}, otherwise the
current directory.\\
Make sure the directory where SQL\TeX\ is installed is in your path.

\vs

For other operating systems, there is no install script, you will have to install
it manually.

On \textsc{OpenVMS} it would be something like:\\
\texttt{\$ COPY SQLTEX.PL SYS\$SYSTEM:\\
\$ COPY SQLTEX.CFG SYS\$SYSTEM:\\
\$ COPY SQLTEX\_R.DAT SYS\$SYSTEM:\\
\$ SET FILE/PROTECTION=(W:R) SYS\$SYSTEM:SQLTEX*.*}\\
However, on \textsc{OpenVMS} you also need to define the command \texttt{SQLTEX} by setting a symbol,
either in the \texttt{LOGIN.COM} for all users who need to execute this script, or in some group-- or
system wide login procedure, with the command:  \\
\texttt{\$ SQLTEX :== "PERL SYS\$SYSTEM:SQLTEX.PL"} 

\subsection{Configuration}\label{config}

The location where SQL\TeX\ is installed also holds the configuration file \texttt{SQLTeX.cfg}.
Multiple configuration files can be created, the command line option \texttt{-c} can be used to
select the requested configuration.

\vs

\noindent\hspace{-3mm}\textit{\underline{Note:}} If a 1.x version of SQL\TeX\ is installed on your system,
make sure you saved the configuration section, which was inline in older versions.

\vs

Some values can be overwritten using command line options (see section~\ref{cmdline}). When the command line options are omitted, the values from the requested configuration file will be used.

\begin{description}

\item[dbdriver] Database driver. The default is \texttt{mysql}.
Other supported databases are \texttt{Pg}, \texttt{Sybase}, \texttt{Oracle},
\texttt{Ingres}, \texttt{mSQL} and \texttt{PostgreSQL}, but also others might
work without modification. \\
If your database driver is not support, look for the function
\texttt{db\_connect} to add support (and please notify me :) 

\item[texex] The default file extension for \LaTeX\ file. When SQL\TeX\ is called, the first
parameter should be the name of the input file. If this filename has no extension,
SQL\TeX\ looks for one with the default extension.

\item[stx] An output file can be given explicitly using the `\texttt{-o}' option. When omitted,
SQL\TeX\ composes an output file name using this string.\\
E.g, if your input file is called \texttt{db-doc.tex}, SQL\TeX\ will produce an
outputfile with the name \texttt{db-doc\_stx.tex}.

\item[rfile\_comment] The comment-sign used in replace files. If this is empty, comments are not allowed in
the replace files.

\item[rfile\_regexploc] This must be part of the value \texttt{rfile\_regexp} below. 

\item[rfile\_regexp] Explains how a regular expression is identified in the replace files (see section~\ref{regexp}). 

\item[cmd\_prefix]\label{prefix} SQL\TeX\ looks for SQL commands in the input file. Commands are specified in the
same way all \LaTeX\ commands are specified: a backslash (\bs) followed by the
name of the command.\\
All SQL\TeX\ commands start with the same string. By default, this is the string
\texttt{\textbf{sql}}. When user commands are defined that start with the same
string, this can be changed here to prevent conflicts.

\item[sql\_open] This string is appended to the \texttt{cmd\_prefix}x to
get the complete SQL\TeX\ command for opening a database.\\
With the default configuration this command is ``\texttt{\bs sqldb}''.

\item[sql\_field] This string is appended to the \texttt{cmd\_prefix} to
get the complete SQL\TeX\ command to read a single field from the database.\\
With the default configuration this command is ``\texttt{\bs sqlfield}''.

\item[sql\_row] This string is appended to the \texttt{cmd\_prefix} to
get the complete SQL\TeX\ command to read one or more rows from the database.\\
With the default configuration this command is ``\texttt{\bs sqlrow}''.

\item[sql\_params] This string is appended to the \texttt{cmd\_prefix} to get the complete SQL\TeX\ command to retrieve a list if fields that will be used as parameters (\texttt{\$PAR1}, see section~\ref{params}) in the multidocument environment (see section~\ref{multidoc}).\\
With the default configuration this command is ``\texttt{\bs sqlparams}''.

\item[sql\_update] This string is appended to the \texttt{cmd\_prefix}
to get the complete SQL\TeX\ command to update one or more rows in the database.\\
With the default configuration this command is ``\texttt{\bs sqlupdate}''.

\item[sql\_start] This string is appended to the \texttt{cmd\_prefix} to get the complete SQL\TeX\ command start a section that will be repeated for every row from an array (see section~\ref{loops}).\\
With the default configuration this command is ``\texttt{\bs sqlstart}''.

\item[sql\_use] This string is appended to the \texttt{cmd\_prefix} to get the complete SQL\TeX\ command use a named variable from the array that is currently being processed in a loop context (see section~\ref{loops}).\\
With the default configuration this command is ``\texttt{\bs sqluse}''.

\item[sql\_end] This string is appended to the \texttt{cmd\_prefix} to get the complete SQL\TeX\ command to end a loop context (see section~\ref{loops}).\\
With the default configuration this command is ``\texttt{\bs sqlend}''.

\item[less\_av \& more\_av] These settings are used to determine how the \textsl{help} output should be displayed. If the command `\texttt{less}' is available on the current system, the output will be parsed through this program. Otherwise the output will be parsed through the program `\texttt{more}' if availeble. Both programs are usually available on Unix system (\texttt{more} is standard on most Unix systems), but ports for other
operating systems are available as well.\\
Set the values to ``0'' for the program(s) that is (are) not available, or if you don't want to use it.\\
If none of these programs is available, the \textsl{help} output is plain echoed to the display.

\item[repl\_step] Replacing strings (see section~\ref{replfiles} below) is done two steps, to prevent values from being replaced twice.
This setting---followed by a three-digit integer - ``000'' to ``999''---is used in the first step and replaces values from the first column.
In the second step, values from the second column replace the temporary value. \\
If the first column in the replace file contains a character sequence that occurs in this temporary value, or if query results might contain the full string followed by three digits, this value might need to be changed in something unique.

\item[alt\_cmd\_prefix] In loop context, this setting is used internally to differentiate between sql statements to process immediately and sql statements on stack.\\
Normally, this setting should never change, but if the value for \texttt{cmd\_prefix} has been changed and a conflict is found, the message ``\texttt{Configuration item 'alt\_cmd\_prefix' cannot start with \textit{<conflicting value>}}'' indicates this setting should change as well.

\end{description}

\subsection{Create replace files}\label{replfiles}

Replace files can be used to substitute values in the output of your SQL commands with a different value. This is especially usefull when the database
contains characters that are special characters in \LaTeX, like the percent sign (`\%'), underscore (`\_') etc.

When SQL\TeX\ is installed, it comes with a standard file---\texttt{SQLTeX\_r.dat}---which is located in the same directory where SQLTeX is installed,
with the following replacements:

\vspace{3mm}

\noindent\begin{verbatim}
$       \$
_       \_
%       \%
&       \&
<       \texttt{<}
>       \texttt{>}
{       \{
}       \}
#       \#
~       \~{}
\       \ensuremath{\backslash}
\end{verbatim}

\vspace{3mm}

These are all single character replacements, but you can add your own replacements that consist of a single character or a character sequence (or even regular expressions, see section~\ref{regexp}).\\
To do so, enter a new line with the character(string) that should be replaced, followed by one or more \texttt{TAB}-character(s) (\textit{not} blanks!) and the character(string) it should be replaced with.

If the first non-blank character is a semicolon (`;'), the line is considered a comment line\footnote{ in the default configuration. See the description for \texttt{rfile\_comment} in section~\ref{config} to change of disable comment lines.}. Blank lines are ignored.

\vspace{3mm}

The contents of the file are case sensitive, so of you add the line: \\
\verb+LaTeX        \LaTeX\+ \\
the word ``LaTeX'' will be changed, but ``latex'' is untouched.

\vspace{3mm}

Different replace files can be created. To select a different replace file for a certain SQL\TeX\ source, use the commandline option
`\texttt{-r \textit{filename}}'. To disable the use of replace files, use `\texttt{-rn}'.

\subsubsection{Regular expressions}\label{regexp}

The replace file can include regular expressions, which are recognized by a pattern given in the configuration setting \texttt{rfile\_regexp}. A part of the pattern, configurable as \texttt{rfile\_regexploc}, will be the actual regular expression.

\vs

By default, \texttt{rfile\_regexploc} is ``\texttt{...}'' and \texttt{rfile\_regexp} is ``\texttt{re(...)}''. If the sequence of three dots can appear anywhere else in the replace file, \texttt{rfile\_regexploc} can be changed to any other sequence of characters, e.g. ``\texttt{regexpHere}''.\\
This also requires \texttt{rfile\_regexp} to be changed. Its new value has to be ``\texttt{re(regexpHere)}''
 
\vs

Both in the default configuration and with the modification example given above, the key for regular expressions is \texttt{re(<\textit{regular expression}>)}, e.g.:\\
\hspace{3mm}\verb+re(<p\.*?>)		\paragraph*{}+ \\
will replace all HTML \texttt{<}p\texttt{>} variants (\texttt{<}p style='font-size: normal'\texttt{>}, \texttt{<}p align='center'\texttt{>} etc)

\vs

An example replacement file using regular expressions to handle HTML codes could look like this:

\noindent\begin{verbatim}
&amp;           \&
<strong>        \textbf{
</strong>       }
<em>            \textit{
</em>           }
re(<br.*?/?>)   \\
re(<p.*?>)      \paragraph*{}
</p>            \\[0pt]
<sup>           $^{
</sup>          }$
re(<span.*?>)   \textsl{
</span>         }
re(<h1.*?>)     \section{
re(<h2.*?>)     \subsection{
re(<h3.*?>)     \subsubsection{
re(</h\d>)      }
\end{verbatim}

\section{Write your SQL\TeX\ file}

For SQL\TeX, you write your \LaTeX\ document just as you're used to. SQL\TeX\ provides you with
some extra commands that you can include in your file.\\
The basic format\footnote{in this document, in all examples will be asumed the default values in the
configuration section as described in section~\ref{config}, have not been changed} of an SQL\TeX\ command is: \\
\texttt{\bs sql\emph{cmd}[options]\{SQL statement\}}

\vs

All SQL\TeX\ commands can be specified anywhere in a line, and can span multiple lines.
When SQL\TeX\ executes, the commands are read, executed, and their results---if they return
any---are written to the output:

\vs

\begin{minipage}[t]{0.5\textwidth}\textsl{Input file:}\\\texttt{\footnotesize{\bs documentclass[article] \\
\bs pagestyle\{empty\} \\
\bs sqldb[oscar]\{mydb\} \\
\bs begin\{document\} \\
}}\end{minipage}\hfill\begin{minipage}[t]{0.5\textwidth}\textsl{Output file:}\\\texttt{\footnotesize{\bs documentclass[article] \\
\bs pagestyle\{empty\} \\
 \\
\bs begin\{document\} \\
}}\end{minipage}

\vs

Above you see the SQL\TeX\ command \texttt{\bs sqldb} was removed. Only the command was removed, not
the \textsl{newline} character at the end of the line, so an empty line will be printed instead.
The example below shows the output is an SQL\TeX\ command was found on a line with other \LaTeX\
directives:

\vs

\begin{minipage}[t]{0.5\textwidth}\textsl{Input file:}\\\texttt{\footnotesize{\bs documentclass[article] \\
\bs pagestyle\{empty\}\bs sqldb[oscar]\{mydb\} \\
\bs begin\{document\} \\
\hrulefill}}\end{minipage}\hfill\begin{minipage}[t]{0.5\textwidth}\textsl{Output file:}\\\texttt{\footnotesize{\bs documentclass[article] \\
\bs pagestyle\{empty\} \\
\bs begin\{document\} \\
}}\end{minipage}

\vs

In these examples the SQL\TeX\ commands did not return a value. When commands actually read from
the database, the returned value is written instead:

\vs

\begin{minipage}[t]{0.5\textwidth}\textsl{Input file:}\\\texttt{\footnotesize{This invoice has \bs sqlfield\{SELECT COUNT(*) FROM INVOICE\_LINE \\
WHERE INVOICE\_NR = 12345\} lines.\\
\hrulefill}}\end{minipage}\hfill\begin{minipage}[t]{0.5\textwidth}\textsl{Output file:}\\\texttt{\footnotesize{This invoice has 4 lines \\
}}\end{minipage}

\subsection{SQL statements}\label{sqlstatements}

This document assumes the reader is familiar with SQL commands. This section only tells something about
implementing them in SQL\TeX\ files, especially with the use of command parameters and variables.
Details about the SQL\TeX\ commands will be described in the next sections.

\vs

Let's look at a simple example. Suppose we want to retreive all header information from the database
for a specific invoice. The SQL statement could look something like this: \\
\texttt{SELECT $\ast$ FROM INVOICE WHERE INVOICE\_NR = 12345;}\\
To implement this statement in an SQL\TeX\ file, the \texttt{\bs sqlrow} command should be used (see
section~\ref{sqlrow}):

First, it is important to know that SQL statements should \textit{not} contain the ending semicolon (;) in
any of the SQL\TeX\ commands. The command in SQL\TeX\ would be:\\
\texttt{\bs sqlrow\{SELECT $\ast$ FROM INVOICE WHERE INVOICE\_NR = 12345\}}

Next, SQL\TeX\ would be useless if you have to change your input file every time you want to generate
the same document for another invoice.

\vs

Therefore, you parameters or variables can be used in your SQL statement. Parameters are given at the command
line (see section~\ref{params}), variables can be defined using the \texttt{\bs sqlfield} command as
described in section~\ref{vars}.

Given the example above, the invoice number can be passed as a parameter by rewriting the command as: \\
\texttt{\bs sqlrow\{SELECT $\ast$ FROM INVOICE WHERE INVOICE\_NR = \$PAR1\}} \\
or as as variable with the code line: \\
\texttt{\bs sqlrow\{SELECT $\ast$ FROM INVOICE WHERE INVOICE\_NR = \$VAR0\}}

Note you have to know what datatype is expected by your database. In the example here the datatype is
\textsc{integer}. If the field ``\textsc{invoice\_nr}'' contains a \textsc{varchar} type, the
\texttt{\$PAR}amater or \texttt{\$VAR}iable should be enclosed by quotes: \\
\texttt{\bs sqlrow\{SELECT $\ast$ FROM INVOICE WHERE INVOICE\_NR = '\$PAR1'\}}

\subsection{Opening the database}

Before any information can be read from a database, this database should be opened.
This is done with the \texttt{\textbf{\bs sqldb}} command.
\texttt{\bs sqldb} requires the name of the dabatase. Optionally, a username and password can be given. When
omitted, SQL\TeX\ assumes no username and password is required to connect to the database (the user that
executes SQL\TeX\ should have access to the specified database). \\
The format of the command is:\\
\texttt{\bs sqldb[username,password]\{database\}}

The command can be used anywhere in your input file, but should occur before the first command that tries to
read data from the database.

\subsection{Reading a single field}\label{sqlfield}

When a single field of information is to be read from the database, the command \texttt{\textbf{\bs sqlfield}}
is used. By default, the command in the inputfile is replaced by its result in the outputfile.\\
The SQL command is enclosed by curly braces. Square brackets can optionally be used to enter some extra options.
Currently, the only supported option is \texttt{setvar} (see section~\ref{vars}).

The full syntax or the \texttt{\bs sqlfield} command is:\\
\texttt{\bs sqlrow[\textit{options}]\{SELECT \textit{fieldname} FROM \textit{tablename} WHERE \textit{your where-clause}\}} \\
By default, the SQL\TeX\ command is replaced with the value returned by the SQL query. This behaviour
can be changed with options.


\subsubsection{Define variables}\label{vars}

The \texttt{\bs sqlfield} can also be used to set a variable. The value returned by the SQL query is not
displayed in this case. Instead, a variable is created which can be used in any other SQL query later in
the document (see also section~\ref{sqlstatements}).

Therefore, the option \texttt{\textbf{[setvar=\textit{n}]}} is used, where \textit{n} is an integer between
0 and 9.

\vs

Suppose you have an invoice in \LaTeX. SQL\TeX\ is executed to retrieve the invoice header information
from the database for a specific customer. Next, the invoice lines are read from the database.

You could pass the invoice number as a paramater to SQL\TeX\ for use in your queries, but that could
change every month. It is easier to :\\
\begin{itemize}
\item pass the customer number as a parameter,
\item retrieve the current date (asuming that is the invoice date as stored in
the database by another program), and store it in a variable: \\
\texttt{\bs sqlfield[setvar=0]\{SELECT DATE\_FORMAT(NOW(), "\%Y-\%m-\%d")\}} \\
This creates a variable that can be used as \texttt{\$VAR0},
\item retrieve the invoice number using the customer number (a command line parameter,
see also section~\ref{params}) and the variable containing the invoice date.
Store this invoice number in \texttt{\$VAR1}: \\
\texttt{\bs sqlfield[setvar=1]\{SELECT INVOICE\_NR FROM INVOICES \\
WHERE CUST\_NR = '\$PAR1' AND INVOICE\_DATE = '\$VAR0'\}}
\item use \texttt{\$VAR1} to retrieve all invoice information.
\end{itemize}

\vs

The SQL queries used here do not display any output in your \LaTeX document.


\subsection{Reading rows of data}\label{sqlrow}

When an SQL query returns more information than one single field, the SQL\TeX\
command \texttt{\textbf{\bs sqlrow}} should be used. As with the \texttt{\bs sqlfield},
command, SQL\TeX\ replaces the command with the values it returns, but \texttt{\bs sqlrow}
accepts different options for formating the output.

\vs

By default, fields are separated by a comma and a blank (`\texttt{,~}'), and rows by 
a newline character (`\texttt{\bs\bs}'). To change this, the options ``\texttt{fldsep}''
and ``\texttt{rowsep}'' can be used.

e.g. In a \texttt{tabular} enviroment the fields should be seperated by an amphesand (\texttt{\&}),
perhaps a line should seperate the rows of information. (\texttt{\bs\bs~\bs hline}).
To do this, the options can be used with \texttt{\bs sqlrow} as shown here: \\
\texttt{\bs sqlrow[fldsep=\&,rowsep=\bs\bs~\bs hline]\{SELECT I.LINE\_NR, A.ARTICLE\_NR, 
A.PRICE, I.AMOUNT, (A.PRICE * I.AMOUNT) FROM ARTICLE A, INVOICE\_LINE I WHERE I.INVOICE\_NR = \$VAR1 
AND I.ARTICLE\_NR = A.ARTICLE\_NR\}}

\vs

This will produce an output like: \\
\texttt{1 \& 9712 \& 12 \& 1 \& 12 \bs\bs~\bs hline \\
2 \& 4768 \& 9.75 \& 3 \& 29.25 \bs\bs~\bs hline \\
3 \& 4363 \& 1.95 \& 10 \& 19.5 \bs\bs~\bs hline \\
4 \& 8375 \& 12.5 \& 2 \& 25 \bs\bs~\bs hline}

\subsubsection{Output rows on seperate lines}

Some \LaTeX\ packages require input on a seperate line. If this output is to be
read from a database, this can be set with the \texttt{rowsep} option using the
fixed text ``\texttt{NEWLINE}''.

\subsubsection{Store data in an array}


The \texttt{\bs sqlrow} command can also be used to store the data in an array. The value returned by the SQL query is not displayed in this case. Instead, an array is created which can be used later the document in a loop context (see section~\ref{loops}).

Therefore, the option \texttt{\textbf{[setarr=\textit{n}]}} is used, where \textit{n} is an integer between
0 and 9.

\subsection{Loop context}\label{loops}

In a loop context, an array if filled with data from the database using \texttt{\bs sqlrow}.\\
Later in the document, the data can be used in a textblock that will be written to the outputfile once for every record retrieved.

\vs

The textblock is between the \texttt{\bs sqlstart\{\textit{n}\}} and \texttt{\bs sqlend\{\textit{n}\}} commands, where \textit{n} is the sequence number of the array to use\footnote{ in \texttt{\bs sqlend}, the sequence number is ignored, but required by syntax.}.

Multiple textblocks can occur in the document, but they can \textit{not} be nested!

\vs

In the example below, data for unpaid invoices is stored in an array identified with sequence number 0:
 
\texttt{\bs sqlrow[setarr=0]\{SELECT I.INVOICE\_NR AS nr\\
\hspace*{15mm},  I.DUE\_DATE AS date\\
\hspace*{15mm},  I.TOTAL AS amount\\
\hspace*{15mm},  C.NAME AS customer\\
\hspace*{15mm}FROM INVOICES I\\
\hspace*{15mm}LEFT OUTER JOIN CUSTOMERS C\\
\hspace*{20mm}ON C.CUST\_NR = I.CUST\_NR\\
\hspace*{15mm}WHERE I.PAY\_DATE = NULL\}}

\vs

To use this data, a textblock must start with: \texttt{\bs sqlstart\{0\}}\\
Between this command and the first occurrence of \texttt{\bs sqlend\{\}}, an unlimited amount\footnote{ limited by your computer's memory only} of \LaTeX\ text can be. Within this text, every occurence of \texttt{\bs sqluse\{<\textit{field name}>\}} will be replaced with the matching field from the current row, e.g.:

\noindent\begin{verbatim}
\sqlstart{0}
\begin{flushright}
Regarding: invoicenumber \sqluse{nr}
\end{flushright}

Dear \sqluse{customer},

On \today, the invoice with number \sqluse{nr}, payable before
\sqluse{date}, was not yet received by us.

We kindly request you to pay the amount of \texteuro\sqluse{amount}
as soon as possible.

\newpage
\sqlend{}
\end{verbatim}


\subsection{Output multiple documents}\label{multidoc}

A single input file can be created to generate more output files. This option
retrieves the first parameter (see section~\ref{params}) from the database
(ignoring any parameters that where given on the command line!).

\vs

The input document must contain the command \texttt{\bs sqlsetparams} (in the
default configuration) without any options. The query that follows can return an
unlimited number of rows all containing exactly 1 field: \\
\texttt{\bs sqlsetparams\{SELECT INVOICE\_NR FROM INVOICES WHERE PAY\_DATE = NULL\}}

\vs

By processing this command, SQL\TeX\ builds a list with all values retrieved and
processes the input file again for each value.\\
In those runs, the queries are executed as described in the previous sections,
using the value as a parameter:\\
\texttt{\bs sqlrow\{SELECT * FROM INVOICES WHERE INVOICE\_NR = \$PAR1\}}

\vs

To enable the multidocument mode, the command line switch \texttt{-m} or \texttt{-M} must be
given and no parameters are allowed. The switches \texttt{-m} and \texttt{-M} cannot be used together.\\
Without the \texttt{-m} or \texttt{-M} switch, a parameter can be given and a single output
document will be created, ignoring the \texttt{\bs sqlsetparams} command.

\vs

With the \texttt{-m} switch, output filenames will be numbered \texttt{\emph{filename}\_1.tex} to \texttt{\emph{filename}\_\emph{n}.tex}.\\
With the \texttt{-M} switch, output filenames will be numbered \texttt{\emph{filename}\_\emph{parameter}.tex}, where \emph{parameter} is the value taken from the
database (\texttt{invoice\_nr} in the example above). Note the parameter will not be formatted to be filename-friendly!\\

\subsection{Update database records}

Since version 1.5, SQL\TeX\ supports database updates as well: \\
\texttt{\bs sqlupdate\{UPDATE INVOICES SET REMINDERS = REMINDERS + 1,
LAST\_REMINDER = NOW() INVOICE\_NR = \$VAR1\}}

\vs

This command accepts no options.

\section{Process your SQL\TeX\ file}

To process you SQL\TeX\ file and create a \LaTeX\ file with all information read from
the database, call SQL\TeX\ with the parameter(s) and (optional) command\-line options as
described here:

\subsection{Parameters}\label{params}

SQL\TeX\ accepts more than one parameter. The first parameter is required; this should
be the input file, pointing to your \LaTeX\ document containing the SQL\TeX\ commands.

By default, SQL\TeX\ looks for a file with extension `\texttt{.tex}'.

\vs

All other parameters are used by the queries, if required. If an SQL query contains the
string \texttt{\$PAR\textit{n}}\footnote{where \textit{n} is a number between 1 and 9. Note 
parameter `0' cannot be used, since that contains the filename!}, it is replaced by that parameter
(see also section~\ref{sqlstatements}).

\subsection{Command line options}\label{cmdline}

SQL\TeX\ accepts the followint command- line options:

\begin{description}
\item[-c \textit{file}] SQL\TeX\ configuration file. Default is \texttt{SQLTeX.cfg}
in the same location where SQL\TeX\ is installed.

\item[-e \textit{string}] add \textit{string} to the output filename:
\texttt{input.tex} will be \texttt{input\textit{string}.tex}. This overwrites
the configuration setting \texttt{stx} \\
In \textit{string}, the values between curly braces \{\} will be substituted:
\begin{description}
\item[P\textit{n}] parameter \textit{n}
\item[M] current monthname (\textit{Mon})
\item[W] current weekday (\textit{Wdy})
\item[D] current date (\textit{yyyymmdd})
\item[DT] current date and time (\textit{yyyymmddhhmmss})
\item[T] current time (\textit{hhmmss})
\end{description}
e.g., the command `\texttt{SQLTeX -e \_\{P1\}\_\{W\} my\_file code}'
will read `\texttt{my\_file.tex}' and write `\texttt{myfile\_code\_Tue.tex}'
The same command, but with option \texttt{-E} would create the
outputfile \texttt{myfile.\_code\_Tuesday}
By default (without \texttt{-e} or \texttt{-E}) the outputfile
\texttt{myfile\_stx.tex} would have been written.
The options \texttt{-E} and \texttt{-e} cannot be used together or with \texttt{-o}.

\item[-E \textit{string}] replace input file extension in outputfile:
\texttt{input.tex} will be \texttt{input.\textit{string}} \\
For further notes, see option \texttt{-e} above.

\item[-f] force overwrite of existing files. By default, SQL\TeX\ exists with a
warning message it the outputfile already exists.

\item[-h] print this help message and exit.

\item[-m] Multidocument mode; create one document for each parameter that is retrieved
from the database in the input document (see section~\ref{multidoc}). This option cannot be used with \texttt{-o}.

\item[-M] Same as -m, but with the parameter in the filename instead of a serial number (see section~\ref{multidoc}).

\item[-N] \texttt{NULL} return values allowed. By default SQL\TeX\ exits if a
query returns an empty set.

\item[-o \textit{file}] specify an output file. Cannot be used with \texttt{-e} or \texttt{-E}.

\item[-p \textit{prefix}] prefix used in the SQL\TeX\ file. Default is \texttt{sql} (see also section~\ref{config}
on page~\pageref{prefix}. This overwrites the configurarion setting \texttt{cmd\_prefix}.

\item[-P] prompt for database password. This overwrites the password in the
input file.

\item[-q] run in quiet mode.

\item[-r \textit{replace}] Specify a file that contains the replace characters (see section~\ref{replfiles}). This is
a list with two \texttt{TAB}- seperated fields per lione. The first field holds a string that will be replaced in the SQL output 

\item[-rn] Do not use a replace file. \texttt{-rn} and \texttt{-r \textit{file}} are handled on the same order in which they appear
on the commandline and overwrite each other.

\item[-s \textit{server}] SQL server to connect to. Default is \texttt{localhost}.

\item[-U \textit{user}] database username. This overwrites the username in the
input file.

\item[-V] print version number and exit.
\end{description}


\section{SQL\TeX\ errors and warnings}

\noindent\textbf{\texttt{no input file specified}}

\vspace{1mm}

\noindent SQL\TeX\ was called without any parameters.\\
\textit{Action:} Specify at least one parameter at the commandline. This parameter should be
the name of your input file.

\vs

\noindent\textbf{\texttt{File \textit{input filename} does not exist}}

\vspace{1mm}

\noindent The input file does not exist.\\
\textit{Action:} Make sure the first parameter points to the input file.

\vs

\noindent\textbf{\texttt{outputfile \textit{output filename} already exists}}

\vspace{1mm}

\noindent The outputfile cannot be created because it already exists.\\
\textit{Action:} Specify another output filename with command line option \texttt{-e},
\texttt{-E} or \texttt{-o}, or force an overwrite with option \texttt{-f} (see also section\ref{cmdline}).

\vs

\noindent\textbf{\texttt{no database opened at line \textit{line nr}}}

\vspace{1mm}

\noindent A query starts at line \textit{line nr}, but at that point no database was opened yet. \\
\textit{Action:} Add an \texttt{\bs sqldb} command prior to the first query statement.

\vs

\noindent\textbf{\texttt{insufficient parameters to substitute variable on line \textit{line nr}}}

\vspace{1mm}

\noindent The query starting at line \textit{line nr} uses a parameter in a \textsc{where}- clause with
\texttt{\$PAR\textit{n}}, where \textit{n} is a number bigger than the number of parameters
passed to SQL\TeX\. \\
\textit{Action:} Specify all required parameters at the command line.

\vs

\noindent\textbf{\texttt{trying to substitute with non existing on line \textit{line nr}}}

\vspace{1mm}

\noindent The query starting at line \textit{line nr} requires a variable \texttt{\$VAR\textit{n}} in its
\textsc{where}- clause, where \textit{n} points to a variable that has not (yet) been set. \\
\textit{Action:} Change the number or set the variable prior to this statement.

\vs

\noindent\textbf{\texttt{trying to overwrite an existing variable on line \textit{line nr}}}

\vspace{1mm}

\noindent At line \textit{line nr}, a \texttt{\bs sqlfield} query tries to set a variable \textit{n}
using the option \texttt{[setvar=\textit{n}]}, but \texttt{\$VAR\textit{n}} already
exists at that point. \\
\textit{Action:} Change the number.

\vs

\noindent\textbf{\texttt{no result set found on line \textit{line nr}}}

\vspace{1mm}

\noindent The query starting at line \textit{line nr} returned a \texttt{NULL} value. If the
option \texttt{-N} was specified at the commandline, this is just a warning message.
Otherwise, SQL\TeX\ exits. \\
\textit{Action:} None.

\vs

\noindent\textbf{\texttt{result set too big on line \textit{line nr}}}

\vspace{1mm}

\noindent The query starting at line \textit{line nr}, called with \texttt{\bs sqlfield} returned more than one field. \\
\textit{Action:} Change your query or use \texttt{\bs sqlrow} instead.

\vs

\noindent\textbf{\texttt{no parameters for multidocument found on line \textit{line nr}}}

\vspace{1mm}

\noindent SQL\TeX\ is executed in multidocument mode, but the statement on line
\textit{line nr} did not provide any parameters for the documents. \\
\textit{Action:} Check your query.

\vs

\noindent\textbf{\texttt{too many fields returned in multidocument mode on \textit{line nr}}}

\vspace{1mm}

\noindent In multidocument mode, the lis of parameters retrieved on line
\textit{line nr} returned more than one fiels per row. \\
\textit{Action:} Check your query.

\vs

\noindent\textbf{\texttt{start using a non-existing array on line \textit{line nr}}}

\vspace{1mm}

\noindent An \texttt{\bs sqlstart} command occurs, but refers to a non-existing array. \\
\textit{Action:} Check the sequence number of the array filled with \texttt{\bs sqlrow[setarr=\textit{n}]} and retrieved with \texttt{\bs sqlstart\{\textit{n}\}} in your input file.

\vs

\noindent\textbf{\texttt{\bs sqluse command encountered outside loop context on line \textit{line nr}}}

\vspace{1mm}

\noindent Data from array is used, but the current input file position is not in the context where this data is available.\\
\textit{Action:} Check the presence and positions of the \texttt{\bs sqlstart} and \texttt{\bs sqlend} commands in your input file.

\vs

\noindent\textbf{\texttt{unrecognized command on line \textit{line nr}}}

\vspace{1mm}

\noindent At line \textit{line nr}, a command was found that starts with ``\texttt{\bs sql}'',
but this command was not recognized by SQL\TeX\. \\
\textit{Action:} Check for typos. If the command is a user- defined command, it will
conflict with default SQL\TeX\ commands. Change the SQL\TeX\ command prefix (see section~\ref{config}).

\vs

\noindent\textbf{\texttt{no sql statements found in \textit{input filename}}}

\vspace{1mm}

\noindent SQL\TeX\ did not find any valid SQL\TeX\ commands. \\
\textit{Action:} Check your input file.


\section{Copyright and disclaimer}

\noindent\hrulefill \\
The SQL\TeX\ project is available from GitHub: https://github.com/oveas/sqltex\\
The latest stable release is always available at http://oveas.com/freeware/overige/sqltex\\
For bugs, questions and comments, please use the issue tracker available at https://github.com/oveas/sqltex/issues

\vspace{3mm}

\noindent Copyright\copyright\ 2001-2016 - Oscar van Eijk, Oveas Functionality Provider

\noindent\hrulefill \\

\noindent This software is subject to the terms of the LaTeX Project Public License; 
see http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html.

\section{History}

\begin{description}
\item[v1.3] \textit{released: Mar 16, 2001}\\First public release
\item[v1.4] \textit{released: May 2, 2002}\\Implemented replace files
\item[v1.4.1] \textit{released: Feb 15, 2005}\\Fix: removed leading whitespaces added to database results befure replace
\item[v1.5] \textit{released: Nov 23, 2007}\\Support for multiple databases\\Implemented database updates (\texttt{sqlupdate})\\Implemented multiple output documents (option \texttt{-m})
\item[v2.0] \textit{released: Jan 12, 2016}\\Fix: Oracle support using ORASID\\Fix: Ensure replacements are handled in the same order as they appear in the replacements file\\Separate configuration file(s)\\Added the options \texttt{-c} and \texttt{-M}\\Support for regular expressions in replace files\\Implemented support for the \LaTeX\ \texttt{\bs input} and \texttt{\bs include} directives\\Implemented loop context\\Skip commentlines\\Project moved from local CVS to GitHub
\end{description}

\end{document}
