Working with COM Objects from within T-SQL | Database Journal

Working with COM Objects from within T-SQL

Dec 13, 2000
4 minute read


Introduction

General concepts

OLE Automation Stored Procedures


  • sp_OACreate



  • sp_OADestroy



  • sp_OAGetProperty



  • sp_OASetProperty



  • sp_OAMethod



  • sp_OAGetErrorInfo



  • sp_OAStop


Example: generate script

Literature

 

Introduction

In this article I want to tell you about how you can work with COM objects from within Transact SQL. You can use OLE Automation Stored Procedures (extended stored procedures with sp_OA prefix) to create a COM object in T-SQL and use that object’s methods and properties.

General concepts

The OLE Automation Stored Procedures, in other words, sp_OA procedures (where OA stands for OLE Automation), allow a connection, through T-SQL commands, to create and use Component Object Model (COM) based objects. These procedures are built into SQL Server as an extended stored procedure.

Each OLE Automation stored procedure returns an integer code that is the HRESULT returned by the underlying OLE Automation operation. If HRESULT is equal to 0, then all is okay, a nonzero HRESULT indicates OLE error (in hexadecimal format).

You can use the sp_displayoaerrorinfo stored procedure to display OLE Automation error information (error description, not only hexadecimal code), when error occurs. This very useful procedure is not installed by default, so you can create it manually from SQL Server Books Online.

See this link for more information (here you can find sp_displayoaerrorinfo stored procedure) “OLE Automation Return Codes and Error Information”:
http://msdn.microsoft.com/library/psdk/sql/8_qd_12_25.htm

Advertisement

OLE Automation Stored Procedures

SQL Server supports seven OLE Automation Stored Procedures:

  • sp_OACreate
  • sp_OADestroy
  • sp_OAGetProperty
  • sp_OASetProperty
  • sp_OAMethod
  • sp_OAGetErrorInfo
  • sp_OAStop

You can find the description of these stored procedures below.

sp_OACreate

First of all, you should call sp_OACreate stored procedure to create an instance of the OLE object. You should pass two parameters into sp_OACreate stored procedure: program ID or class ID and variable. The variable will be a reference to the OLE object for the further using by other sp_OA stored procedures.

This is the example (for SQL Server 7.0):

DECLARE @object int
DECLARE @hr int

-- Create an object that points to the SQL Server
EXEC @hr = sp_OACreate 'SQLDMO.SQLServer', @object OUT
IF @hr <> 0
BEGIN
EXEC sp_displayoaerrorinfo @object, @hr
RETURN
END

NOTE:If you use SQL Server 6.5, you must write ‘SQLOLE.SQLServer’ instead ‘SQLDMO.SQLServer’.

See this link for more information
“sp_OACreate (T-SQL)”: http://msdn.microsoft.com/library/psdk/sql/sp_oa-oz.htm

sp_OADestroy

This stored procedure can be used to destroy a created OLE object. If you don’t call sp_OADestroy, the created OLE object will be destroyed automatically when the batch completes execution.

This is the example:

-- Destroy the previously created SQL Server object
EXEC @hr = sp_OADestroy @object
IF @hr <> 0
BEGIN
EXEC sp_displayoaerrorinfo @object, @hr
RETURN
END

See this link for more information
“sp_OADestroy (T-SQL)”: http://msdn.microsoft.com/library/psdk/sql/sp_oa-oz_1.htm

sp_OAGetProperty

You can use sp_OAGetProperty stored procedure to get a property value of an OLE object.

See this link for more information
“sp_OAGetProperty (T-SQL)”: http://msdn.microsoft.com/library/psdk/sql/sp_oa-oz_3.htm

Advertisement

sp_OASetProperty

You can use sp_OASetProperty stored procedure to set a property of an OLE object to a new value.

See this link for more information
“sp_OASetProperty (T-SQL)”: http://msdn.microsoft.com/library/psdk/sql/sp_oa-oz_5.htm

sp_OAMethod

You can use sp_OAMethod stored procedure to call a method of an OLE object.

This example calls the VerifyConnection method of the previously created SQL Server object:

-- Verify the connection
EXEC @hr = sp_OAMethod @object, 'VerifyConnection', @return OUT
IF @hr <> 0
BEGIN
EXEC sp_displayoaerrorinfo @object, @hr
RETURN
END

See this link for more information
“sp_OAMethod (T-SQL)”: http://msdn.microsoft.com/library/psdk/sql/sp_oa-oz_4.htm

sp_OAGetErrorInfo

This stored procedure can be used to display OLE Automation error information, when error occurs, as sp_displayoaerrorinfo stored procedure.

However, easier to use sp_displayoaerrorinfo stored procedure instead of sp_OAGetErrorInfo, because sp_displayoaerrorinfo is a wrapped stored procedure for sp_OAGetErrorInfo.

See this link for more information
“sp_OAGetErrorInfo (T-SQL)”: http://msdn.microsoft.com/library/psdk/sql/sp_oa-oz_2.htm

sp_OAStop

This stored procedure can be used to stop the OLE Automation Stored Procedures execution environment.

The execution environment will automatically restart the next time you call sp_OACreate stored procedure.

See this link for more information
“sp_OAStop (T-SQL)”: http://msdn.microsoft.com/library/psdk/sql/sp_oa-oz_6.htm

Advertisement

Example: generate script

This script will generate script for all tables and all dependent objects for the given database.

You can pass the server name, user name, user password, database name and file name into sp_GenerateScript stored procedure, as in the example below:

EXEC sp_GenerateScript @server = 'Server_Name',
                       @uname = 'User_Name',
                       @pwd = 'Password',
                       @dbname = 'Database_Name',
                       @filename = 'c:File_Name.sql'

You can specify different number of parameters (from zero to five parameters).

Alexander Chigrik

I am the owner of MSSQLCity.Com - a site dedicated to providing useful information for IT professionals using Microsoft SQL Server. This site contains SQL Server Articles, FAQs, Scripts, Tips and Test Exams.

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.