Microsoft KB Archive/67832

COBOL 4.00 Call-Level Access to Microsoft SQL Server

PSS ID Number: Q67832 Article last modified on 06-07-1992

h4.00 OS/2

Summary: Call-level access to Microsoft SQL Server involves making calls to functions that reside in a database library called PDBLIB.DLL. This library ships with Microsoft SQL Server version 1.10. You can make SQL queries through these function calls and obtain information from a SQL database. The sample COBOL program below contains the necessary function calls and parameters to allow access to the Microsoft SQL Server and the processing of one query. This information applies to Microsoft COBOL Professional Development System (PDS) version 4.00 for MS OS/2. Call Microsoft SQL Server Support for detailed assistance regarding call-level queries.

More Information: Use the following to compile and link the sample program further below: cobol sqlcall; link sqlcall,,,coblib+os2+pdblib; Use the directives OSVS and VSC2. Make sure the PDBLIB.DLL can be located with the LIBPATH environment variable under OS/2, or copy PDBLIB.DLL to the directory where your COBOL libraries currently reside. At program run time, the Microsoft SQL Server must be up and running. The program will invoke a query. For a sample test, you could use the PUBS database that ships with Microsoft SQL Server. The PUBS database structure is documented in Appendix A of the “Microsoft SQL Server: System Administrator’s Guide.”

Sample Program
$set osvs vsc2 identification division. program-id. sqlcall. * Program sets up interface to SQL SERVER using * call-level interface. data division. working-storage section. 01 login       POINTER. 01 dbproc      POINTER. 01 msghp       PROCEDURE-POINTER. 01 usr         pic x(16)  value &quot;username&quot;. * usr contains your SQL SERVER user id. 01 svr        pic x(16)  value &quot;servername&quot;. * The svr variable should contain your SERVER name. * Place a space between the quotes if you are using * SQL SERVER as a local server. 01 cmd         pic x(40). 01 filler      pic x(1) value x'00'. 01 msgtext2    pic x(255). 01 objname     pic x(30). 78 NO-MORE-ROWS   value   -2. 78 DBSETUSER      value   2. 78 FAIL           value   0. 78 CHARBIND       value   4. 78 SIZEOF-SHORT   value   2. 78 SIZEOF-LONG    value   4. local-storage section. linkage section. 01 dbpr        pointer. 01 msgno       pic S9(9) comp-5. 01 state       pic S9(4) comp-5. 01 sev         pic S9(4) comp-5. 01 msgtext     pic x(255). procedure division. main section. INSPECT usr REPLACING FIRST SPACE BY x'00'. INSPECT svr REPLACING FIRST SPACE BY x'00'. SET msghp TO ENTRY &quot;MSGH&quot;. CALL &quot;_dbmsghandle&quot; USING BY VALUE msghp. CALL &quot;_dblogin&quot; RETURNING login. CALL &quot;_dbsetlname&quot; USING BY VALUE login, BY REFERENCE usr, BY VALUE DBSETUSER SIZE 2. CALL &quot;_dbopen&quot; USING BY VALUE login, BY REFERENCE svr RETURNING dbproc. display &quot;Enter SQL&quot; accept cmd. CALL &quot;_dbcmd&quot; USING BY VALUE dbproc, BY REFERENCE cmd. CALL &quot;_dbsqlexec&quot; USING BY VALUE dbproc. CALL &quot;_dbresults&quot; USING BY VALUE dbproc. CALL &quot;_dbprhead&quot; USING BY VALUE dbproc. CALL &quot;_dbprrow&quot; USING BY VALUE dbproc. CALL &quot;_dbbind&quot; USING BY VALUE dbproc, BY VALUE 1 SIZE 2, BY VALUE CHARBIND SIZE 2, BY VALUE 0 SIZE 4, BY REFERENCE objname. perform until RETURN-CODE = NO-MORE-ROWS CALL &quot;_dbnextrow&quot; USING BY VALUE dbproc if RETURN-CODE not = NO-MORE-ROWS display objname end-perform. stop run. handle-msg section. ENTRY &quot;MSGH&quot; USING BY REFERENCE dbpr, BY VALUE msgno, BY VALUE state, BY VALUE sev, BY REFERENCE msgtext. display &quot;Msgno=&quot; msgno &quot; Sev=&quot; sev &quot; State=&quot; state. move spaces to msgtext2. STRING msgtext DELIMITED BY x'00' INTO msgtext2. display msgtext2. EXIT PROGRAM RETURNING 0. Copyright Microsoft Corporation 1992.