Coding Requirements for Embedded SQL Programs
The following sections discuss coding requirements for writing Embedded SQL statements.
Comments Embedded in Pascal Output
Each Embedded SQL statement generates one comment and a few lines of Pascal code. You may find that the preprocessor translates 50 lines of Embedded SQL into 200 lines of Pascal. This can confuse the program developer who is trying to debug the original source code. To facilitate debugging, each group of Pascal statements associated with a particular statement is preceded by a comment corresponding to the original Embedded SQL source. (Note that only executable Embedded SQL statements are preceded by a comment.) Each comment is one line long and informs the reader of the file name, line number, and type of statement in the original source file.
One consequence of the generated comment is that you cannot comment out embedded statements by putting the opening comment delimiter on an earlier line. You have to put the delimiter on the same line, before the exec keyword, to cause the preprocessor to treat the complete statement as a Pascal comment.
Embedded Statements Inside Pascal If Blocks
As mentioned above, the preprocessor may produce several Pascal statements for a single Embedded SQL statement. However, all the statements generated by the preprocessor are enclosed in Pascal begin and end delimiters, composing a Pascal block. Thus the statement:
if (not dba) then
exec sql select passwd
into :passwd
from security
where usrname = :userid;
will produce legal Pascal code, even though the SQL select statement produces more than one Pascal statement. However, two or more Embedded SQL statements will generate multiple Pascal blocks, so you must delimit them yourself, just as you would delimit two Pascal statements in a single if block. For example:
if (not dba) then
begin
exec frs message 'Confirming your user id';
exec sql select passwd
into :passwd
from security
where usrname = :userid;
end;
Note that, because the preprocessor generates a Pascal block for every Embedded SQL statement, the Pascal compiler may generate the error "Internal Table Overflow" when a single procedure has a very large number of Embedded SQL statements and local variables. You can correct this problem by splitting the file or procedure into smaller components.
All Embedded SQL statements must be terminated by a semicolon. Therefore, because Pascal does not permit semicolons before the else clause of an if statement, you must surround any single Embedded SQL statement that precedes an else clause with a Pascal begin-end block. For example, the following if statement will cause a Pascal error:
if error then
exec frs message 'Error occurred';
{Semicolon required by Embedded SQL}
else
exec frs message 'No error occurred';
By delimiting the then clause with begin-end, you eliminate the error:
if error then
begin
exec frs message 'Error occurred';
{Semicolon required by Embedded SQL}
end {
| ... but that's okay because
| there's no semicolon here
}
else
exec frs message 'No error occurred';
Embedded SQL Statements That Do Not Generate Code
The following Embedded SQL declarative statements do not generate any Pascal code:
declare cursor
declare statement
declare table
whenever
These statements must not contain labels. Also, they must not be coded as the only statements in Pascal constructs that do not allow null statements. For example, coding a declare cursor statement as the only statement in a Pascal if statement not bounded by begin and end would cause compiler errors:
if (using_database) then
exec sql declare empcsr cursor for
select ename from employee;
else
writeln('You have not accessed the database');
The code generated by the preprocessor would be:
if (using_database) then
else
writeln('You have not accessed the database');
This is an illegal use of the Pascal else clause.
Last modified date: 08/28/2024