Examples

Current Articles | Categories | Search

By David Dye @ Tuesday, May 06, 2008 8:24 AM :: 284 Views :: 0 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
Currently, there are no comments. Be the first to post one!
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

Vote For It - Server Side Compiles in SQL CLR by Jonathan Kehayias

Greg Low a SQL Server MVP, has submitted a very compelling Connect item 265266, Add server-side compilation ability to SQL CLR which aims to remove the requirement to develop SQLCLR assemblies external to SQL Server.  Read about this suggestion here, and vote for it by visiting the Connect site to let Microsoft know you think it is important.

Using CLR Impersonation to Access Resources Outside of SQL Server by Jonathan Kehayias

Traditionally if you had a need to access a file or other resource outside of SQL Server, the SQL Server service account was required to have appropriate file system access to the folder or path containing the file.  With CLR integration, this is no longer an absolute requirement.  Identity Impersonation will allow you to implicitly or explicitly change the execution context inside of a SQLCLR Function, Procedure, or Trigger. 

Using an Application Configuration (app.config/web.config) File in SQL Server CLR Integration by Jonathan Kehayias

A common part of programming in .NET is to use an configuration file to store configuration information in an easily modifiable location.  The app.config or web.config file is an invaluable inclusion in most .NET projects and developers may need to maintain this functionality as a part of logic sharing between objects in the database and the application as well.  This article will demonstrate how to configure your SQLCLR project to use Configuration Files in SQL.

Recent Examples


Copyright 2007 by SQLCLR.net Terms Of Use Privacy Statement
Website graphics provided by Matt Green Designs
Page generated in 0.6874868 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.