Takes you through the process of calling your published stored procedure from a COBOL program.
Save and close the SQLServerSP solution
- From the Visual Studio main menu, click
File > Close Solution.
- If prompted to save, click
OK.
Create the SQLServerSPCall project
- In Visual Studio, click
.
- In the
New Project dialog box, expand
.
- Click the
Database category.
- In the center pane, select
SQL Server Database Project.
- Complete the fields as follows:
Name
|
SQLServerSPCall
|
Location
|
c:\tutorials\SQL
|
Solution Name
|
SQLServerSPCsll
|
Framework
|
Choose the appropriate .NET Framework version from the drop-down list.
Note: Must be version 4.5 or later.
|
- Uncheck
Create directory for solution.
- Click
OK.
Add a 32-bit solution platform
Because Visual Studio runs in 32-bit, and the connection you've created using SQL Server runs in 64-bit, you need to add
a 32-bit solution platform before you can execute your stored procedure.
- From the
Solution Explorer, right-click the
SQLServerSP solution; then select
Configuration Manager.
- From the
Active solution platform drop-down list, do one of the following:
- If
x86 is an option, select it.
- If
x86 is not an option:
- Select
<New...>.
- From the
Type or select the new platform drop-down list, select
x86; then click
OK.
- On the
Configuration Manager, click
Close.
Set project properties
- From the
Solution Explorer, double-click
Properties under your
SQLServerSPCall project.
- On the
SQL tab, select
OpenESQL from the
ESQL Preprocessor drop-down list.
- Click
Add.
- On the
Available Directives list, click
DBMAN; then click
OK.
- Repeat steps
3 and
4, but this time add the
BEHAVIOR directive.
Note: The default value for both DBMAN (ADO) and BEHAVIOR (OPTIMIZE) are correct, so you don't need to make any value changes.
- Click
Save
; then close the
Properties window.
Code a COBOL program
You now code a COBOL program to call your stored procedure.
- If
Program1.cbl is not open in the COBOL editor, double-click it from the
Solution Explorer. If it is open, click its tab to bring it into focus.
- Replace all of the code in the program with the following code:
program-id. Program1 as "SQLServerSPCall.Program1".
data division.
working-storage section.
exec sql include sqlca end-exec.
01 empid PIC X(6). *>string.
01 lastname PIC X(50). *>string.
01 firstname PIC X(50). *>string.
01 connectString string.
01 spReturnCode binary-long.
procedure division.
exec sql connect to "LookupEMP" end-exec
if sqlcode <> 0
display "CONNECT FAILED"
end-if
set empid to "000020"
exec sql
:spReturnCode = call "LookupEMP" (:empid INOUT, :lastname OUT, :firstname OUT)
end-exec
if sqlcode <> 0
display "Call FAILED"
else
display "User = " firstname " " lastname
end-if
exec sql disconnect all end-exec.
goback.
end program Program1.
Note: You could also use the
OpenESQL Assistant to generate the CALL statement from the
Auxiliary Code tab and insert it into the program rather than coding it manually as done here.
- Click
Save
to save
Program1.cbl.
Run the COBOL Program
- In the COBOL editor, insert a breakpoint at the
goback statement.
- Press
F5 to run the program in the debugger.
When the debugger hits the breakpoint, you should see the following in a generated console window as a result of calling
the stored procedure:
User = THOMPSON MICHAEL
- Press
F5 to stop debugging.