Demonstration Application

The Tutorials: SQL CLR Stored Procedures Called via COBOL and JCL topics use a demonstration application that includes JCL, native COBOL, procedural COBOL, and SQL CLR stored procedure code. First, a native COBOL program is invoked via JCL. The invoked program calls a SQL Server SQL CLR stored procedure. The called stored procedure calls a second, procedural, COBOL program that then calls a second stored procedure that, unlike the first stored procedure, is nested. This nested stored procedure then calls other COBOL programs. Finally, a count of called programs and stored procedures is returned back to the calling native COBOL program. The count is increased by 1 for every program called and by 10 for every stored procedure called.

The tutorials are divided into single-project and multi-project functionality. The single-project tutorials call all programs and stored procedures from a single assembly. The multi-project tutorials call programs and stored procedures from multiple assemblies.

Demonstration Solutions

Each tutorial uses its own provided solution created using the SQL Server Database project template and then modified to include individual tutorial customizations. Each solution contains either three or four projects, depending on whether the tutorial demonstrates single- or multi-project functionality. While each project is slightly different from the others, all solutions contain projects that use the same names and provide essentially the same functionality.

Project Organization

Project organization differs slightly from solution to solution, depending on whether the tutorial demonstrates single- or multi-program functionality.

Single-project project organization
In the single-project tutorials, all application programs and stored procedures are contained in a single project - SQLCLRTutorial. All the stored procedures and program files are built into one DLL that is deployed to the sample SQLCLR_Test SQL Server database.

Putting the programs and stored procedures into a single project is a deployment advantage because everything is in a single assembly. No project references or procedure pointers are required. This organization is best for simple, self-contained applications. Larger, more diverse applications are best organized in a multi-project configuration.

Multi-project project organization
In the multi-project tutorials, the application programs and stored procedures are divided between two projects - SQLCLRTutorial and LegacyPrograms - each with its own assembly. Both of the stored procedures are built into one DLL, and all of the programs are built into another DLL. Both are deployed to the sample SQLCLR_Test SQL Server database.

In multi-project solutions such as these, you must define references or procedure pointers that identify the assemblies used, that is, when one assembly calls a program in a second assembly, the first assembly must either reference the second assembly or use a procedure pointer to load the second assembly. This ensures that routines contained in the second assembly are found at run time. One advantage to the multi-project approach is that you can more logically group types of code. For example, you can organize your code by creating projects that include sets of related programs, and sets of related stored procedures. Each project has its own assembly distinct from the others. Because the publishing process includes only assemblies that have changed, this method of organization can improve efficiency with regard to the size of the deployed application and the time required to publish. This method does require knowledge of the code sufficient to configure the appropriate references and procedure pointers.

Tip: To learn more about references and procedure pointers, see Issuing COBOL Calls from within SQL CLR Stored Procedures.

Solution Projects and Source Code

When you create a solution using the SQL Server Database project template, as has been done for these tutorials, Enterprise Developer automatically generates two projects - a COBOL project and a corresponding .Publish project. The .Publish project automatically references the COBOL project. The COBOL project properties are set to build the project as a verifiable (or safe) assembly. This is required to ensure that any assembly published to SQL Server runs in the SQL CLR environment.

Tip: If you were to subsequently add another COBOL project and corresponding .Publish project to an existing SQL Server Database solution, you must manually set the properties for the COBOL project to build as verifiable. For more information, see To set project properties to build verifiable code.

The projects and source code provided in each solution are:

SPCall Project
A COBOL/JCL project added to the solution as a customization. It contains a native COBOL program that initiates the first call to a SQL CLR stored procedure, and the JCL that invokes it the program.
SPCall.jcl JCL script
SPCall.cbl Native COBOL program
SPCall.jcl
Invokes SPCall.cbl:
 RUN PROG (SPCALL) PLAN (SPCALL) LIB ('MY.DBRMLIB')
SPCall.cbl
Invoked by SPCall.jcl, this program calls the SP1 stored procedure in the SQLCLRTutorial project.
       exec sql
            call "SP1" (:countval INOUT)
       end-exec
SQLCLRTutorial Project
Created automatically from the SQL Server Database project template.
Single-project tutorials
The SQLCLRTutorial project in single-project tutorials contains both the required COBOL programs and the stored procedures:
A.cbl Procedural COBOL programs
B.cbl
C.cbl
SP1.cbl SQL CLR stored procedures
SP2.cbl
A.cbl, B.cbl, C.cbl
Procedural COBOL programs that each increment the COBOL program counter by 1.
SP1.cbl
SP1.cbl contains stored procedure code. At run time, SQL Server meta-data identifies the assembly containing this stored procedure code, and then loads the assembly into the SQL Server database. This loaded assembly, SQLCLRTutorial, is the only assembly; therefore all calls from SP1.cbl to other programs and stored procedures are found in it. For example, when the stored procedure code in SP1.cbl calls program A, it finds A in the loaded SQLCLRTutorial assembly. SP1 calls programs A and C, and calls stored procedure SP2.
SP2.cbl
Stored procedure that calls programs A and B, and also increments the stored procedure counter by 10.
Multi-project tutorials
Created from the SQL Server Database project template, the SQLCLRTutorial project in multi-project tutorials contains just the stored procedures:
SP1.cbl SQL CLR stored procedures
SP2.cbl

The called procedural COBOL programs are part of the LegacyPrograms project.

SP1.cbl
SP1.cbl contains stored procedure code. At run time, SQL Server meta-data identifies the assembly containing this stored procedure code, and then loads the assembly into the SQL Server database. This loaded assembly, SQLCLRTutorial, does not contain the called COBOL programs or stored procedures.

To ensure that called programs stored procedures are found, SP1.cbl defines a procedure pointer that loads the LegacyProgram assembly before calling any programs.

           set assembly-to-load to entry "LegacyPrograms"

The LegacyPrograms assembly contains the called programs and stored procedures. For example, when the stored procedure code in SP1.cbl calls program A, it finds A in the loaded LegacyPrograms assembly.

SP1 then calls programs A and D, and calls stored procedure SP2.

SP2.cbl
Stored procedure that calls programs A, B, and C, and also increments the stored procedure counter by 10.
LegacyPrograms Project
Included in multi-project tutorials only, LegacyPrograms contains just the procedural COBOL programs:
A.cbl Procedural COBOL programs
B.cbl
C.cbl
D.cbl

The LegacyPrograms assembly is loaded by a procedure pointer defined in SP1. Each COBOL program in LegacyPrograms increments the COBOL program counter by 1.

SQLCLRTutorial.Publish Project
Also created by default from the SQL Server Database project template, the SQLCLRTutorial.Publish project does not contain any application code. Its purpose is to create the deployment package for the solution and to deploy it. The publishing process creates the stored procedures that call the into the DLL that SQL Server executes in a SQL CLR runtime environment. The SQL CLR run time works with the COBOL runtime. Publishing includes deploying both of these runtimes.