<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>T-SQL CLR - wiseDATAman</title><link>https://wisedataman.com/category/t-sql-clr/</link><description>SQL Server &amp; SysAdmin, Blog, Tools &amp; Scripts</description><language>en-gb</language><lastBuildDate>Sun, 12 Jul 2009 00:00:00 +0000</lastBuildDate><atom:link href="https://wisedataman.com/category/t-sql-clr/index.xml" rel="self" type="application/rss+xml"/><item><title>SQL Server CLR IO Utility</title><link>https://wisedataman.com/sql-server-clr-io-utility/</link><pubDate>Sun, 12 Jul 2009 00:00:00 +0000</pubDate><guid>https://wisedataman.com/sql-server-clr-io-utility/</guid><description>&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; the use of TRUSTWORTHY described in this article can be a potential security risk so you might want to consider alternative options.&lt;/p&gt;&#10;&lt;h2 id="introduction"&gt;Introduction&lt;/h2&gt;&#10;&lt;p&gt;Using CLR (.NET Framework) code in SQL Server is a very powerful way of extending the built-in features and functionality. The .NET framework provides access to a library of useful code, allowing you to accomplish useful tasks with a minimal amount of code.  The SQL Server IO project does just that; providing access to some useful IO utilities from within SQL Server by leveraging the power of the .NET Framework.&lt;/p&gt;&#10;&lt;h2 id="installation-instructions"&gt;Installation Instructions&lt;/h2&gt;&#10;&lt;p&gt;You need to ensure that CLR is enabled for the server and the trustworthy database setting is turned on.  The trustworthy database setting is required to allow CLR code to have external access.&lt;/p&gt;&#10;&lt;p&gt;To enable CLR, run the following code:&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;Enable CLR&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;sp_configure &amp;#39;clr enabled&amp;#39;, 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GO&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;RECONFIGURE&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;To enable the trustworthy setting for the database, run the following code:&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;SET TRUSTWORTHY ON&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ALTER DATABASE MyDatabase SET TRUSTWORTHY ON;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; the use of TRUSTWORTHY can be a potential security risk.&lt;/p&gt;&#10;&lt;p&gt;Next, download the &amp;ldquo;T-SQL Code&amp;rdquo; and run it in SQL Server Management Studio to install the CLR functions and stored procedures in the current database.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Note: Consider the security implecations before running any of the code in this article.&lt;/strong&gt;&lt;/p&gt;&#10;&lt;h2 id="code-download"&gt;Code Download&lt;/h2&gt;&#10;&lt;p&gt;&lt;img src="https://wisedataman.com/wp-content/uploads/2009/07/disk_blue.gif" alt="Download"&gt;&lt;a href="https://wisedataman.com/wp-content/uploads/2020/05/SQLServerIO_tsql_version1.zip"&gt;T-SQL Code&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://wisedataman.com/wp-content/uploads/2009/07/disk_blue.gif" alt="Download"&gt;&lt;a href="https://wisedataman.com/wp-content/uploads/2020/05/SQLServerIO_source_version1.zip"&gt;Visual Studio Source Code&lt;/a&gt;&lt;/p&gt;&#10;&lt;h2 id="clrfunctions"&gt;CLR Functions&lt;/h2&gt;&#10;&lt;h3 id="sqlio_fngetdrives"&gt;SQLIO_fnGetDrives&lt;/h3&gt;&#10;&lt;p&gt;Returns information about the drives installed on your computer/server. SELECT * FROM dbo.SQLIO_fnGetDrives()&lt;/p&gt;&#10;&lt;figure class="wp-block-image size-large"&gt;&lt;img src="https://wisedataman.com/wp-content/uploads/2020/05/SQLIO_fnGetDrives.png" alt="SQLIO_fnGetDrives function output" class="wp-image-116" loading="lazy"&gt;&lt;/figure&gt;&#10;&lt;h3 id="sqlio_fngetfiles"&gt;SQLIO_fnGetFiles&lt;/h3&gt;&#10;&lt;p&gt;Returns a list of files in the specified folder.  The function takes 3 parameters:&lt;/p&gt;&#10;&lt;p&gt;@path - The folder from which to start the search&lt;br&gt;&#10;@searchPattern - The search string to match against the names of files in path&lt;br&gt;&#10;@includeSubfolders - Specifies whether the search operation should include all subdirectories or only the current directory.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;fnGetFiles Example&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;SELECT * FROM dbo.SQLIO_fnGetFiles(&amp;#39;C:\Program Files (x86)\Microsoft SQL Server\MSSQL.4\MSSQL\LOG&amp;#39;,&amp;#39;&amp;#39;,0)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;figure class="wp-block-image size-large"&gt;&lt;img src="https://wisedataman.com/wp-content/uploads/2020/05/SQLIO_fnGetFiles-1024x258.png" alt="SQLIO_fnGetFiles function output" class="wp-image-117" loading="lazy"&gt;&lt;/figure&gt;&#10;&lt;h3 id="sqlio_fngetfolders"&gt;SQLIO_fnGetFolders&lt;/h3&gt;&#10;&lt;p&gt;Returns a list of subfolders within the specified folder.  The function takes 3 parameters:&lt;/p&gt;&#10;&lt;p&gt;@path - The folder from which to start the search&lt;br&gt;&#10;@searchPattern - The search string to match against the names of files in path&lt;br&gt;&#10;@includeSubfolders - Specifies whether the search operation should include all subdirectories or only the current directory.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;fnGetFolders&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;SELECT * FROM dbo.SQLIO_fnGetFolders(&amp;#39;C:\Program Files (x86)\Microsoft SQL Server\MSSQL.4\MSSQL&amp;#39;,&amp;#39;&amp;#39;,0)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;figure class="wp-block-image size-large"&gt;&lt;img src="https://wisedataman.com/wp-content/uploads/2009/07/SQLIO_fnGetFolders.png" alt="" class="wp-image-118" loading="lazy"&gt;&lt;/figure&gt;&#10;&lt;h3 id="sqlio_fnfileexists"&gt;SQLIO_fnFileExists&lt;/h3&gt;&#10;&lt;p&gt;Takes a file path as a parameter and returns the bit value &amp;ldquo;1&amp;rdquo; if the file exists.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;fnFileExists example&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;SELECT dbo.SQLIO_fnFileExists(&amp;#39;C:\test.txt&amp;#39;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_fnfolderexists"&gt;SQLIO_fnFolderExists&lt;/h3&gt;&#10;&lt;p&gt;Takes a folder path as a parameter and returns the bit value &amp;ldquo;1&amp;rdquo; if the folder exists.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;fnFileExists&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;SELECT dbo.SQLIO_fnFileExists(&amp;#39;C:\MyFolder&amp;#39;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_fnreadfile"&gt;SQLIO_fnReadFile&lt;/h3&gt;&#10;&lt;p&gt;Takes the path of a file as a parameter and returns the file data in varbinary format.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;fnReadFile&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;SELECT dbo.SQLIO_fnReadFile(&amp;#39;C:\Test.txt&amp;#39;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_fnreadtextfile"&gt;SQLIO_fnReadTextFile&lt;/h3&gt;&#10;&lt;p&gt;Takes the path of a file as an parameter and returns the file data in nvarchar format.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;fnReadTextFile&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;SELECT dbo.SQLIO_fnReadTextFile(&amp;#39;C:\test.txt&amp;#39;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h2 id="clrstoredprocedures"&gt;CLR Stored Procedures&lt;/h2&gt;&#10;&lt;h3 id="sqlio_deletefile"&gt;SQLIO_DeleteFile&lt;/h3&gt;&#10;&lt;p&gt;Takes the path of a file as an parameter and deletes the specified file. &lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;DeleteFile&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_DeleteFile &amp;#39;C:\test.txt&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_deletefolder"&gt;SQLIO_DeleteFolder&lt;/h3&gt;&#10;&lt;p&gt;Takes the path of a folder as a parameter and deletes the specified folder.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;DeleteFolder&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_DeleteFolder &amp;#39;C:\MyFolder&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_deletefiles"&gt;SQLIO_DeleteFiles&lt;/h3&gt;&#10;&lt;p&gt;Takes the path of a folder, a search pattern and an include subfolders parameter, similar to the dbo.SQLIO_fnGetFiles function and deletes all the files matched from the search.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;DeleteFiles&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_DeleteFiles &amp;#39;C:\Temp&amp;#39;,&amp;#39;*.tmp&amp;#39;,1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_deletefilesolderthan"&gt;SQLIO_DeleteFilesOlderThan&lt;/h3&gt;&#10;&lt;p&gt;This stored procedure is the same as the dbo.SQLIO_DeleteFiles procedure, but with an additional maxAge date parameter.  Only files older than the maxAge date specified will be deleted.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;DeleteFilesOlderThan&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_DeleteFilesOlderThan &amp;#39;C:\Temp&amp;#39;,&amp;#39;*.tmp&amp;#39;,1,&amp;#39;20090101&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_createfolder"&gt;SQLIO_CreateFolder&lt;/h3&gt;&#10;&lt;p&gt;Takes a path as a parameter and creates the new folder on the file system.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;CreateFolder&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_CreateFolder &amp;#39;C:\Temp\New Folder&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_createfile"&gt;SQLIO_CreateFile&lt;/h3&gt;&#10;&lt;p&gt;Creates a new file, by taking a file path and varbinary data value.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;CreateFile&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_CreateFile &amp;#39;C:\Temp\NewFile.txt&amp;#39;,0x68007400740070003A002F002F007700770077002E00770069007300650073006F00660074002E0063006F002E0075006B00&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_createtextfile"&gt;SQLIO_CreateTextFile&lt;/h3&gt;&#10;&lt;p&gt;Creates a new text file, by specifying a file path and string value.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;CreateTextFile&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_CreateTextFile &amp;#39;C:\Temp\NewFile.txt&amp;#39;,&amp;#39;Text Here&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_appendtextfile"&gt;SQLIO_AppendTextFile&lt;/h3&gt;&#10;&lt;p&gt;Appends text to the end of a file, by specifying a file path and text to append.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;AppendTextFile&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_AppendTextFile &amp;#39;C:\Temp\myfile.txt&amp;#39;,&amp;#39;My Appended Text&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_movefile"&gt;SQLIO_MoveFile&lt;/h3&gt;&#10;&lt;p&gt;Takes source and destination parameters, moving a file from one location to the other.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;MoveFile&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_MoveFile &amp;#39;C:\Old Folder\MyFile.txt&amp;#39;,&amp;#39;C:\New Folder\MyFile.txt&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_copyfile"&gt;SQLIO_CopyFile&lt;/h3&gt;&#10;&lt;p&gt;This stored procedure takes source and destination parameters, copying a file from one location to the other.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;CopyFile&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_CopyFile &amp;#39;C:\temp\MyFile.txt&amp;#39;,&amp;#39;C:\temp\Copy Of MyFile.txt&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="sqlio_movefolder"&gt;SQLIO_MoveFolder&lt;/h3&gt;&#10;&lt;p&gt;Takes source and destination parameters, copying a folder from one location to the other.&lt;/p&gt;&#10;&lt;div class="code-block"&gt;&lt;div class="code-title"&gt;MoveFolder&lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXEC dbo.SQLIO_MoveFolder &amp;#39;C:\Old Folder\MyFolder&amp;#39;,&amp;#39;C:\New Folder\MyFolder&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;</description></item></channel></rss>