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 solution and project
- On the
What would you like to do? dialog box, click
Create a new project.
- On the
Create a new project dialog box, scroll down the list, and select
SQL Server Database Project, and then click
Next.
Attention: Be sure that you select the SQL Server Database Project template for COBOL and Windows as other templates with this name might
exist.
- Complete the fields as follows:
Project name
|
SQLServerSPCall
|
Location
|
c:\tutorials\SQL
|
- Check
Place solution and project in the same directory.
- Click
Create.
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
SQLServerSPCall 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
Application tab, change the
Output type to
Console Application.
- 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 "SQLServerDB" 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.