Click here to Skip to main content
15,867,765 members
Articles / Desktop Programming / MFC
Article

Tutorial on Extended Stored Procedures for MS SQL Server v7.0

Rate me:
Please Sign up or sign in to vote.
4.25/5 (16 votes)
9 Mar 2000CPOL 350.5K   5.7K   51   40
An interface that can be used to build robust applications that extend the power of MS SQL Server.
  • Download demo project - 296 Kb
  • Download source files - 11 Kb
  • Introduction

    Open Data Services is an interface that can be used to build robust applications that extend the power of MS SQL Server.

    Extended stored procedures

    An extended stored procedure is a C or C++ DLL that can be called from Transact-SQL using the same syntax as calling a Transact-SQL stored procedure. Extended stored procedures are a way to extend the capabilities of Transact-SQL to include any resources or services available to Microsoft Win32 applications.

    In other words you can create a Extended Stored Procedure in your custom language (VC++, VB, Delphi). These dll's can use API's provided by Open Data Services to interact, control and enhance the functionality of SQL server to provide the functionality you might require.

    Examples:

    1. Imagine you have an application that performs some processing on the basis of the some records inserted newly in a particular table. The common way that one may do the same is open a recordset connection on the table and keep polling and re-querying till a new entry is inserted.

      The drawback in the above example is polling, it would put a lot of burden on the table on frequently accessed tables. The Extended Stored Procedure would adopt an event based approach, where an extended stored procedure residing on the server would be triggered on a new insert and perform custom processing.

    2. Imagine you have an application that needs to perform some checks on some condition and immediately send a response mail to someone notifying of the event.

      The drawback in the above example is you might have a particular condition to trigger the event today and a totally different condition tomorrow. The Extended Stored Procedure would adopt an event-based approach where your custom dll could generate and manage mail, all that one would have to do is to trigger the Extended Stored Procedure in the SQL Server trigger.


    Some quick tips on installing an extended stored procedure.

    Adding an extended stored procedure:

    To add an extended stored procedure function in an extended stored procedure DLL, you must run the sp_addextendedproc system stored procedure, specifying the name of the function and the name of the DLL in which that function resides. The user executing the command has to be a SQL Server system administrator.

    For example, this command registers the function xp_sample, located in xp_sample.dll, as a SQL Server extended stored procedure:

    sp_addextendedproc 'xp_sample','xp_sample.dll'

    It is a good practice to place the extended stored procedure dll in the SQL Server bin path along with any dependent dlls. SQL Server uses the LoadLibrary() method to locate the dll.

    Removing an extended stored procedure:

    sp_dropextendedproc 'xp_sample','xp_sample.dll'

    How Extended Stored Procedures Work

    When a client call (from ISQL, your own application) to the extended stored procedure is performed which in turn executes an extended stored procedure, the request is transmitted to the SQL Server. SQL Server then locates the DLL associated with the extended stored procedure, and loads the DLL if it is not already loaded using the LoadLibrary() method. So inherently the first call to the stored procedure will have the overhead of loading the dll. Then SQL server calls the exported extended stored procedure located in the dll, which gets an interface to the SQL Server in form of a Server Procedure. Your procedure can then read parameters passed to it and return back results if it so intends.

    However if your extended stored procedure raises an exception its process is killed and you have to restart the SQL server. SQL server has to be restarted even if you intend to replace the XP dll as it may have been loaded.

    Programming

    The sample extended stored procedure provided xp_sample is compiled and tested in VC ver 6.0. It should work on version 5 too as it has been tested.

    I have created the sample by first creating a MFC Regular DLL statically linked. (VC 6 provides XP's in the AppWizard but when I started working on a project 6 months back we had to use VC Ver 5.0). Don’t forget to include the Srv.h which has the declarations of the ODS functions and link to the library opends60.lib.

    Then define your export Extended Stored Procedure function which is of the format:

    SRVRETCODE xp_sample(SRV_PROC *srvproc)

    Where srvproc is a handle to the client connection and SRVRETCODE indicates the success or failure code that you intend to return. Do not forget to include the function in the exports .DEF file. The sample first gets a count of the no of parameters passed to it using the srv_rpcparams function. Then it gets the length, type and data of each paramater using srv_paramlen, srv_paramtype, srv_paramdata functions respectively. It then appends all the data received and sends it back to the SQL Server using srv_describe, srv_sendrow and srv_senddone functions.

    Programming Conventions

    1. It is a recommended practice to name your extended stored procedure and your extended stored procedure dll to start with xp_ by convention.
    2. It is also recommended that all Microsoft SQL Server extended stored procedure DLLs implement and export the following function:<>

      __declspec(dllexport) ULONG __GetXpVersion()
      {
         return ODS_VERSION;
      }

      It helps to prevent warnings and allows version checks when your xp is installed against the ODS Library on the SQL Server Installed as your stored procedure library may require a ODS version higher than what is installed on the SQL Server on which it is intended to be installed.

    Debugging an Extended Stored Procedure

    To debug an extended stored procedure DLL by using Microsoft Visual C++ follow the following steps.

    1. Stop SQL Server.
    2. Copy the updated SQL server extended to SQL Server bin directory preferably.
    3. Register the SQL Server using the sp_addextendedproc system stored procedure.
    4. In the project settings output Debug category for:
      • Executable for debug session: Provide $MSSSQLSERVERPATH$\BINN\sqlservr.exe
      • Working Directory: Provide $MSSSQLSERVERPATH$\BINN
      • Program arguments: -c so that SQL server starts as an application and not as an service.
    5. You can then put your break points and debug as normal.

    Note:

    1. If you plan to use MFC in the stored procedure or any other dependencies be sure that it is statically linked or the dependency dlls are also available in the $MSSSQLSERVERPATH$\BINN path.
    2. Your include directory in options must contain the SQL Development tools include and lib paths.
    3. I have tried compiling and checking whether it works on SQL Server 6.5, it does. But I doubt Microsoft supports it for version 6.5.

    Best of luck and happy extended stored procedures.

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Architect
    India India
    1993 started with Computers

    BE(Computer Science) and MS (Software Systems)

    Industry Experience: 10 Years

    C, C++, VC++(MFC), .NET, C#, MTS, Queuing, ASP.NET, AJAX, Java, J2EE, SunOne, JMS

    Banking, Insurance & Pension,Health Care

    Comments and Discussions

     
    AnswerRe: how to debug it Pin
    Albert van Peppen4-Apr-03 0:32
    professionalAlbert van Peppen4-Apr-03 0:32 
    GeneralSelect in Xp Pin
    kambiz18-Sep-02 23:43
    kambiz18-Sep-02 23:43 
    GeneralRe: Select in Xp Pin
    Anonymous3-Oct-02 2:42
    Anonymous3-Oct-02 2:42 
    GeneralCompile XP Pin
    3-Jul-02 5:16
    suss3-Jul-02 5:16 
    GeneralRe: Compile XP Pin
    Santosh Rao3-Jul-02 17:56
    Santosh Rao3-Jul-02 17:56 
    GeneralRe: Compile XP Pin
    Anonymous21-Aug-02 23:59
    Anonymous21-Aug-02 23:59 
    GeneralRe: Compile XP Pin
    Albert van Peppen12-Sep-02 0:13
    professionalAlbert van Peppen12-Sep-02 0:13 
    GeneralRe: Compile XP Pin
    cduarte2-Apr-03 11:13
    cduarte2-Apr-03 11:13 
    Cry | :(( Cry | :(( When I compile the xp_sample (and included the srv.h), the VC++ give me an error :
    d:\archivos de programa\microsoft visual studio\vc98\include\srvdbtyp.h(20) : error C2011: 'srv_datetime' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvdbtyp.h(26) : error C2011: 'srv_money' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvdbtyp.h(45) : error C2011: 'dbdatetime4' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvdbtyp.h(52) : error C2011: 'dbdaterec' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvdbtyp.h(76) : error C2011: 'dbnumeric' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(20) : error C2011: 'srv_handler' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(29) : error C2011: 'srv_pevents' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(43) : error C2011: 'srv_events' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(59) : error C2011: 'srv_subchannel' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(77) : error C2011: 'translation_info' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(93) : error C2011: 'srv_params' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(110) : error C2011: 'srv_loginrec' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(166) : error C2011: 'srv_login_oldtds' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(195) : error C2011: 'srv_stats' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(205) : error C2011: 'srv_queuehead' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(212) : error C2011: 'srv_listhead' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(222) : error C2011: 'srv_listentry' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(356) : error C2011: 'srv_config' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(407) : error C2011: 'srv_tdshdr' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(422) : error C2011: 'srv_inputbuf' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(433) : error C2011: 'srv_comport_queue' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(445) : error C2011: 'srv_comportbuf' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(467) : error C2011: 'srv_io' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(498) : error C2011: 'srv_coldesc' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(512) : error C2011: 'srv_rpcp' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(523) : error C2011: 'srv_comport' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(532) : error C2011: 'srv_proc' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(666) : error C2011: 'srv_control' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(746) : error C2011: 'srv_server' : 'struct' type redefinition
    d:\archivos de programa\microsoft visual studio\vc98\include\srvstruc.h(799) : error C2011: 'ods_callbacks' : 'struct' type redefinition
    D:\Archivos de Programa\Microsoft Visual Studio\MyProjects\XPSample\xp_sample.cpp(156) : error C2065: 'ODS_VERSION' : undeclared identifier
    Error executing cl.exe.

    xp_sample.dll - 31 error(s), 0 warning(s)

    GeneralRe: Compile XP Pin
    Albert van Peppen4-Apr-03 0:26
    professionalAlbert van Peppen4-Apr-03 0:26 
    GeneralRe: Compile XP Pin
    Ciciu14-Apr-04 23:15
    Ciciu14-Apr-04 23:15 
    GeneralRe: Compile XP Pin
    Member 188214815-Apr-05 21:06
    Member 188214815-Apr-05 21:06 
    GeneralRe: Compile XP Pin
    Member 188214815-Apr-05 3:45
    Member 188214815-Apr-05 3:45 
    GeneralReplacing an Extended Stored Proc DLL Pin
    Richard Bywater14-May-02 21:53
    Richard Bywater14-May-02 21:53 
    Generalextended stored procedure error Pin
    thiggins18-Feb-02 6:05
    thiggins18-Feb-02 6:05 
    GeneralPassing 4-byte int data type parameters Pin
    JohnnyLR7-Jan-02 10:35
    JohnnyLR7-Jan-02 10:35 
    GeneralRe: Passing 4-byte int data type parameters Pin
    Richard Bywater14-May-02 22:04
    Richard Bywater14-May-02 22:04 
    GeneralRe: Passing 4-byte int data type parameters Pin
    Param Matharu15-Oct-03 17:21
    Param Matharu15-Oct-03 17:21 
    GeneralRe: Passing 4-byte int data type parameters Pin
    friedov14-Oct-04 7:10
    friedov14-Oct-04 7:10 
    Generalabc Pin
    11-May-01 2:05
    suss11-May-01 2:05 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.