Examples

Current Articles | Categories | Search

By David Dye @ Tuesday, May 06, 2008 8:24 AM :: 1997 Views :: 3 Comments

As this procedure accesses the file system it is necessary to set the permission level to external.  If the file specified in the path is not present then it will be created and if it is present it will be over written.

 

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Imports System.Xml
 
 
Partial Public Class outputxml
    <Microsoft.SqlServer.Server.SqlProcedure()> _
    Public Shared Sub outputxml(ByVal XmlData As SqlXml, ByVal Filename As SqlString)
 
        'Create the variables to hold the values that are supplied by the parameters
        'input by the stored procedure
        Dim xmlDoc As New XmlDocument()
        Dim output As SqlPipe = SqlContext.Pipe()
 
        Try
            'Load the result set into the XmlDoc Variable and then save the results in the
            'path provided by the stored procedure. The values are provided by the
            'input parameters of the stored procedure
            xmlDoc.LoadXml(XmlData.Value)
            xmlDoc.Save(Filename.Value)
 
        Catch ex As Exception
            'If an error occurs catch the message and pipe it back to SQL
            output.Send(ex.Message.ToString)
        End Try
 
    End Sub
End Class

 

To create this procedure and use it in SQL Server:

--Import the assembly into SQL
ALTER DATABASE AdventureWorks SET trustworthy ON
CREATE ASSEMBLY outputxml
from 'C:\outputxml.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS
 
--If necessary alter the authorization of the database
ALTER AUTHORIZATION ON DATABASE::database_name TO valid_login
 
 
-- Create the proc from the imported dll
CREATE PROCEDURE output
@xmldata XML,
@filename nvarchar(1024)
AS
EXTERNAL NAME outputxml.[outputxml.outputxml]
.outputxml
 
 
-- Test managed stored procedure
DECLARE @output xml
SET @output = (SELECT ProductID, Name, ListPrice
                    FROM Production.Product Product
                    FOR XML AUTO, ROOT('Catalog'), TYPE)
EXEC dbo.outputxml @output, 'c:\Output.xml'
GO
Posted @ Tuesday, May 06, 2008 8:24 AM by David Dye
Previous Page | Next Page
Comments
By alan cannon on Wednesday, February 24, 2010
the structre listed is excelent Thank you for posting the information. I need some additional help with the Create assembly / User rights being set to external access assembly..
Thanks

By Eric Silva on Wednesday, November 24, 2010
I have a project similar to this. So could you explain what the first half of the example is? is it a vb.net app? or how do I set up the example step by step?

By David Dye on Saturday, January 08, 2011
Eric,
I am terribly sorry that I am just now responding to your comment. I just noticed that you posted this and the date. This is a CLR stored proecdure written in VB.net. The code is compiled into a .dll and imported into SQL Server 2005 or greater as an assembly, this can be done with t-sql or directly with Visual Studio. A stored procedure is then created referencing the assembly.

Hope this answered your question

You must be logged in to post a comment. You can login here

Survey

Which of the following CLR objects are you currently using in SQL Server?




Submit Survey  View Results

Links

  Search

What's New

 Subscribe in a reader

Q&A with Adam Machanic - Who's Afraid of SQLCLR by Jonathan Kehayias

You could almost hear the gasps of horror when Microsoft integrated the .NET Common Language Runtime (CLR) into SQL Server 2005. Run C# or VB.NET code inside SQL Server? For many database developers and administrators, it was like an alien had just burst through SQL Server’s midsection.

But SQL Server MVP Adam Machanic says that more and more developers and DBAs are discovering that far from destroying SQL Server performance and security, SQLCLR is actually a powerful ally in solving complex business and technical problems.

Read More on the PASS website..

SQLCLR String Splitting Part 2: Even Faster, Even More Scalable by Site Administrator

SQL Server MVP Adam Machanic shows a new way of parsing strings in SQL Server using SQLCLR that outperforms all conventional TSQL methods, as well as the most common SQLCLR implemenations, using a custom split function that allows streaming of the results.

Trading in xp_cmdshell for SQLCLR (Part 1) - List Directory Contents by Jonathan Kehayias

Learn how to use SQLCLR to get file system information instead of using xp_cmdshell on your SQL Servers.

Recent Examples


Copyright 2007 by SQLCLR.net Terms Of Use Privacy Statement
Website graphics provided by Matt Green Designs
Page generated in 0.1872012 seconds.

All information and example code on this site is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from its use.

This site is in no way affiliated with Microsoft. Unless specifically stated otherwise, nothing should be construed to represent the official positions or opinions of Microsoft and/or its Employees.