As it relates to the question as stated (and not the desire as stated in the "Edit"):
NO, it is not possible to call SMO from T-SQL. It cannot be done via the OLE Automation stored procedures (i.e. sp_OA*), nor via SQLCLR.
OLE Automation
I'm not sure what the OLEComponent name would be, but you can create objects by specifying their clsid. Of course, finding that for SMO isn't easy either, but I did find two references out in the inter-webz:
- what is the CLSID and PROG ID of Microsoft.sqlserver.Management.Smo (on Stack Overflow)
- To create.NET object from simple JScript. (on Programmer's Town)
DECLARE @ObjectToken INT, @HResult INT;
DECLARE @Source VARCHAR(500), @Description VARCHAR(500);
EXEC @HResult = sp_OACreate '{603F0A84-5631-3CA0-BCE0-C96597C2C1A0}', @ObjectToken OUT;
--EXEC @HResult = sp_OACreate '{755F7A20-E39C-31E0-8174-2E430C2C4E7A}', @ObjectToken OUT;
IF (@HResult <> 0)
BEGIN
EXEC sp_OAGetErrorInfo @ObjectToken, @Source OUT, @Description OUT;
RAISERROR('OLE Automation error. HResult: %x; @Source: %s; @Description: %s',
16, 1, @HResult, @Source, @Description);
RETURN;
END;
EXEC sp_OADestroy @ObjectToken;
Both values return:
Msg 50000, Level 16, State 1, Line 14
OLE Automation error. HResult: 80040154; @Source: ODSOLE Extended Procedure; @Description: Class not registered
Even though SMO is COM-visible, I suspect that it not working here is related to the following requirement, which is stated in the MSDN page for sp_OACreate
The specified OLE object must be valid and must support the IDispatch interface.
The SMO library does not appear to implement the IDispatch interface.
Even if someone does manage to find a way to get this to work, it is still ill-advised considering that the OLE Automation stored procedures have been deprecated since the release of SQL Server 2005.
SQLCLR
While SQLCLR would seem like the natural way to go with SMO (since SMO is also .NET), this definitely does not work as the SMO library specifically detects if it is being used within SQL Server, and if so, aborts with the following error message:
Msg 6522, Level 16, State 2, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "SmoTest":
System.Exception: This functionality is disabled in the SQLCLR. It is recommended that you execute from your client application.
System.Exception:
at Microsoft.SqlServer.Management.Common.ConnectionManager..ctor()
at Microsoft.SqlServer.Management.Smo.Server..ctor()
There is no way around this (well, unless you are Microsoft ;).