Skip to content

Latest commit

 

History

History
57 lines (41 loc) · 4.63 KB

ole-automation-objects-in-transact-sql.md

File metadata and controls

57 lines (41 loc) · 4.63 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic helpviewer_keywords monikerRange
OLE Automation Objects in Transact-SQL
Learn how OLE Automation objects that are executed through stored procedures run in the address space of an instance of the SQL Server Database Engine.
WilliamDAssafMSFT
wiassaf
03/10/2022
sql
stored-procedures
conceptual
triggers [SQL Server], OLE Automation
batches [SQL Server], OLE Automation
OLE Automation [SQL Server]
OLE Automation [SQL Server], about OLE Automation
>=sql-server-2016

OLE Automation Objects in Transact-SQL

[!INCLUDE SQL Server]

[!INCLUDEtsql] includes several system stored procedures that allow OLE Automation objects to be referenced in [!INCLUDEtsql] batches, stored procedures, and triggers. These system stored procedures run as extended stored procedures, and the OLE Automation objects that are executed through the stored procedures run in the address space of an instance of the [!INCLUDEssDEnoversion] in the same way that an extended stored procedure runs.

The OLE Automation stored procedures enable [!INCLUDEtsql] batches to reference SQL-DMO objects and custom OLE Automation objects, such as objects that expose the IDispatch interface. A custom in-process OLE server that is created by using [!INCLUDEmsCoName] [!INCLUDEvisual-basic] must have an error handler (specified with the On Error GoTo statement) for the Class_Initialize and Class_Terminate subroutines. Unhandled errors in the Class_Initialize and Class_Terminate subroutines can cause unpredictable errors, such as an access violation in an instance of the [!INCLUDEssDE]. Error handlers for other subroutines are also recommended.

The first step when using an OLE Automation object in [!INCLUDEtsql] is to call the sp_OACreate system stored procedure to create an instance of the object in the address space of the instance of the [!INCLUDEssDE].

After an instance of the object has been created, call the following stored procedures to work with the properties, methods, and error information related to the object:

  • sp_OAGetProperty obtains the value of a property.

  • sp_OASetProperty sets the value of a property.

  • sp_OAMethod calls a method.

  • sp_OAGetErrorInfo obtains the most recent error information.

When there is no more need for the object, call sp_OADestroy to deallocate the instance of the object created by using sp_OACreate.

OLE Automation objects return data through property values and methods. The sp_OAGetProperty and sp_OAMethod procedures return these data values in the form of a result set.

The scope of an OLE Automation object is a batch. All references to the object must be contained in a single batch, stored procedure, or trigger.

When it references objects, the [!INCLUDEssNoVersion] OLE Automation objects support traversing the referenced object to other objects that it contains. For example, when using the SQL-DMO SQLServer object, references can be made to databases and tables contained on that server.

See also

Next steps