30
Bordoloi and Bordoloi and Bock Bock Copyright 2004 Prentice Hall, Inc. 11-1 CHAPTER 11: CHAPTER 11: EMBEDDED SQL EMBEDDED SQL

Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Embed Size (px)

Citation preview

Page 1: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-1

CHAPTER 11: CHAPTER 11: EMBEDDED SQLEMBEDDED SQL

Page 2: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-2

• Structured Query Language can be used in Structured Query Language can be used in conjunction with many different procedural and conjunction with many different procedural and object-oriented programming host languages.object-oriented programming host languages.

• This approach to programming is termed This approach to programming is termed embedded SQLembedded SQL, and simply means that the host , and simply means that the host language includes the ability to use SQL language includes the ability to use SQL statements to both retrieve and store records in a statements to both retrieve and store records in a non-procedural fashion. non-procedural fashion.

EMBEDDED SQLEMBEDDED SQL

Page 3: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-3

• Database processing is primarily accomplished Database processing is primarily accomplished through use of the ActiveX Data Objects (ADO) through use of the ActiveX Data Objects (ADO) control.control.

• This specialized control enables a computer This specialized control enables a computer programmer to connect an application program programmer to connect an application program written in Visual Basic to an existing database.written in Visual Basic to an existing database.

• It is easy to create the database connection. It is easy to create the database connection.

Embedding SQL in Visual Basic 6.0Embedding SQL in Visual Basic 6.0

Page 4: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-4

Figure 11.1 shows an example employee processing form Figure 11.1 shows an example employee processing form with an ADO control.with an ADO control.

Page 5: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-5

• The ADO control is used to process employee records. The ADO control is used to process employee records. • The employee processing form displays a subset of the The employee processing form displays a subset of the

data columns from the data columns from the employeeemployee table. table.• The ADO control provides First, Previous, Next, and The ADO control provides First, Previous, Next, and

Last buttons (arrows) to make it easy to navigate from Last buttons (arrows) to make it easy to navigate from one row of data to the next.one row of data to the next.

• When one of the ADO control navigation buttons is When one of the ADO control navigation buttons is clicked, the form display updates to a new record.clicked, the form display updates to a new record.

• The ADO control also has properties that can be The ADO control also has properties that can be visually programmed to make a database connection. visually programmed to make a database connection.

ADO ControlADO Control

Page 6: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-6

• The The ConnectionStringConnectionString property stores a character string that property stores a character string that specifies how to connect to a database.specifies how to connect to a database.

• Part of the Part of the ConnectionStringConnectionString information specifies where information specifies where the database is located.the database is located.

• The The ConnectionStringConnectionString also specifies the type of database as also specifies the type of database as well as information about provider software that is used to well as information about provider software that is used to make a database connection. In the coding segment shown make a database connection. In the coding segment shown in the following VB6 Example, a connection is made to an in the following VB6 Example, a connection is made to an Oracle database named Oracle database named CompanyCompany. .

• REM VB6 Example REM VB6 Example

• ConnectionString = "Provider=MSDAORA.1; User ID=dbock; ConnectionString = "Provider=MSDAORA.1; User ID=dbock; Password=mypassword; Data Source=Company; Persist Password=mypassword; Data Source=Company; Persist Security Info=True" Security Info=True"

(Note: All of the above code will be entered on a single coding line.)(Note: All of the above code will be entered on a single coding line.)

ADO ControlADO Control

Page 7: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-7

• The The CommandTypeCommandType property specifies how data will be property specifies how data will be retrieved from a database.retrieved from a database.

• Data rows are retrieved from disk and stored in a Data rows are retrieved from disk and stored in a memory object called a memory object called a recordsetrecordset. .

• The The RecordSourceRecordSource property stores the actual SQL property stores the actual SQL statement used to create a recordset.statement used to create a recordset.

• The records displayed for the recordset in Figure 11.1 The records displayed for the recordset in Figure 11.1 were retrieved with the SELECT statement shown in were retrieved with the SELECT statement shown in VB6 Example 11.2. VB6 Example 11.2.

• REM VB6 Example 11.2 REM VB6 Example 11.2 • SELECT emp_last_name, emp_first_name, emp_middle_name, emp_ssn, SELECT emp_last_name, emp_first_name, emp_middle_name, emp_ssn,

emp_address, emp_city, emp_state, emp_zipemp_address, emp_city, emp_state, emp_zip FROM employee FROM employee WHERE emp_zip = '62025';WHERE emp_zip = '62025';

ADO ControlADO Control

Page 8: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-8

ADO ControlADO Control• There are two additional properties for each textbox control There are two additional properties for each textbox control

that must be set in order for a Visual Basic form to display that must be set in order for a Visual Basic form to display database data.database data.

• These are the These are the DataFieldDataField and and DataSourceDataSource properties shown properties shown in Figure 11.2 that displays a Visual Basic property in Figure 11.2 that displays a Visual Basic property window that is associated with the textbox control that window that is associated with the textbox control that displays the employee first name. displays the employee first name.

Page 9: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-9

• The The DataSourceDataSource property of each textbox is set to the name property of each textbox is set to the name of the ADO control.of the ADO control.

• Here the ADO control is namedHere the ADO control is named adoEmployee. adoEmployee.

• Setting theSetting the DataSource DataSource property of a textbox links the property of a textbox links the textbox control to the ADO control.textbox control to the ADO control.

• DataFieldDataField property is set to the appropriate column name property is set to the appropriate column name from the from the employeeemployee table in order to specify the exact value table in order to specify the exact value to display in a textbox.to display in a textbox.

• Each textbox on the employee processing form will have the Each textbox on the employee processing form will have the same value for the same value for the DataSourceDataSource property, but the property, but the DataFieldDataField property for each textbox will reflect the column of data to property for each textbox will reflect the column of data to be displayed in that particular textbox.be displayed in that particular textbox.

ADO ControlADO Control

Page 10: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-10

• This is achieved by storing the value entered into the This is achieved by storing the value entered into the InputBox to a Visual Basic memory variable that stores InputBox to a Visual Basic memory variable that stores string or character data.string or character data.

• The Visual Basic code to produce the InputBox and The Visual Basic code to produce the InputBox and store the value is shown in VB6 Example 11.4.store the value is shown in VB6 Example 11.4.

• The value entered for the social security number is The value entered for the social security number is stored to the stored to the strSSNstrSSN variable. variable.

• REM VB6 SQL Example 11.4 REM VB6 SQL Example 11.4

• Dim strSSN As String strSSN = InputBox("Enter Employee SSN:", _ Dim strSSN As String strSSN = InputBox("Enter Employee SSN:", _

"Employee SSN Search", vbOKCancel)"Employee SSN Search", vbOKCancel)

Selecting Data Specified by a User Inputted ValueSelecting Data Specified by a User Inputted Value

Page 11: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-11

• The program stores the SELECT statement to a second The program stores the SELECT statement to a second string variable named string variable named strSQLstrSQL in the code segment in the code segment shown in VB6 Example 11.5. shown in VB6 Example 11.5.

• Each piece of the SELECT statement is a string of Each piece of the SELECT statement is a string of characters that are concatenated together with the Visual characters that are concatenated together with the Visual Basic concatenation operator (the ampersand – &). The Basic concatenation operator (the ampersand – &). The WHERE clause has the WHERE clause has the emp_ssnemp_ssn column name set equal column name set equal to the value stored to the to the value stored to the strSSNstrSSN variable that was variable that was captured through use of the InputBox. captured through use of the InputBox.

Selecting Data Specified by a User Inputted ValueSelecting Data Specified by a User Inputted Value

Page 12: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-12

• At execution time, the SELECT statement stored to the At execution time, the SELECT statement stored to the strSQLstrSQL variable includes the social security number stored variable includes the social security number stored to the to the strSSNstrSSN variable. variable.

• REM VB6 Example 11.5REM VB6 Example 11.5

• 'Store SQL statement to a string variable'Store SQL statement to a string variable

• strSQL = "SELECT emp_last_name, emp_first_name, " & _strSQL = "SELECT emp_last_name, emp_first_name, " & _

"emp_middle_name, emp_ssn, emp_address, " & _"emp_middle_name, emp_ssn, emp_address, " & _

"emp_city, emp_state, emp_zip" & _"emp_city, emp_state, emp_zip" & _

"FROM employee" & _"FROM employee" & _

"WHERE emp_ssn = " & strSSN"WHERE emp_ssn = " & strSSN

Selecting Data Specified by a User Inputted ValueSelecting Data Specified by a User Inputted Value

Page 13: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-13

• The The RecordSourceRecordSource property of the ADO control is property of the ADO control is updated by storing the updated by storing the strSQLstrSQL string variable to the string variable to the RecordSourceRecordSource property as shown in VB6 Example 11.6. property as shown in VB6 Example 11.6.

• The ADO control is refreshed with the The ADO control is refreshed with the RefreshRefresh method. method. The The RefreshRefresh method automatically creates a new method automatically creates a new recordset, and the correct employee row will be recordset, and the correct employee row will be retrieved and displayed on the employee processing retrieved and displayed on the employee processing form. form.

• REM VB6 Example 11.6REM VB6 Example 11.6

• 'Update the recordset retrieved by the ADO control'Update the recordset retrieved by the ADO control

adoEmployee.RecordSource = strSQLadoEmployee.RecordSource = strSQL

adoEmployee.Refresh adoEmployee.Refresh

Selecting Data Specified by a User Inputted ValueSelecting Data Specified by a User Inputted Value

Page 14: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-14

• Visual Basic 6.0 uses the ActiveX Data Objects (ADO) Visual Basic 6.0 uses the ActiveX Data Objects (ADO) approach to connect to databases through OLEDB approach to connect to databases through OLEDB providers (software drivers).providers (software drivers).

• ADO has evolved into ADO.NET in Visual Basic.NET. ADO has evolved into ADO.NET in Visual Basic.NET. ADO.NET still uses OLEDB providers to connect to ADO.NET still uses OLEDB providers to connect to databases such as Oracle, Microsoft SQL Server, and databases such as Oracle, Microsoft SQL Server, and Microsoft Access.Microsoft Access.

• The major tasks facing a database programmer are:The major tasks facing a database programmer are:– Connecting to a database.Connecting to a database.

– Executing SQL statements to add, delete, modify, or retrieve Executing SQL statements to add, delete, modify, or retrieve table rows.table rows.

– Working with datasets. Datasets replace the recordsets used Working with datasets. Datasets replace the recordsets used in VB 6.0.in VB 6.0.

Embedding SQL in Visual Basic.netEmbedding SQL in Visual Basic.net

Page 15: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-15

• A sample VB.NET connection string for an Oracle A sample VB.NET connection string for an Oracle database named Company is shown in VB.NET database named Company is shown in VB.NET Example 11.1.Example 11.1.

• REM VB.NET Example 11.1REM VB.NET Example 11.1• strConnection = "Provider=MSDAORA.1; User ID=dbock; strConnection = "Provider=MSDAORA.1; User ID=dbock;

Password=mypassword; Data Source=Company; Persist Password=mypassword; Data Source=Company; Persist Security Info=True" Security Info=True"

• The connection string value is stored to a string constant The connection string value is stored to a string constant named named strConnectionstrConnection. VB.NET Example 11.2 shows . VB.NET Example 11.2 shows the declaration of a connection object named the declaration of a connection object named objConnection.objConnection.

Making a Database ConnectionMaking a Database Connection

Page 16: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-16

• The value of the The value of the strConnectionstrConnection constant is passed to the constant is passed to the objConnectionobjConnection object as an argument inside object as an argument inside parentheses. Following this the database connection is parentheses. Following this the database connection is opened by using the Open method. opened by using the Open method.

• REM VB.NET Example 11.2REM VB.NET Example 11.2• Module MyConnection Module MyConnection • Private Const strConnection As String = & _Private Const strConnection As String = & _• "Provider = MSDAORA.1; User ID=dbock; "Provider = MSDAORA.1; User ID=dbock;

Password=mypassword; & _ Password=mypassword; & _ • "Data Source=Company; Persist Security Info=False""Data Source=Company; Persist Security Info=False"• Sub Main()Sub Main()• Dim objConnection As New _ Dim objConnection As New _ • System.Data.OleDb.OleDbConnection(strConnection)System.Data.OleDb.OleDbConnection(strConnection)• objConnection.Open()objConnection.Open()• End SubEnd Sub• End ModuleEnd Module

Making a Database ConnectionMaking a Database Connection

Page 17: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-17

• Visual Basic.NET uses a command object Visual Basic.NET uses a command object ((OleDBCommandOleDBCommand) to execute SQL statements.) to execute SQL statements.

• The approach is again similar to that used in Visual The approach is again similar to that used in Visual Basic 6.0 in that a programmer creates a string memory Basic 6.0 in that a programmer creates a string memory variable to store the SQL statement to be executed. variable to store the SQL statement to be executed.

• The SELECT statement is shown in VB.NET Example The SELECT statement is shown in VB.NET Example 11.3.11.3.

• REM VB.NET Example 11.3REM VB.NET Example 11.3• SELECT emp_last_name, emp_first_name, SELECT emp_last_name, emp_first_name,

emp_middle_name, emp_ssn, emp_middle_name, emp_ssn, emp_address, emp_city, emp_state, emp_zipemp_address, emp_city, emp_state, emp_zipFROM employeeFROM employeeWHERE emp_zip = '62025';WHERE emp_zip = '62025';

Executing SQL StatementsExecuting SQL Statements

Page 18: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-18

• The .NET code module named The .NET code module named MyConnectionMyConnection shown shown earlier in VB.NET Example 11.2 needs to be modified earlier in VB.NET Example 11.2 needs to be modified to accommodate reading data.to accommodate reading data.

• The modified code is shown in VB.NET Example 11.4.The modified code is shown in VB.NET Example 11.4.• Note that the Note that the strSQLstrSQL memory variable stores the memory variable stores the

SELECT command as a series of concatenated character SELECT command as a series of concatenated character strings.strings.

• A command object named A command object named objCommandobjCommand is created and is created and the value of the value of strSQLstrSQL and the and the objConnectionobjConnection connection connection object are passed to the object are passed to the objCommandobjCommand object as object as parameters inside parentheses.parameters inside parentheses.

• A reader object (A reader object (objReaderobjReader) is created to store the actual ) is created to store the actual rows returned when the rows returned when the ExecuteReaderExecuteReader method of the method of the objCommandobjCommand object executes. object executes.

Executing SQL StatementsExecuting SQL Statements

Page 19: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-19

• REM VB.NET Example 11.4REM VB.NET Example 11.4• Module MyConnection Module MyConnection • Private Const strConnection As String = & _Private Const strConnection As String = & _• "Provider = MSDAORA.1; User ID=dbock; Password=mypassword; & _ "Provider = MSDAORA.1; User ID=dbock; Password=mypassword; & _ • "Data Source=Company; Persist Security Info=False""Data Source=Company; Persist Security Info=False"• Sub Main()Sub Main()• Dim strSQL As StringDim strSQL As String• strSQL = "SELECT emp_last_name, emp_first_name, " & _strSQL = "SELECT emp_last_name, emp_first_name, " & _• "emp_middle_name, emp_ssn, emp_address, " & _"emp_middle_name, emp_ssn, emp_address, " & _• "emp_city, emp_state, emp_zip " & _"emp_city, emp_state, emp_zip " & _• "FROM employee " & _"FROM employee " & _• "WHERE emp_zip = '62025';""WHERE emp_zip = '62025';"• Dim objConnection As New _ Dim objConnection As New _ • System.Data.OleDb.OleDbConnection(strConnection)System.Data.OleDb.OleDbConnection(strConnection)• Dim objCommand As New _ Dim objCommand As New _ • System.Data.OleDb.OleDbCommand(strSQL, objConnection)System.Data.OleDb.OleDbCommand(strSQL, objConnection)• Dim objReader As _Dim objReader As _• System.Data.OleDB.OleDBReaderSystem.Data.OleDB.OleDBReader• objConnection.Open()objConnection.Open()• objReader = objCommand.ExecuteReader()objReader = objCommand.ExecuteReader()• 'Additional code goes here to process the rows returned'Additional code goes here to process the rows returned• 'by the SQL query.'by the SQL query.• End SubEnd Sub• End ModuleEnd Module

Executing SQL StatementsExecuting SQL Statements

Page 20: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-20

Data Division ModificationsData Division Modifications

• The Data Division of a COBOL program is used to The Data Division of a COBOL program is used to declare both the structure of files or databases that a declare both the structure of files or databases that a program will access. program will access.

• It is also used to declare memory variables known as It is also used to declare memory variables known as Working-Storage variables.Working-Storage variables.

• The Working-Storage Section of a COBOL program is The Working-Storage Section of a COBOL program is also used to declare tables in memory that will store also used to declare tables in memory that will store data rows that are retrieved from database tables. data rows that are retrieved from database tables.

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 21: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-21

• EXEC SQL and END-EXEC commands are used to EXEC SQL and END-EXEC commands are used to mark the beginning and end of SQL statements so that a mark the beginning and end of SQL statements so that a COBOL compiler can properly compile the program.COBOL compiler can properly compile the program.

• The code in COBOL Example 11.1 shows the The code in COBOL Example 11.1 shows the declaration of a table named declaration of a table named employeeemployee that will store that will store data rows retrieved from the data rows retrieved from the employeeemployee table of our table of our Company database.Company database.

• This code is located in the Working-Storage Section.This code is located in the Working-Storage Section.• You will also note that only the table columns that will You will also note that only the table columns that will

be retrieved for processing need to be defined in the be retrieved for processing need to be defined in the table.table.

• The other columns in the The other columns in the employeeemployee table are ignored. table are ignored.

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 22: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-22

– * COBOL Example 11.1* COBOL Example 11.1

– EXEC SQLEXEC SQL

– DECLARE EMPLOYEE TABLEDECLARE EMPLOYEE TABLE

– (EMP_SSN CHAR(9),(EMP_SSN CHAR(9),

– EMP_LAST_NAME CHAR(25),EMP_LAST_NAME CHAR(25),

– EMP_FIRST_NAME CHAR(25),EMP_FIRST_NAME CHAR(25),

– EMP_MIDDLE_NAME CHAR(25),EMP_MIDDLE_NAME CHAR(25),

– EMP_ADDRESS CHAR(50),EMP_ADDRESS CHAR(50),

– EMP_CITY CHAR(25),EMP_CITY CHAR(25),

– EMP_STATE CHAR(2),EMP_STATE CHAR(2),

– EMP_ZIP CHAR(9) )EMP_ZIP CHAR(9) )

– END-EXEC.END-EXEC.

• In order to process the In order to process the employeeemployee table, rows that are table, rows that are retrieved are stored to standard COBOL variables.retrieved are stored to standard COBOL variables.

• These are also declared in the Working-Storage Section. These are also declared in the Working-Storage Section.

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 23: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-23

• In order to process the In order to process the employeeemployee table, rows that are retrieved are table, rows that are retrieved are stored to standard COBOL variables.stored to standard COBOL variables.

• These are also declared in the Working-Storage Section.These are also declared in the Working-Storage Section.

• Row data are moved from the Row data are moved from the employeeemployee table to the COBOL table to the COBOL variables. The Working-Storage declaration of these variables is variables. The Working-Storage declaration of these variables is shown in COBOL Example 11.2. shown in COBOL Example 11.2.

– * COBOL Example 11.2* COBOL Example 11.2

– 01 EMPLOYEE-WORK.01 EMPLOYEE-WORK.

– 05 EMP-SSN PIC X(9).05 EMP-SSN PIC X(9).

– 05 EMP-LAST-NAME PIC X(25).05 EMP-LAST-NAME PIC X(25).

– 05 EMP-FIRST-NAME PIC X(25).05 EMP-FIRST-NAME PIC X(25).

– 05 EMP-MIDDLE-NAME PIC X(25).05 EMP-MIDDLE-NAME PIC X(25).

– 05 EMP-ADDRESS PIC X(50).05 EMP-ADDRESS PIC X(50).

– 05 EMP-CITY PIC X(25).05 EMP-CITY PIC X(25).

– 05 EMP-STATE PIC X(2).05 EMP-STATE PIC X(2).– 05 EMP-ZIP PIC X(9).05 EMP-ZIP PIC X(9).

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 24: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-24

• In order to process SQL statements and the In order to process SQL statements and the employee_dataemployee_data table declared in COBOL Example 11.2, table declared in COBOL Example 11.2, a a SQL communications areaSQL communications area (SQLCA) must be defined (SQLCA) must be defined in the server's memory.in the server's memory.

• The SQL communications area includes a SQLCODE The SQL communications area includes a SQLCODE parameter. The SQLCODE parameter stores coded parameter. The SQLCODE parameter stores coded values that represent what occurs within an information values that represent what occurs within an information system whenever any SQL statement executes.system whenever any SQL statement executes.

• The normal execution of an SQL statement causes the The normal execution of an SQL statement causes the SQLCODE parameter to store a value of zero.SQLCODE parameter to store a value of zero.

• Program code must be written for the COBOL program Program code must be written for the COBOL program Procedure Division in order to test the value ofProcedure Division in order to test the value of

Contd.Contd.

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 25: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-25

SQLCODE through the use of IF statements. SQLCODE through the use of IF statements. • The IF statements will determine if a given SQL The IF statements will determine if a given SQL

statement executes successfully.statement executes successfully.• The SQL communications area is created by the The SQL communications area is created by the

INCLUDE command shown in COBOL example 11.3.INCLUDE command shown in COBOL example 11.3.

• * COBOL Example 11.3* COBOL Example 11.3

• EXEC SQLEXEC SQL

• INCLUDE SQLCAINCLUDE SQLCA

• END-EXEC.END-EXEC.

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 26: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-26

Procedure Division ModificationsProcedure Division Modifications• The retrieval of data rows through use of an embedded The retrieval of data rows through use of an embedded

SQL SELECT statement in COBOL requires the storage SQL SELECT statement in COBOL requires the storage of data values to of data values to hosthost variables. variables.

• For the For the employeeemployee table, this simply means that data row table, this simply means that data row values will be stored to the values will be stored to the 01 EMPLOYEE-WORK01 EMPLOYEE-WORK memory variable created in COBOL Example 11.2.memory variable created in COBOL Example 11.2.

• A SELECT statement embedded in a Procedure A SELECT statement embedded in a Procedure Division looks like the one shown in COBOL Example Division looks like the one shown in COBOL Example 11.4.11.4.

• Note that the host variables are preceded by a colon ( : ) Note that the host variables are preceded by a colon ( : ) symbol that is used to differentiate these variables to the symbol that is used to differentiate these variables to the program compiler. program compiler.

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 27: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-27

• The SELECT statement is NOT ended by a semicolon The SELECT statement is NOT ended by a semicolon when it is embedded in COBOL.when it is embedded in COBOL.

• The code is located within a paragraph named The code is located within a paragraph named PROCESS-EMPLOYEE-DATA.PROCESS-EMPLOYEE-DATA.

– * COBOL Example 11.4* COBOL Example 11.4– PROCESS-EMPLOYEE-DATA.PROCESS-EMPLOYEE-DATA.– EXEC SQLEXEC SQL– SELECT EMP_SSN, EMP_LAST_NAME, EMP_FIRST_NAME, SELECT EMP_SSN, EMP_LAST_NAME, EMP_FIRST_NAME, – EMP_MIDDLE_NAME, EMP_ADDRESS, EMP_MIDDLE_NAME, EMP_ADDRESS, – EMP_CITY, EMP_STATE, EMP_ZIPEMP_CITY, EMP_STATE, EMP_ZIP– INTO :EMP-SSN, :EMP-LAST-NAME, :EMP-FIRST-NAME,INTO :EMP-SSN, :EMP-LAST-NAME, :EMP-FIRST-NAME,– :EMP-MIDDLE-NAME, :EMP-ADDRESS, :EMP-CITY,:EMP-MIDDLE-NAME, :EMP-ADDRESS, :EMP-CITY,– :EMP-STATE, :EMP-ZIP:EMP-STATE, :EMP-ZIP– FROM EMPLOYEEFROM EMPLOYEE– WHERE EMP_ZIP = "62025"WHERE EMP_ZIP = "62025"– END-EXEC.END-EXEC.

Contd.Contd.

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 28: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-28

– IF SQLCODE = 0IF SQLCODE = 0– * (Place additional code here to process the employee row)* (Place additional code here to process the employee row)– ELSEELSE– * (Insert additional code here to handle the retrieval* (Insert additional code here to handle the retrieval– * error – no employees with the specified Zip Code)* error – no employees with the specified Zip Code)– END IF. END IF.

• Additional code would be added to process the data Additional code would be added to process the data rows retrieved from the rows retrieved from the employeeemployee table by calling the table by calling the PROCESS-EMPLOYEE-DATA paragraph through use PROCESS-EMPLOYEE-DATA paragraph through use of a standard PERFORM UNTIL command within of a standard PERFORM UNTIL command within COBOL.COBOL.

• The embedded SQL command replaces the use of a The embedded SQL command replaces the use of a COBOL READ command for the simple retrieval of COBOL READ command for the simple retrieval of data. data.

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 29: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-29

• Data can be inserted into the Data can be inserted into the employeeemployee table by using table by using the SQL INSERT statement in place of the COBOL the SQL INSERT statement in place of the COBOL WRITE command as shown in COBOL Example 11.5. WRITE command as shown in COBOL Example 11.5.

• Let's assume that the data values to be inserted have Let's assume that the data values to be inserted have already been moved to the work variables defined already been moved to the work variables defined within the within the 01 EMPLOYEE-WORK01 EMPLOYEE-WORK memory variable memory variable created earlier.created earlier.

• This could be accomplished by using a series of This could be accomplished by using a series of ACCEPT commands to enable data entry from the ACCEPT commands to enable data entry from the keyboard.keyboard.

• Alternatively, the data values could be READ from a Alternatively, the data values could be READ from a sequential or indexed data file. sequential or indexed data file.

Embedding SQL in COBOLEmbedding SQL in COBOL

Page 30: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.11-1 CHAPTER 11: EMBEDDED SQL

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 11-30

• * COBOL Example 11.5* COBOL Example 11.5

• EXEC SQLEXEC SQL

• INSERT INTO EMPLOYEE (EMP_SSN, EMP_LAST_NAME,INSERT INTO EMPLOYEE (EMP_SSN, EMP_LAST_NAME,

• EMP_FIRST_NAME, EMP_MIDDLE_NAME, EMP_FIRST_NAME, EMP_MIDDLE_NAME, EMP_ADDRESS, EMP_ADDRESS,

• EMP_CITY, EMP_STATE, EMP_ZIP)EMP_CITY, EMP_STATE, EMP_ZIP)

• VALUES (:EMP-SSN, :EMP-LAST-NAME, :EMP-FIRST-VALUES (:EMP-SSN, :EMP-LAST-NAME, :EMP-FIRST-NAME,NAME,

• :EMP-MIDDLE-NAME, :EMP-ADDRESS, :EMP-CITY,:EMP-MIDDLE-NAME, :EMP-ADDRESS, :EMP-CITY,

• :EMP-STATE, :EMP-ZIP):EMP-STATE, :EMP-ZIP)

• END-EXEC.END-EXEC.

• Note that the values inserted from the Working-Storage Note that the values inserted from the Working-Storage variables match up on a one-for-one basis with the variables match up on a one-for-one basis with the column names defined for the column names defined for the employeeemployee table in the table in the Working-Storage Section of the program. Working-Storage Section of the program.

Embedding SQL in COBOLEmbedding SQL in COBOL