Export (0) Print
Expand All

Creating Extended Stored Procedures

SQL Server 2000

An extended stored procedure is a function with a prototype:

SRVRETCODE xp_extendedProcName (SRVPROC *);

Using the prefix "xp_" is optional. Extended stored procedure names are case sensitive when referenced in Transact-SQL statements, regardless of code page/sort order installed on the server. An extended stored procedure is implemented in a 32-bit dynamic-linked library (DLL). When you build a DLL:

  • If an entry point is necessary, write a DllMain function.

    This function is optional; if you do not provide it in source code, the compiler links its own version, which does nothing but return TRUE. If you provide a DllMain function, the operating system calls this function when a thread or process attaches to or detaches from the DLL.

  • All functions called from outside the DLL (all extended stored procedure functions) must be exported.

    You can export a function by listing its name in the EXPORTS section of a .def file, or you can prefix the function name in the source code with __declspec(dllexport), a Microsoft compiler extension (Note that __declspec() begins with two underscores).

These Open Data Services files are required for creating an extended stored procedure DLL.

File Description
Srv.h Open Data Services header file
Opends60.lib Import library for Opends60.dll

It is highly recommended that all Microsoft® SQL Server™ 2000 extended stored procedure DLLs implement and export the following function:

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

Note  __declspec(dllexport) is a Microsoft-specific compiler extension. If your compiler does not support this directive, you should export this function in your DEF file under the EXPORTS section.

When SQL Server is started with the trace flag -T260 or if a user with system administrator privileges runs DBCC TRACEON (260), then if the extended stored procedure DLL does not support __GetXpVersion(), a warning message (Error 8131: Extended stored procedure DLL '%' does not export __GetXpVersion().) is printed to the error log (Note that __GetXpVersion() begins with two underscores). If you get this message, and you are running an extended stored procedure DLL compiled with headers and libraries from SQL Server version 6.x, refer to Level 1: Handling Discontinued Functionality. If you get this message and are running an extended stored procedure DLL compiled with headers and libraries from SQL Server 7.0, your extended stored procedure DLL is not exporting the function __GetXpVersion().

If the extended stored procedure DLL exports __GetXpVersion(), but the version returned by the function is less than that required by the server, a warning message (Error 8132: Extended stored procedure DLL '%' reports its version is %d.%d. Server expects version %d.%d.) stating the version returned by the function and the version expected by the server is printed to the error log. If you get this message, you are returning an incorrect value from __GetXpVersion(), or you are compiling with an older version of srv.h.

Note  SetErrorMode, a Microsoft Win32® function, should not be called in extended stored procedures.

For more information about creating a DLL, see the development environment documentation and the Microsoft Win32 SDK documentation.

To create an extended stored procedure DLL by using Microsoft Visual C++

  1. Create a new project of type Win32 Dynamic Link Library.

  2. Set the directory for include files and library files to C:\Program Files\Microsoft SQL Server\80\Tools\DevTools\Include and C:\Program Files\Microsoft SQL Server\80\Tools\DevTools\Lib, respectively.
    1. On the Tools menu, click Options.

    2. In the Options dialog box, click the Directories tab and set the directory for include files and library files.
  3. On the Project menu, click Settings.

  4. In the Project Settings dialog box, click the Link tab. Click the General category, and then add opends60.lib to object/library modules.

  5. Add source files (.c, .cpp, and .rc files, and so on) to your project.

  6. Compile and link your project.
Show:
© 2014 Microsoft