|
Extending SQL Server with COM Objects
|
|
In the article titled
Using COM Objects in SQL Server , we saw the basics of how to access
COM objects (like DLL's) from SQL Server. Using this feature, we can extend the
basic features of SQL Server as we will see in this article. If you check-out
the XML processing capability of SQL Server, you can see that you have
functions that can load an XML from a variable and then process it. What we lack
is the ability to load an XML document from a file. In this article, we
will see how to write such a procedure using the sp_OA* set of
procedures.
In order to allow the loading of an XML from a file, we
can use the MSXML parser, which has a method called load to
load an XML from a file and a method called xml which
retrieves the XML from the DOM document as a string. This string is then
returned into a local T-SQL variable. Here is the code for the LoadXMLFromFile
procedure.
This procedure loads an XML from a give file and then
returns the XML as an output parameter. If any error is encountered in the
code, we issue a RAISERROR from the procedure. One thing to remember is that we
are bounded by the limit of 8000 characters for the XML string since TEXT
variables are not allowed to be declared as variables . Functionally,
the above procedure is equivalent to the following VB 6 code.
Actually, its very easy to write your COM component call
in a rich development environment like VB and then translate that into a SQL
Server sp_OA* call. This is useful because, we will not have access to any
intellisense for COM objects from SQL Server. Thus, you should be very familiar
with the methods and their parameters if you directly write code in SQL Server.
Ok, now having written the procedure, let's see how we can
use this to load an XML file like the following:
Here is the code for accessing our procedure to load this
file and display its contents:
The code is quite simple. We just call
our procedure with the name of the file that we created above. We have also
added some timing code to see how the code performs, since it is calling an
external COM object. After the call is successful, we then use the standard SQL
Server XML functions to load the XML into a DOM document and then display its
contents using OPENXML. When you execute this code, the
following output is shown:
You can see that the loading time for a small XML like the
above is negligible. You can work with various XML loads and see the
performance yourself. You can also extend the above procedure by adding more
error handling capabilities and other functions. Have fun!
I guess you will start appreciating the power that you
have in your hands by extending SQL Server features. Take care that your COM
component does not do something complex like opening a connection to SQL Server
and then starting a transaction. In these cases, the behavior of your
application will have performance issues and can result in deadlocks. Also,
transaction behavior becomes very complex. If you limit yourself to writing
small extensions like these, you can actually make SQL Server very powerful.
NB: Yukon promises to
change all this by integrating the .NET Common Language Runtime directly into
SQL Server. This opens a lot of doors to application programmers and
database administrators. I've collected a set of Yukon public links
here. Keep your fingers crossed till the product reaches its public
beta :-)
|
| Home |