Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for C—429847-008
10-39
Basic Dynamic SQL Program
Example 10-7. Basic Dynamic SQL Program
(page 1 of 4)
1 /* This program finds the average salary for employees */
2 /* according to criteria established by the user. The */
3 /* program contains a SELECT statement for the EMPLOYEE */
4 /* table; the user enters the selection criteria, which */
5 /* the program concatenates to the SELECT statement as a */
6 /* WHERE clause. */
7 /* */
8 #pragma inspect
9 #pragma symbols
10 #pragma SQL
11 #pragma xmem
12 #pragma runnable
13 #pragma nolmap
14 #pragma nomap
15
16 #pragma nolist
17 #include <stdioh>
18 #include <stdlibh>
19 #include <stringh>
20 #include <memoryh>
21
22 /* For SQL error reporting: */
23 #include <cextdecs (SQLCADISPLAY)>
24
25 /* For SQL data type literals: */
26 #include <sqlh>
27 #pragma list
28
29 #define MAXCMD 512
30
31 /* Global variables: */
32 int sqlcode; /* for error checking */
33 long average; /* for output value */
34 int i; /* loop counter */
35 char temp[100]; /* temporary storage for user input */
36
37 /* Buffers for storing SQL statements are always blank */
38 /* padded, never null terminated. */
39 char cmd[MAXCMD]; /* for SQL statement user enters */
40
41
42 /* Include SQLCA for error checking, SQLSA for dynamic SQL */
43 /* processing information: */
44 exec sql INCLUDE SQLCA;
45 exec sql INCLUDE SQLSA;
46
47 /* The program will have only one output column, SALARY. */
48 /* Since we will be generating its average, we do not need */
49 /* to print the column name--we can therefore omit */
50 /* declaring a names buffer. We will use this SQLDA (not */
51 /* a template) because we are not allocating memory */
52 /* dynamically--we know we need only one output variable. */
53
54 /* C differs from Pascal and COBOL in requiring that the */
55 /* INCLUDE SQLDA statement appear within a DECLARE section. */
56 exec sql BEGIN DECLARE SECTION;
57 exec sql INCLUDE SQLDA (osqlda,1) ;
58 exec sql END DECLARE SECTION;
59
60 /* ------------------------------------------------------- */
61
Summary of Contents for NonStop SQL/MP
Page 4: ......
Page 14: ...Contents HP NonStop SQL MP Programming Manual for C 429847 008 x ...
Page 60: ...Host Variables HP NonStop SQL MP Programming Manual for C 429847 008 2 26 VARCHAR Data Type ...
Page 294: ...SQL MP Sample Database HP NonStop SQL MP Programming Manual for C 429847 008 A 6 ...