160 likes | 310 Views
Embedded SQL. Objectives. Understand how SQL can be embedded into a program Understand how to retrieve single rows & update tables using embedded SQL Understand the use of cursors to retrieve multiple rows & update the database Understand the concept of error handling. Embedded SQL.
E N D
Objectives • Understand how SQL can be embedded into a program • Understand how to retrieve single rows & update tables using embedded SQL • Understand the use of cursors to retrieve multiple rows & update the database • Understand the concept of error handling
Embedded SQL • SQL commands may be embedded in a number of programming languages such as COBOL, C, & Fortran • Embedded SQL may be used to create a more flexible, accessible user interface • Performance may be improved with embedded SQL because the programmer can control the database access • Embedded SQL may be used to improve database security & enforce complex business rules
Components of Embedded SQL • The host program is written in a 3GL • Sections of code are written in SQL throughout the program as needed. Each section of code begins with the keywords EXEC SQL & ends with END-EXEC • A separate precompiler is needed for each host language that will be used to access the database • The precompiler translates the SQL commands into code that can be understood by the compiler
Using COBOL • Any tables to be processed must be declared in working-storage with the SQL command Declare tablename table • The table format must also be contained in an 01 record layout that describes each field • The SQLCA (SQL communications area) is used by the system to provide feedback to the program & must be included by coding the SQL command - Include SQLCA
Using COBOL - continued • COBOL variable names may be used within SQL statements be preceding the variable name with a colon • The results of SQL queries must be placed in host variables through the use of the INTO clause Select custname into :cname from customer where custid = :custno-in • After every SQL statement is executed, the SQLCODE contains a code indicating if execution was normal (SQLCODE = 0) Programs should check the value of SQLCODE after every Select statement
Retrieving Multiple Rows • COBOL processes a single record at a time. An SQL select statement returns a set of rows at once. In order for COBOL to process the set of rows returned by SQL a Cursor must be used. • A cursor allows the processing of a set of rows as though they were individual records in a sequential file • The cursor is a pointer that points to one row at the time
Using A Cursor • The cursor is declared in the working storage section with the Declare cursor-name cursor for select statement • The select statement in the cursor definition is the statement that will be used for execution in the procedure division. • A different cursor must be declared for every SQL statement that will be executed that could return more than 1 row.
The Cursor in the Procedure Division • Within the procedure division the cursor must be opened, fetched, and closed • Opening the cursor causes the query to be executed & makes the results available to the program • Executing a fetch, advances the pointer to the next row & places the row contents in the indicated host variables • Closing a cursor deactivates it
A Cursor Example In working-storage: Exec SQL Declare cust-cur cursor for select cname, cphone from customer where carea = ‘912’ End-exec. In the procedure division: Exec SQL Open cust-cur End-exec.
Example - continued The fetch SQL is put inside a perform loop that executes until the SQLCODE = 100 (100 is the code for no more rows) Exec SQL Fetch cust-cur into :custname, :custphone End-exec. Exec SQL Close cust-cur End-exec.
Error Handling • SQLCODE contains 100 for no more records, positive numbers for various unusual but normal conditions (no records matched query), and negative numbers for fatal errors • Rather than checking for each code individually after every SQL statement, the Whenever statement may be used
Whenever Exec SQL Whenever SQLError goto error-para End-exec. Exec SQL Whenever SQLwarning continue End-exec. Exec SQL Whenever not found continue End-exec.
Embedded SQL Summary • SQL commands must be contained within Exec SQL, end-exec blocks • The SQL table definitions & the SQLCA must be included in the data division • Host language variables may be used within SQL statements as long as they are preceded with a colon • A cursor must be declared, opened, fetched (within a perform loop), & closed if the query retrieves more than 1 row • The SQLCODE should be checked after every SQL statement is executed