Takes you through the process of creating a connection, coding a stored procedure using OpenESQL Assistant, and running the application that calls the stored procedure.
Requirements
Before attempting this tutorial, you must complete the following tutorials in the order listed:
- Tutorial: Enable SQL CLR Integration
- Tutorial: Create a Sample Database
- Tutorial: Create and Configure a Database Project
Create an ADO.NET Connection
Catalog an ADO.NET database connection and connect to it using the OpenESQL Assistant.
- Start OpenESQL Assistant
-
- In Visual Studio, click
View > Micro Focus SQL Tools > OpenESQL Assistant.
- Catalog an ADO.NET Connection
-
- From the tool bar in the OpenESQL Assistant, click the
ADO.NET Connection Editor icon
.
- Click
> Save > All framework/availability combinations; then do one of the following:
- If
All framework/availability combinations is checked, proceed to the next step.
- If
All framework/availability combinations is not checked, click it to check it; then proceed to the next step.
- Click
> Framework and ensure that
4.0, 4.5, 4.6 is checked.
- Click
Add to create a new database connection; then click
Next.
- In the
Data Source Name field, type
SQLCLRTutorial; then click
Next.
- On the .NET Data Providers page, select
SqlClient Data Provider; then click
Next.
- In the
Data Source field, type
. (dot) to specify your local SQL Server instance.
- In the
Initial Catalog field, type
SQLCLR_Test, which is the name of your SQL Server database.
- Do one of the following to set authentication for your SQL Server instance:
- If you want to connect using Windows authentication (recommended), change the value for the
Integrated Security key to
True.
- If you want to connect by providing SQL Server-specific login credentials, type your SQL Server user ID and password into the
Value fields for the
User ID and
Password keys respectively.
- Click
Next, and then
Finish.
- After the new connection appears on the main window of the ADO.NET Connection Editor, click
Test to verify that the connection works.
- Click the
x icon in the upper right corner of the ADO.NET Connection Editor main window to close it.
- Connect to your SQL Server Instance
-
- On the OpenESQL Assistant, uncheck all listed data source names, if any.
- From the tool bar, click the
Refresh list of data sources icon
.
- Check
SQLCLRTutorial to make the connection.
Code a SQL CLR Stored Procedure using OpenESQL Assistant
Use the OpenESQL Assistant to build on the SQL CLR stored procedure such that it looks up an employee number and returns the employee number and first and last names of the employee.
- Create a SQL CLR Stored Procedure Query
-
- In the OpenESQL Assistant, check the
EMP (TEST) table listed under the
SQLCLRTutorial connection entry.
- On the Select Type of Query to Create dialog box, click
SELECT (singleton).
- Check
Generate Query as a SQLCLR Stored Procedure.
- In the
SP name field, type
SQLCLRTutorial. The OpenESQL Assistant uses this name for both the generated program and the stored procedure.
- Click
OK.
- On the tree view, check the
A.EMPNO,
A.FIRSTNME, and
A.LASTNAME columns to add them to the query.
- Click the
Search Criteria tab.
- Verify that the following fields are set to the values specified:
Column
|
A.EMPNO
|
Conditional Operator
|
=
|
Target Type
|
Host Variable
|
Target Value
|
:EMP-EMPNO
|
- Click
> (right arrow) to construct the arguments for the WHERE clause of the SELECT statement you are building.
As you build the query, the OpenESQL Assistant generates all code necessary to complete the stored procedure program, including host variables and SQLCA declarations.
- Add the Stored Procedure to the Project
-
- In the Solution Explorer, click the
SQLCLRTutorial COBOL project name to ensure that our stored procedure is added to the correct project.
- Right-click over the toolbar in the OpenESQL Assistant, and select
Add SP to SQLCLR project.
The OpenESQL Assistant adds the stored procedure to the project in the file
SQLCLRTutorial.cbl.
- In the Solution Explorer, right-click the
StoredProcedure1.cbl file and select
Delete.
- Click
Yes to confirm deletion.
- Click
Save.
Publish, Debug, and Execute a Stored Procedure
This takes you through the process of publishing, debugging, and executing the stored procedure, using the SQL Server Object Explorer in Visual Studio.
Note:
- Before attempting to debug by stepping into the stored procedure code using the steps below, if you are using a UAC-enabled operating system, you must be running
Enterprise Developer as an administrator.
- When connecting to your SQL Server, if Microsoft SQL Server 2012 prompts you with an Attach Security Warning, please click
Attach to clear the prompt.
- Publish Your Stored Procedure to SQL Server
-
- In the Solution Explorer, right-click the
SQLCLRTutorial.Publish project; then select
Publish.
- On the Publish Database dialog box, click
Edit.
- In the
Server Name field, type
. (dot) to represent your local SQL Server instance.
- In the
Select or enter database name field, select
SQLCLR_Test from the drop-down list; then click
OK.
- On the Publish Database dialog box, click
Publish. When the publishing process is complete, the Data Tools Operations window shows a status of
Publish Completed Successfully.
- Debug Your Stored Procedure
- Now that your stored procedure is available for use, you can debug it from Visual Studio.
- From the SQL Server Object Explorer, click
Add SQL Server
.
- In the
Server Name field, type
. (dot) to represent your local SQL Server instance; then click
Connect.
- On the SQL Server Object Explorer, right-click your SQL Server instance, represented by a dot followed by your local server information in parentheses, and check
Application Debugging and
Allow SQL/CLR Debugging. If a prompt appears, click
Yes to clear it.
- On the SQL Server Object Explorer, expand the database
SQLCLR_Test > Programmability > Stored Procedures; then right-click the
dbo.SQLCLRTutorial stored procedure and select
Debug Procedure.
- In the
Value field for the
@lkEMPNO name, type
000020.
- Check
Null for the
@lkFIRSTNME and
@lkLASTNAME names, and then click
OK.
Visual Studio connects to your SQL Server instance, creates a test SQL script, calls your stored procedure, and runs the debugger with your cursor on the
USE [SQLCLR_Test] statement of the test SQL script.
- Use the Visual Studio debugger to step through the lines of code in your stored procedure, examining variables and values, etc.
The Data Tools window on the
Results tab displays information returned from the debugger.
- To exit the debugger, click
Debug >
Stop Debugging.
- Execute Your Stored Procedure
- You can execute the stored procedure providing by providing the required input values, and see the output that results from the stored procedure call.
- From the SQL Server Object Explorer, right-click the
dbo.SQLCLRTutorial stored procedure, and select
Execute Procedure.
- If not already set, type
000020 into the
Value field for the
@lkEMPNO name, and check
Null for the other two names.
- Click
OK to execute the stored procedure.
- View the results on the
Results tab:
Call a Published Stored Procedure
This section takes you through the process of calling your published stored procedure from a COBOL program.
Note: When connecting to your SQL Server, if Microsoft SQL Server 2012 prompts you with an Attach Security Warning, please click
Attach to clear the prompt.
- Create a Visual Studio Project
- You need to create a Visual Studio project in which to code a program that calls your published stored procedure.
- In Visual Studio, click
.
- Under
Installed Templates, click
COBOL >
Managed >
Console Application.
- From the drop-down field above the list of templates, select
.NET Framework 4 or
.NET Framework 4.5.
- In the
Name field, type
SQLCLRTutorialCall.
- In the
Location field, specify a directory in which to store the project; then click
OK.
You now have a new solution containing one project.
- Create a 32-bit Solution Platform
- Because Visual Studio runs in 32-bit, and the connection you've created using SQL Server 2012 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 Solution name; 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.
- From the Configuration Manager, click
Close.
- Set Project Properties
-
- From the Solution Explorer, double-click
Properties under your
SQLCLRTutorialCall project.
- On the
SQL tab, select
OpenESQL from the
ESQL Preprocessor drop-down list; then click
Add.
- On the
Available Directives list, click
DBMAN; then click
OK.
- From the
Value drop-down, select
ADO.
- Click
Save, and 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 code 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 "SQLCLRtutorialCall.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 "SQLCLRtutorial" end-exec
if sqlcode <> 0
display "CONNECT FAILED"
end-if
set empid to "000020"
exec sql
:spReturnCode = call "SQLCLRtutorial" (: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.
- Save the program.
- Run the COBOL Program
-
- From the
Program1.cbl [Code] window, insert a breakpoint at the
goback. statement.
- Press
F5 to run the program.
When the debugger hits the breakpoint, you should see the following in the
Output window as a result of calling the stored procedure:
User = MICHAEL THOMPSON
- Click
File >
Save All (optional).
- Exit Visual Studio.