1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/*
* Program type: Embedded Dynamic SQL
*
* Description:
* This program adds several departments with small default
* budgets, using 'execute immediate' statement.
* Then, a prepared statement, which doubles budgets for
* departments with low budgets, is executed.
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include "example.h"
#include <stdlib.h>
#include <string.h>
#define MAXLEN 256
int get_line (char *line);
void clean_up (void);
static char *Dept_data[] =
{
"117", "Field Office: Hong Kong", "110",
"118", "Field Office: Australia", "110",
"119", "Field Office: New Zealand", "110",
0
};
int Dept_ptr = 0;
char Db_name[128];
EXEC SQL
SET DATABASE empdb = "employee.fdb" RUNTIME :Db_name;
int main(int argc, char** argv)
{
BASED_ON department.department dept_name;
BASED_ON department.dept_no dept_id;
char exec_str[MAXLEN], prep_str[MAXLEN];
if (argc > 1)
strcpy(Db_name, argv[1]);
else
strcpy(Db_name, "employee.fdb");
EXEC SQL
WHENEVER SQLERROR GO TO MainError;
EXEC SQL
CONNECT empdb;
clean_up();
EXEC SQL
SET TRANSACTION;
/*
* Prepare a statement, which may be executed more than once.
*/
strcpy(prep_str,
"UPDATE DEPARTMENT SET budget = budget * 2 WHERE budget < 100000");
EXEC SQL
PREPARE double_small_budget FROM :prep_str;
/*
* Add new departments, using 'execute immediate'.
* Build each 'insert' statement, using the supplied parameters.
* Since these statements will not be needed after they are executed,
* use 'execute immediate'.
*/
while (get_line(exec_str))
{
printf("\nExecuting statement:\n\t%s;\n", exec_str);
EXEC SQL
EXECUTE IMMEDIATE :exec_str;
}
EXEC SQL
COMMIT RETAIN;
/*
* Execute a previously prepared statement.
*/
printf("\nExecuting a prepared statement:\n\t%s;\n\n", prep_str);
EXEC SQL
EXECUTE double_small_budget;
EXEC SQL
COMMIT;
EXEC SQL
DISCONNECT empdb;
exit(0);
MainError:
EXEC SQL
WHENEVER SQLERROR CONTINUE;
isc_print_status(gds__status);
printf("SQLCODE=%d\n", SQLCODE);
EXEC SQL ROLLBACK;
exit(1);
}
/*
* Construct an 'insert' statement from the supplied parameters.
*/
int get_line(char* line)
{
if (Dept_data[Dept_ptr] == 0)
return 0;
if (Dept_data[Dept_ptr + 1] == 0)
return 0;
if (Dept_data[Dept_ptr + 2] == 0)
return 0;
sprintf(line, "INSERT INTO DEPARTMENT (dept_no, department, head_dept) \
VALUES ('%s', '%s', '%s')", Dept_data[Dept_ptr],
Dept_data[Dept_ptr + 1], Dept_data[Dept_ptr + 2]);
Dept_ptr += 3;
return(1);
}
/*
* Delete old data.
*/
void clean_up (void)
{
EXEC SQL WHENEVER SQLERROR GO TO CleanErr;
EXEC SQL SET TRANSACTION;
EXEC SQL EXECUTE IMMEDIATE
"DELETE FROM department WHERE dept_no IN ('117', '118', '119')";
EXEC SQL COMMIT;
return;
CleanErr:
isc_print_status(gds__status);
printf("SQLCODE=%d\n", SQLCODE);
exit(1);
}
|