Previous Topic Next topic Print topic


Tutorial: Create, Run, and Call a SQL CLR Stored Procedure

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
  1. From the tool bar in the OpenESQL Assistant, click the ADO.NET Connection Editor icon Create New Query.
  2. Click Settings Menu icon > 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.
  3. Click Settings Menu icon > Framework and ensure that 4.0, 4.5, 4.6 is checked.
  4. Click Add to create a new database connection; then click Next.
  5. In the Data Source Name field, type SQLCLRTutorial; then click Next.
  6. On the .NET Data Providers page, select SqlClient Data Provider; then click Next.
  7. In the Data Source field, type . (dot) to specify your local SQL Server instance.
  8. In the Initial Catalog field, type SQLCLR_Test, which is the name of your SQL Server database.
  9. 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.
  10. Click Next, and then Finish.
  11. After the new connection appears on the main window of the ADO.NET Connection Editor, click Test to verify that the connection works.
  12. 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
  1. On the OpenESQL Assistant, uncheck all listed data source names, if any.
  2. From the tool bar, click the Refresh list of data sources icon Create New Query.
  3. 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
  1. In the OpenESQL Assistant, check the EMP (TEST) table listed under the SQLCLRTutorial connection entry.
  2. On the Select Type of Query to Create dialog box, click SELECT (singleton).
  3. Check Generate Query as a SQLCLR Stored Procedure.
  4. In the SP name field, type SQLCLRTutorial. The OpenESQL Assistant uses this name for both the generated program and the stored procedure.
  5. Click OK.
  6. On the tree view, check the A.EMPNO, A.FIRSTNME, and A.LASTNAME columns to add them to the query.
  7. Click the Search Criteria tab.
  8. Verify that the following fields are set to the values specified:
    Column A.EMPNO
    Conditional Operator =
    Target Type Host Variable
    Target Value :EMP-EMPNO
  9. 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
  1. In the Solution Explorer, click the SQLCLRTutorial COBOL project name to ensure that our stored procedure is added to the correct project.
  2. 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.

  3. In the Solution Explorer, right-click the StoredProcedure1.cbl file and select Delete.
  4. Click Yes to confirm deletion.
  5. 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 Visual COBOL 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
  1. In the Solution Explorer, right-click the SQLCLRTutorial.Publish project; then select Publish.
  2. On the Publish Database dialog box, click Edit.
  3. In the Server Name field, type . (dot) to represent your local SQL Server instance.
  4. In the Select or enter database name field, select SQLCLR_Test from the drop-down list; then click OK.
  5. 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.
  1. From the SQL Server Object Explorer, click Add SQL Server Add SQL Server.
  2. In the Server Name field, type . (dot) to represent your local SQL Server instance; then click Connect.
  3. 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.
  4. 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.
  5. In the Value field for the @lkEMPNO name, type 000020.
  6. 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.

  7. 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.

  8. 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.
  1. From the SQL Server Object Explorer, right-click the dbo.SQLCLRTutorial stored procedure, and select Execute Procedure.
  2. If not already set, type 000020 into the Value field for the @lkEMPNO name, and check Null for the other two names.
  3. Click OK to execute the stored procedure.
  4. View the results on the Results tab:
    Execute Stored Procedure Results

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.
  1. In Visual Studio, click File > New > Project.
  2. Under Installed Templates, click COBOL > Managed > Console Application.
  3. From the drop-down field above the list of templates, select .NET Framework 4 or .NET Framework 4.5.
  4. In the Name field, type SQLCLRTutorialCall.
  5. 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.
  1. From the Solution Explorer, right-click the Solution name; then select Configuration Manager.
  2. 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:
      1. Select <New...>.
      2. From the Type or select the new platform drop-down list, select x86; then click OK.
  3. From the Configuration Manager, click Close.
Set Project Properties
  1. From the Solution Explorer, double-click Properties under your SQLCLRTutorialCall project.
  2. On the SQL tab, select OpenESQL from the ESQL Preprocessor drop-down list; then click Add.
  3. On the Available Directives list, click DBMAN; then click OK.
  4. From the Value drop-down, select ADO.
  5. Click Save, and close the Properties window.
Code a COBOL Program
You now code a COBOL program to call your stored procedure.
  1. 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.
  2. 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.
  3. Save the program.
Run the COBOL Program
  1. From the Program1.cbl [Code] window, insert a breakpoint at the goback. statement.
  2. 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
  3. Click File > Save All (optional).
  4. Exit Visual Studio.
Previous Topic Next topic Print topic