Click here to Skip to main content
11,935,467 members (64,839 online)
Click here to Skip to main content
Add your own
alternative version


51 bookmarked

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

, 9 Mar 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


    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.


    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.


    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.


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


    About the Author

    Santosh Rao
    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

    You may also be interested in...

    Comments and Discussions

    QuestionCall Java code from MS SQL Pin
    ash200827-May-08 15:40
    memberash200827-May-08 15:40 
    Generalh Pin
    Anonymous6-May-05 19:54
    sussAnonymous6-May-05 19:54 
    GeneralSample app that calls this XP Pin
    Tom Wright2-Feb-05 6:21
    memberTom Wright2-Feb-05 6:21 
    GeneralLink to VO Pin
    thuehmer15-Dec-04 7:43
    memberthuehmer15-Dec-04 7:43 
    GeneralRe: Link to VO Pin
    Cristian Amarie27-Dec-04 5:12
    memberCristian Amarie27-Dec-04 5:12 
    GeneralProblem in Stored Procedure Pin
    abhiVyas23-Aug-04 21:23
    memberabhiVyas23-Aug-04 21:23 
    GeneralRe: Problem in Stored Procedure Pin
    baiju.km24-Aug-04 1:14
    memberbaiju.km24-Aug-04 1:14 
    GeneralFailed to compile xp_dblib.dll Pin
    Mahabubur Rahaman21-Jul-04 18:40
    memberMahabubur Rahaman21-Jul-04 18:40 
    GeneralSupport for 64-bit Pin
    xavitomorera3-May-04 13:13
    memberxavitomorera3-May-04 13:13 
    QuestionHow can I put big image file to image field Pin
    marchday200421-Apr-04 22:13
    membermarchday200421-Apr-04 22:13 
    GeneralStored Procedure Pin
    Mhondoro4-Apr-04 23:21
    memberMhondoro4-Apr-04 23:21 
    GeneralRe: Stored Procedure Pin
    marchday200421-Apr-04 22:33
    membermarchday200421-Apr-04 22:33 
    GeneralPassing Params to XP Pin
    schlaem14-Jan-04 0:08
    memberschlaem14-Jan-04 0:08 
    GeneralWord Wrap Pin
    Param11115-Oct-03 18:25
    memberParam11115-Oct-03 18:25 
    General__GetXpVersion Pin
    Gary Tarr22-Jul-03 6:34
    memberGary Tarr22-Jul-03 6:34 
    GeneralSQL & VB Pin
    canuckster18-Apr-03 4:00
    membercanuckster18-Apr-03 4:00 
    GeneralRe: SQL &amp; VB Pin
    Byungangshei8-Aug-03 10:20
    sussByungangshei8-Aug-03 10:20 
    GeneralRe: SQL & VB Pin
    venkatasubramanian n2-Sep-03 21:56
    membervenkatasubramanian n2-Sep-03 21:56 
    GeneralRe: SQL &amp; VB Pin
    khimchoon16-Sep-03 23:01
    memberkhimchoon16-Sep-03 23:01 
    GeneralCXProc class for VC++ Pin
    Leonid Belousov9-Nov-02 10:36
    memberLeonid Belousov9-Nov-02 10:36 
    Questionhow to debug it Pin
    Anonymous15-Oct-02 8:44
    sussAnonymous15-Oct-02 8:44 
    AnswerRe: how to debug it Pin
    Albert van Peppen4-Apr-03 1:32
    memberAlbert van Peppen4-Apr-03 1:32 
    GeneralSelect in Xp Pin
    kambiz19-Sep-02 0:43
    susskambiz19-Sep-02 0:43 
    GeneralRe: Select in Xp Pin
    Anonymous3-Oct-02 3:42
    sussAnonymous3-Oct-02 3:42 
    GeneralCompile XP Pin
    Batzal3-Jul-02 6:16
    memberBatzal3-Jul-02 6:16 
    GeneralRe: Compile XP Pin
    Santosh Rao3-Jul-02 18:56
    memberSantosh Rao3-Jul-02 18:56 
    GeneralRe: Compile XP Pin
    Anonymous22-Aug-02 0:59
    sussAnonymous22-Aug-02 0:59 
    GeneralRe: Compile XP Pin
    Albert van Peppen12-Sep-02 1:13
    memberAlbert van Peppen12-Sep-02 1:13 
    GeneralRe: Compile XP Pin
    cduarte2-Apr-03 12:13
    membercduarte2-Apr-03 12:13 
    GeneralRe: Compile XP Pin
    Albert van Peppen4-Apr-03 1:26
    memberAlbert van Peppen4-Apr-03 1:26 
    GeneralRe: Compile XP Pin
    Ciciu15-Apr-04 0:15
    memberCiciu15-Apr-04 0:15 
    GeneralRe: Compile XP Pin
    sodum15-Apr-05 22:06
    membersodum15-Apr-05 22:06 
    GeneralRe: Compile XP Pin
    sodum15-Apr-05 4:45
    membersodum15-Apr-05 4:45 
    GeneralReplacing an Extended Stored Proc DLL Pin
    Richard Bywater14-May-02 22:53
    memberRichard Bywater14-May-02 22:53 
    Generalextended stored procedure error Pin
    thiggins18-Feb-02 7:05
    memberthiggins18-Feb-02 7:05 
    GeneralPassing 4-byte int data type parameters Pin
    JohnnyLR7-Jan-02 11:35
    memberJohnnyLR7-Jan-02 11:35 
    GeneralRe: Passing 4-byte int data type parameters Pin
    Richard Bywater14-May-02 23:04
    memberRichard Bywater14-May-02 23:04 
    GeneralRe: Passing 4-byte int data type parameters Pin
    Param11115-Oct-03 18:21
    memberParam11115-Oct-03 18:21 
    GeneralRe: Passing 4-byte int data type parameters Pin
    friedov14-Oct-04 8:10
    memberfriedov14-Oct-04 8:10 
    Generalabc Pin
    Anonymous11-May-01 3:05
    memberAnonymous11-May-01 3: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.

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.151126.1 | Last Updated 10 Mar 2000
    Article Copyright 2000 by Santosh Rao
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid