<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://beyondrelational.com/live/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag '#sql'</title><link>http://beyondrelational.com/live/search/SearchResults.aspx?a=1&amp;o=DateDescending&amp;tag=%23sql&amp;orTags=0</link><description>Search results matching tag '#sql'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>SQL Server - Changing Rows to Columns Using PIVOT</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2012/02/18/sql-server-changing-rows-to-columns-using-pivot.aspx</link><pubDate>Sun, 19 Feb 2012 04:05:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15409</guid><dc:creator>pprajapati</dc:creator><description>&lt;div dir="ltr" style="text-align:left;"&gt;
&lt;br /&gt;
During working with one logic, i got chance to work with PIVOT operation.&amp;nbsp;Sometime we need do require rowdata as column in our custom logic, then we can use some temp table and then populate agreegate data in temp table.&amp;nbsp;But With PIVOT we can do it very easily.&amp;nbsp;Let me prepare small example and explain as how how can we use PIVOT and get row data as column.&lt;br /&gt;
&lt;br /&gt;
Before go ahead to run the script of Pivot, we will create database and table objects.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;CREATE DATABASE DEMO
GO

USE DEMO
GO

-- Creating table for demo
IF (object_id(&amp;#39;TblPivot&amp;#39;,&amp;#39;U&amp;#39;) &amp;gt; 0)
DROP TABLE TblPivot

CREATE TABLE TblPivot
(
ItemCode int,
ItemName varchar(100),
ItemColour varchar(50)
)

GO

-- Inerting some sample records

INSERT INTO TblPivot
SELECT 1,&amp;#39;Samsung Mobile&amp;#39;,&amp;#39;Red&amp;#39;
UNION ALL
SELECT 2,&amp;#39;Nokia Mobile&amp;#39;,&amp;#39;Blue&amp;#39;
UNION ALL
SELECT 3,&amp;#39;Nokia Mobile&amp;#39;,&amp;#39;Green&amp;#39;
UNION ALL
SELECT 4,&amp;#39;Motorola Mobile&amp;#39;,&amp;#39;Red&amp;#39;
UNION ALL
SELECT 5,&amp;#39;Samsung Mobile&amp;#39;,&amp;#39;Green&amp;#39;
UNION ALL
SELECT 2,&amp;#39;Nokia Mobile&amp;#39;,&amp;#39;Blue&amp;#39;
UNION ALL
SELECT 1,&amp;#39;Samsung Mobile&amp;#39;,&amp;#39;Red&amp;#39;
UNION ALL
SELECT 2,&amp;#39;Nokia Mobile&amp;#39;,&amp;#39;Blue&amp;#39;

GO
&lt;/pre&gt;
&lt;br /&gt;
Now we will check the original table data and aggregated data using Pivot.&lt;br /&gt;
So we will run both script for the same.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;-- Getting table data
SELECT 
ItemCode, 
ItemName, 
ItemColour
from TblPivot
GO

-- Getting agreegated data using Pivot and converted rows to column
SELECT
*
 FROM
 (
    SELECT 
 ItemCode, 
 ItemName, 
 ItemColour
    FROM TblPivot
 ) AS P
PIVOT
(
  Count(ItemName) FOR ItemColour IN (Red, Blue, Green)
) AS pv

GO
&lt;/pre&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:left;"&gt;
&lt;a href="http://3.bp.blogspot.com/-nFQHUhQ1NaY/Tc5AtviktiI/AAAAAAAAAfo/yubOdwUfieA/s1600/Pivot_1.png" style="margin-left:1em;margin-right:1em;"&gt;&lt;img border="0" height="640" src="http://3.bp.blogspot.com/-nFQHUhQ1NaY/Tc5AtviktiI/AAAAAAAAAfo/yubOdwUfieA/s640/Pivot_1.png" width="512" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
You can review here and see how the PIVOT is working. Let me share your experience with PIVOT operation.&lt;br /&gt;
&lt;/div&gt;</description></item><item><title>SSIS - Delete files older than retention period from folder with parameter using Script Task in SQL Server</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2012/02/10/ssis-delete-files-older-than-retention-period-from-folder-with-parameter-using-script-task-in-sql-server.aspx</link><pubDate>Fri, 10 Feb 2012 14:03:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15382</guid><dc:creator>pprajapati</dc:creator><description>&lt;div dir="ltr" style="text-align:left;"&gt;
After using some of the ways to delete the files from the&amp;nbsp;particular&amp;nbsp;folder like&amp;nbsp;&lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/30/ssis-delete-files-from-specified-folder-using-script-task-in-sql-server.aspx" target="_blank"&gt;&lt;strong&gt;Delete file using Script Task&lt;/strong&gt;&lt;/a&gt; and &amp;nbsp;&lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/23/ssis-delete-files-from-specified-folder-using-file-system-task-in-sql-server.aspx" target="_blank"&gt;&lt;strong&gt;File System Task&lt;/strong&gt;&lt;/a&gt; in SSIS. We have seen such methods and used&amp;nbsp;logic to get the files from the loop container and process to delete them and same thing applied for the scripts as well.&amp;nbsp;we also looked for the files deletion which are older than some&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;&amp;nbsp;&lt;a href="http://beyondrelational.com/blogs/paresh/archive/2012/02/04/ssis-delete-files-older-than-retention-period-from-specified-folder-using-script-task-in-sql-server.aspx" target="_blank"&gt;Retention&amp;nbsp;period with Script Task&lt;/a&gt;&lt;/span&gt;&lt;/strong&gt; also.&lt;br /&gt;
&lt;br /&gt;
In earlier posts we have not used any parameters in the scripts and directly applied folder path and retention period values there. But here i would like to use parameters for the folder path and&amp;nbsp;retention&amp;nbsp;period and pass through the scripts and delete them as per condition.&lt;br /&gt;
&lt;br /&gt;
1. Let check the files from the target folder.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-U4Ss7HEZyvU/TvM69jF50NI/AAAAAAAABMA/U40LUTPZo9g/s1600/deletefile4_0.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="312" src="http://1.bp.blogspot.com/-U4Ss7HEZyvU/TvM69jF50NI/AAAAAAAABMA/U40LUTPZo9g/s640/deletefile4_0.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
2. Create parameters and set the values. Here it is going to be delete the files which are older then 3 days from the&amp;nbsp;E:\ImagesBackup folder in this case.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-FcJ1ygMy1mE/TvM7sJR_jII/AAAAAAAABM0/3ErvmvY1XrE/s1600/deletefile4_1.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="140" src="http://2.bp.blogspot.com/-FcJ1ygMy1mE/TvM7sJR_jII/AAAAAAAABM0/3ErvmvY1XrE/s640/deletefile4_1.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
3. Drag and drop File System Task&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-CyTAkEEkW1M/TvM76LwEgnI/AAAAAAAABNA/ajEwKxBt9Nk/s1600/deletefile4_2.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="270" src="http://3.bp.blogspot.com/-CyTAkEEkW1M/TvM76LwEgnI/AAAAAAAABNA/ajEwKxBt9Nk/s640/deletefile4_2.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
4. Put the parameters as ReadOnlyVariables&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-YVP1uU7witM/TvM8bOuAVCI/AAAAAAAABNM/Vj-O9qZ8lXk/s1600/deletefile4_3.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="542" src="http://3.bp.blogspot.com/-YVP1uU7witM/TvM8bOuAVCI/AAAAAAAABNM/Vj-O9qZ8lXk/s640/deletefile4_3.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
5. Apply attached script in editor which have additional logic with condition to check the file lat&amp;nbsp;modified&amp;nbsp;date and check if older than specified retention period or not. Here you can see the parameters used in the script.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-AnTo_WNfcOQ/TvM8rjPMM5I/AAAAAAAABNY/0RjlglyP5Xw/s1600/deletefile4_4.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="368" src="http://1.bp.blogspot.com/-AnTo_WNfcOQ/TvM8rjPMM5I/AAAAAAAABNY/0RjlglyP5Xw/s640/deletefile4_4.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;span style="color:#0b5394;"&gt;Please note here we need to import system.IO namespace.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
6. Turn on final step and run package. Files older than specified retention period&amp;nbsp;get deleted.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-sc_dCAhT6mY/TvM8xEHV6LI/AAAAAAAABNk/bk-yUwdhK8k/s1600/deletefile4_5.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="430" src="http://2.bp.blogspot.com/-sc_dCAhT6mY/TvM8xEHV6LI/AAAAAAAABNk/bk-yUwdhK8k/s640/deletefile4_5.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
You &amp;nbsp;can use the script below for the same as mentioned in above image,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;--// You need to apply below one line in &amp;quot;namespaces&amp;quot; region.
using System.IO;

--//You need to apply below lines inplace of &amp;nbsp;// TODO: Add your code here
int RetentionPeriod = Convert.ToInt32(Dts.Variables[&amp;quot;User::Period&amp;quot;].Value.ToString());
string directoryPath = Dts.Variables[&amp;quot;User::BackupFolder&amp;quot;].Value.ToString();
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, &amp;quot;*.*&amp;quot;);

foreach (string currFile in oldFiles)
  {
         FileInfo currFileInfo = new FileInfo(currFile);
              
            if (currFileInfo.LastWriteTime &amp;lt; (DateTime.Now.AddDays(-RetentionPeriod)))
                {
                    currFileInfo.Delete();
                }
            }
&lt;/pre&gt;
Hope you liked this post. Stay tuned for more.  &lt;/div&gt;</description></item><item><title>SSIS - Delete files older than retention period from specified folder using Script Task in SQL Server</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2012/02/04/ssis-delete-files-older-than-retention-period-from-specified-folder-using-script-task-in-sql-server.aspx</link><pubDate>Sat, 04 Feb 2012 13:18:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15316</guid><dc:creator>pprajapati</dc:creator><description>&lt;div dir="ltr" style="text-align:left;"&gt;
Earlier we have seen for the &lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/30/ssis-delete-files-from-specified-folder-using-script-task-in-sql-server.aspx" target="_blank"&gt;&lt;span style="font-size:large;"&gt;Delete file using Script Task&lt;/span&gt;&lt;/a&gt; and&amp;nbsp;&lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/23/ssis-delete-files-from-specified-folder-using-file-system-task-in-sql-server.aspx" target="_blank"&gt;&lt;span style="font-size:large;"&gt;files deletion using File System Task&lt;/span&gt;&lt;/a&gt; in SSIS. We have directly wrote the logic to get the files and delete from specified folder in the script for the Script Task. For File System Task we have Used Foreach Loop Container and variables to hold file names which are passed from earlier stage one by one and then finally used with File System Task to delete it.&lt;br /&gt;
&lt;br /&gt;
Here we have same but not to delete all the files inside passed folder but get deleted files which are older than specified days. This is nothing but minor change in script where we pass the folder path and retention period. Lets start the steps as following,&lt;br /&gt;
&lt;br /&gt;
1. Let check the files from the target folder.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-FlO0C9E5IFU/TushRopSsMI/AAAAAAAABKs/T-AdTCQUUdA/s1600/deletefile3_0.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="272" src="http://2.bp.blogspot.com/-FlO0C9E5IFU/TushRopSsMI/AAAAAAAABKs/T-AdTCQUUdA/s640/deletefile3_0.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
2. Drag and drop Script Task.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-gOKw4ZrAsfQ/TushcJrTheI/AAAAAAAABK0/shWADAXV0FM/s1600/deletefile3_1.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="346" src="http://1.bp.blogspot.com/-gOKw4ZrAsfQ/TushcJrTheI/AAAAAAAABK0/shWADAXV0FM/s640/deletefile3_1.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
3. Open script editor from the properties.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-SP5FhW40Ls8/TushiqK2JbI/AAAAAAAABK8/pOUQM8wHuSE/s1600/deletefile3_2.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="544" src="http://2.bp.blogspot.com/-SP5FhW40Ls8/TushiqK2JbI/AAAAAAAABK8/pOUQM8wHuSE/s640/deletefile3_2.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
4. Apply attached script in editor which have additional logic with condition to check the file lat&amp;nbsp;modified&amp;nbsp;date and check if older than specified retention period or not.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-tCKDdklF7Po/TusiAyYl62I/AAAAAAAABLE/9G62pdnrnG0/s1600/deletefile3_3.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="378" src="http://4.bp.blogspot.com/-tCKDdklF7Po/TusiAyYl62I/AAAAAAAABLE/9G62pdnrnG0/s640/deletefile3_3.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;span style="color:#0b5394;"&gt;Please note here we need to import system.IO namespace.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
5. Turn on final step and run package. Files older than specified retention period&amp;nbsp;get deleted.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-8QOxRoVrpAw/TusiMIb3b3I/AAAAAAAABLM/RXWA8Oe3Vm8/s1600/deletefile3_4.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="472" src="http://4.bp.blogspot.com/-8QOxRoVrpAw/TusiMIb3b3I/AAAAAAAABLM/RXWA8Oe3Vm8/s640/deletefile3_4.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;p&gt;
You can also find script code here,&lt;/p&gt;
&lt;pre class="brush:sql"&gt;--// You need to apply below one line in &amp;quot;namespaces&amp;quot; region.
using System.IO;

--//You need to apply below lines inplace of &amp;nbsp;// TODO: Add your code here
int RetentionPeriod = 3;
string directoryPath = @&amp;quot;E:\ImagesBackup&amp;quot;;
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, &amp;quot;*.png&amp;quot;);
     foreach (string currFile in oldFiles)
            {
                FileInfo currFileInfo = new FileInfo(currFile);
                if (currFileInfo.LastWriteTime &amp;lt; (DateTime.Now.AddDays(-RetentionPeriod)))
                {
                    currFileInfo.Delete();
                }
            }
&lt;/pre&gt;
&lt;p&gt;
&lt;br /&gt;
&lt;br /&gt;
Hope you liked this post. Stay tuned for more.&lt;/p&gt;</description></item><item><title>Error encountered in SQL Server - Alter failed for Login sa. Cannot set a credential for principal 'sa'.</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2012/01/27/error-encountered-in-sql-server-alter-failed-for-login-sa-cannot-set-a-credential-for-principal-sa.aspx</link><pubDate>Sat, 28 Jan 2012 02:19:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15250</guid><dc:creator>pprajapati</dc:creator><description>&lt;div style="text-align:left;" dir="ltr"&gt;
Recently when i worked with SQL Server security, i encountered with one error while trying to modify &amp;#39;SA&amp;#39; account properties.&amp;nbsp;The exception details looks following,&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span style="color:red;"&gt;Alter failed for Login sa. Cannot set a credential for principal &amp;#39;sa&amp;#39;.&lt;/span&gt;&lt;/blockquote&gt;
Also you can see the image below for the same.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="clear:both;text-align:center;" class="separator"&gt;
&lt;/div&gt;
&lt;div style="clear:both;text-align:center;" class="separator"&gt;
&lt;a style="clear:left;float:left;margin-bottom:1em;margin-right:1em;" href="http://1.bp.blogspot.com/-cs5kvqxb5wE/Txabq6UMTiI/AAAAAAAABZE/xWGnVRabAAw/s1600/SaEnable1.png"&gt;&lt;img width="640" src="http://1.bp.blogspot.com/-cs5kvqxb5wE/Txabq6UMTiI/AAAAAAAABZE/xWGnVRabAAw/s640/SaEnable1.png" height="576" border="0" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
The &lt;span style="font-size:large;"&gt;&lt;strong&gt;fix&lt;/strong&gt; &lt;/span&gt;for the error is the option &amp;quot;&lt;span style="color:#0b5394;"&gt;&lt;strong&gt;Map to Credential&lt;/strong&gt;&lt;/span&gt;&amp;quot; is checked in the &amp;quot;General&amp;quot; tab of the Login Properties Page as mentioned below,&lt;br /&gt;
&lt;br /&gt;
&lt;div style="clear:both;text-align:center;" class="separator"&gt;
&lt;a style="clear:left;float:left;margin-bottom:1em;margin-right:1em;" href="http://3.bp.blogspot.com/-S1n5xSqGTsU/TxabcOPkHuI/AAAAAAAABY8/EeYQzhpnXKo/s1600/SaEnable2.png"&gt;&lt;img width="640" src="http://3.bp.blogspot.com/-S1n5xSqGTsU/TxabcOPkHuI/AAAAAAAABY8/EeYQzhpnXKo/s640/SaEnable2.png" height="574" border="0" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Working with Application Role - SQL Server</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2012/01/20/working-with-application-role-sql-server.aspx</link><pubDate>Fri, 20 Jan 2012 14:01:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15239</guid><dc:creator>pprajapati</dc:creator><description>&lt;div style="text-align:left;" dir="ltr"&gt;
In last post we have seen &lt;a href="http://beyondrelational.com/blogs/paresh/archive/2012/01/13/sql-server-custom-database-role.aspx" target="_blank"&gt;&lt;strong&gt;custom database roles&lt;/strong&gt;&lt;/a&gt; as how can we create it and assign required access to users. We also noticed that we can add multiple members with same role. That was the security with database roles and members comes into the picture. Now here we will study of Application Role. This is the security for the application level and no such members comes into the picture.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style="color:#0b5394;"&gt;Application Role :&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
As per msdn, An application role is a database principal that enables an application to run with its own, user-like permissions. You can use application roles to enable access to specific data to only those users who connect through a particular application.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color:#0b5394;"&gt;&lt;strong&gt;Workaround:&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
We can implement application role and take into effect with following steps, I am going to here with some of examples, so like create those required objects, so we can set them with application role.&lt;br /&gt;
&lt;br /&gt;
1. &lt;span style="text-decoration:underline;"&gt;Create required objects&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;pre class="brush:sql"&gt;USE demo
GO

CREATE TABLE SampleTable1
(
Id int,
Name varchar(10)
)
GO

CREATE TABLE SampleTable2
(
Id int,
Name varchar(10)
)
GO

CREATE PROCEDURE SampleSP1
AS 
BEGIN
SET NOCOUNT ON
SELECT * FROM SampleTable1
End
GO

CREATE PROCEDURE SampleSP2
AS 
BEGIN
SET NOCOUNT ON
SELECT * FROM SampleTable2
End
GO
&lt;/pre&gt;
&lt;br /&gt;
Here we have created same required objects as we created in earlier post for database role.&lt;br /&gt;
&lt;br /&gt;
2. &lt;span style="text-decoration:underline;"&gt;Create an application role&lt;/span&gt;&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE demo
GO

-- sp_addapprole [@rolename = ] &amp;#39;rolename&amp;#39;, [@password = ] &amp;#39;password&amp;#39;

EXEC sp_addapprole &amp;#39;AppRole&amp;#39;, &amp;#39;AppPwd&amp;#39;
GO
&lt;/pre&gt;
&lt;br /&gt;
3. &lt;span style="text-decoration:underline;"&gt;Add permission to this application role&lt;/span&gt;&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE demo
GO

GRANT SELECT ON SampleTable1 to AppRole;
GRANT SELECT, INSERT,UPDATE ON SampleTable2 to AppRole;
GRANT EXEC ON SampleSP1 to AppRole;
GO
&lt;/pre&gt;
&lt;br /&gt;
You can see we have assigned same access to application role as we did in earlier post for the custom database role.&amp;nbsp;Application role is created here now, You can also create/view with expanding Roles inside security tab for&amp;nbsp;particular&amp;nbsp;database and inside it you can find Application Role tab.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="clear:both;text-align:center;" class="separator"&gt;
&lt;a style="clear:left;float:left;margin-bottom:1em;margin-right:1em;" href="http://2.bp.blogspot.com/-meP0V-Xs5EE/TxAhc_aVgkI/AAAAAAAABXE/4mpBh4sI9BY/s1600/Approle_1.png"&gt;&lt;img width="640" src="http://2.bp.blogspot.com/-meP0V-Xs5EE/TxAhc_aVgkI/AAAAAAAABXE/4mpBh4sI9BY/s640/Approle_1.png" height="574" border="0" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
4. &lt;span style="text-decoration:underline;"&gt;Connecting database and activating application role&lt;/span&gt;&lt;br /&gt;
Application roles are enabled/activated by &lt;strong&gt;sp_settapprole &lt;/strong&gt;system stored procedure and it has required password.So application can be connected to SQL Server with this application role with scope of&amp;nbsp;particular&amp;nbsp;session and required password to&amp;nbsp;authenticate&amp;nbsp;it to connect SQL Server. To authenticate application roles and activate it it should be required to use in .Net connection code or other application database connection method code. You can refer the link &lt;span style="font-size:large;"&gt;&lt;span style="color:#0b5394;"&gt;&lt;a target="_blank" href="http://www.codeproject.com/KB/database/AppRole.aspx"&gt;here&lt;/a&gt;&lt;/span&gt; &lt;/span&gt;to use application role in application code.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color:#0b5394;"&gt;sp_settapprole&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;system stored procedure activate the application role for the specific connection while connecting with application and the syntax is as following,&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE demo
GO

-- sp_setapprole [@rolename = ] &amp;#39;rolename&amp;#39;, [@password = ] &amp;#39;password&amp;#39;

EXEC sp_setapprole &amp;#39;AppRole&amp;#39;, &amp;#39;AppPwd&amp;#39;
GO
&lt;/pre&gt;
&lt;br /&gt;
We can change the password&amp;nbsp;for the&amp;nbsp;application&amp;nbsp;role with&amp;nbsp;following,&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE demo
GO

-- sp_approlepassword [@rolename = ] &amp;#39;rolename&amp;#39;, [@password = ] &amp;#39;new password&amp;#39;

EXEC sp_approlepassword &amp;#39;AppRole&amp;#39;, &amp;#39;AppChangedPwd&amp;#39;
GO
&lt;/pre&gt;
&lt;br /&gt;
Hope these all the steps are enough to implement application role. Stay tuned for more.&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>SQL Server - Custom Database Role</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2012/01/13/sql-server-custom-database-role.aspx</link><pubDate>Fri, 13 Jan 2012 13:53:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15198</guid><dc:creator>pprajapati</dc:creator><description>&lt;div dir="ltr" style="text-align:left;"&gt;
Recently while working with database security, I learned database roles as how the each role used. Apart from the server level roles if we need to require to assign access/rights to particular databases level then we need to go through database level roles.&lt;br /&gt;
&lt;br /&gt;
Following are the fixed database level roles as per &lt;a href="http://msdn.microsoft.com/en-us/library/ms189121.aspx" target="_blank"&gt;MSDN&lt;/a&gt;,&lt;br /&gt;
&lt;strong&gt;db_owner :&lt;/strong&gt;
Members of the db_owner fixed database role can perform all configuration and maintenance activities on the database, and can also drop the database.&lt;br /&gt;
&lt;strong&gt;db_securityadmin :&lt;/strong&gt;
Members of the db_securityadmin fixed database role can modify role membership and manage permissions. Adding principals to this role could enable unintended privilege escalation.&lt;br /&gt;
&lt;strong&gt;db_accessadmin :&lt;/strong&gt;
Members of the db_accessadmin fixed database role can add or remove access to the database for Windows logins, Windows groups, and SQL Server logins.&lt;br /&gt;
&lt;strong&gt;db_backupoperator :&lt;/strong&gt;
Members of the db_backupoperator fixed database role can back up the database.&lt;br /&gt;
&lt;strong&gt;db_ddladmin :&lt;/strong&gt;
Members of the db_ddladmin fixed database role can run any Data Definition Language (DDL) command in a database.&lt;br /&gt;
&lt;strong&gt;db_datawriter :&lt;/strong&gt;
Members of the db_datawriter fixed database role can add, delete, or change data in all user tables.&lt;br /&gt;
&lt;strong&gt;db_datareader :
&lt;/strong&gt;Members of the db_datareader fixed database role can read all data from all user tables.&lt;br /&gt;
&lt;strong&gt;db_denydatawriter :&lt;/strong&gt;
Members of the db_denydatawriter fixed database role cannot add, modify, or delete any data in the user tables within a database.&lt;br /&gt;
&lt;strong&gt;db_denydatareader :
&lt;/strong&gt;Members of the db_denydatareader fixed database role cannot read any data in the user tables within a database.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-zaQtyTnHKZ8/Tw_SsTYDWEI/AAAAAAAABW0/n9RI--LrhQo/s1600/DBroles_1.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="574" src="http://4.bp.blogspot.com/-zaQtyTnHKZ8/Tw_SsTYDWEI/AAAAAAAABW0/n9RI--LrhQo/s640/DBroles_1.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
You can see the image for all above fixed database roles.&amp;nbsp;Now we will see how can we use the roles and bind with users. Let&amp;#39;s create a small demo with example.&lt;br /&gt;
&lt;br /&gt;
Here i am creating required objects used for demo, So let&amp;#39;s do that.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE demo
GO

CREATE TABLE SampleTable1
(
Id int,
Name varchar(10)
)
GO

CREATE TABLE SampleTable2
(
Id int,
Name varchar(10)
)
GO

CREATE PROCEDURE SampleSP1
AS 
BEGIN
SET NOCOUNT ON
SELECT * FROM SampleTable1
End
GO

CREATE PROCEDURE SampleSP2
AS 
BEGIN
SET NOCOUNT ON
SELECT * FROM SampleTable2
End
GO
&lt;/pre&gt;
&lt;br /&gt;
Now here i want assigned to the access to user as following,&lt;br /&gt;
1. User can see the data from SampleTable2 table.&lt;br /&gt;
2. User can perform select/insert/update operation on&amp;nbsp;SampleTable1 table, not delete operation.&lt;br /&gt;
3. Can execute stored procedure&amp;nbsp;SampleSP1.&lt;br /&gt;
4. Can not execute stored procedure&amp;nbsp;SampleSP2.
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style="color:#0b5394;"&gt;Workaround 1:&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
If we assigned fixed database roles to User like db_datareader and db_datawriter for&amp;nbsp;particular&amp;nbsp;database, then user can perform all DML operation on all tables, views for the database. Even if we assigned EXECUTE permission to user then user can execute all the stored procedures.&lt;br /&gt;
&lt;br /&gt;
After all these permission we need to deny permission from the user for some of the tables and stored procedures which are not required to be&amp;nbsp;accessed.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style="color:#0b5394;"&gt;Workaround 2:&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Instead of doing above such stuffs, We will create a new custom database level roles and assigned requited access to user for the objects.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE demo
GO

CREATE LOGIN [SupportUser] 
WITH PASSWORD=N&amp;#39;SupportUser&amp;#39;, 
 DEFAULT_DATABASE=[master], 
 DEFAULT_LANGUAGE=[us_english], 
 CHECK_EXPIRATION=OFF, 
 CHECK_POLICY=OFF
GO

CREATE USER [SupportUser] FOR LOGIN [SupportUser]
GO

CREATE ROLE [SupportRole] 
Go

GRANT SELECT ON SampleTable2 TO [SupportRole];
GRANT SELECT,INSERT,UPDATE ON SampleTable1 to [SupportRole];
GRANT EXEC ON SampleSP1 to [SupportRole]
GO

EXEC sp_addrolemember N&amp;#39;SupportRole&amp;#39;, N&amp;#39;SupportUser&amp;#39;
GO
&lt;/pre&gt;
&lt;br /&gt;
Let&amp;#39;s connect the SQL Server instance with this newly created user and see the access rights,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE demo
GO

PRINT &amp;#39;Inserting in SampleTable1&amp;#39;
GO
INSERT INTO SampleTable1
 (
 Id, 
 Name
 )
SELECT 
 1,
 &amp;#39;Sample1&amp;#39;
GO

PRINT &amp;#39;Inserting in SampleTable2&amp;#39;
GO
INSERT INTO SampleTable2
 (
 Id, 
 Name
 )
SELECT 
 1,
 &amp;#39;Sample2&amp;#39;
GO

PRINT &amp;#39;Deleting from SampleTable1&amp;#39;
GO
DELETE FROM SampleTable1
GO

PRINT &amp;#39;Viewing from SampleTable1&amp;#39;
GO
SELECT * FROM SampleTable2
GO
SELECT * FROM SampleTable1
GO


PRINT &amp;#39;Executing SampleSP11&amp;#39;
GO
EXEC SampleSP1
GO
PRINT &amp;#39;Executing SampleSP2&amp;#39;
GO
EXEC SampleSP2
GO
&lt;/pre&gt;
&lt;br /&gt;
You can see the below image to see the access of running user,&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-EyaFnrKGH_Q/Tw_TNLhMiKI/AAAAAAAABW8/Nn8gEh5v23s/s1600/DBroles_2.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="518" src="http://1.bp.blogspot.com/-EyaFnrKGH_Q/Tw_TNLhMiKI/AAAAAAAABW8/Nn8gEh5v23s/s640/DBroles_2.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
Main benefit of the custom database role is role can be assigned to multiple users. You can see below script where i have assigned same role to different user. So once role created it can &amp;nbsp;be assigned to multiple users.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE demo
GO

CREATE LOGIN [DBAUser] 
WITH PASSWORD=N&amp;#39;DBAUser&amp;#39;, 
 DEFAULT_DATABASE=[master], 
 DEFAULT_LANGUAGE=[us_english], 
 CHECK_EXPIRATION=OFF, 
 CHECK_POLICY=OFF
GO

CREATE USER [DBAUser] FOR LOGIN [DBAUser]
GO

EXEC sp_addrolemember N&amp;#39;SupportRole&amp;#39;, N&amp;#39;DBAUser&amp;#39;
GO
&lt;/pre&gt;
&lt;br /&gt;
Hope you liked this post.
&lt;/div&gt;</description></item><item><title>SQL Server - Archive old database backup files using TSQL Script</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2012/01/07/sql-server-archive-old-database-backup-files-using-tsql-script.aspx</link><pubDate>Sat, 07 Jan 2012 13:37:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15161</guid><dc:creator>pprajapati</dc:creator><description>&lt;div dir="ltr" style="text-align:left;"&gt;
I experienced into one issues for database backups were failed. And this was due to space issues on disk drive. This disk drive is spacific to allocated for the database backups only.&lt;br /&gt;
&lt;br /&gt;
The space was eaten by this database backups and this drive contains so many old backups. I have manually deleted all files and continue this activity so many days.If you create a&amp;nbsp;maintenance&amp;nbsp;plans then it have option to delete old backups files.&amp;nbsp;But i have the stored procedure for the database backups. So i do not have option to delete old and unused database backup files.&lt;br /&gt;
&lt;br /&gt;
Finally, I have created a script to clean those old backups. I have created one stored procedure in which you need to pass there parameters,&amp;nbsp;One is Backup type as want to delete full, differential or transaction log backups.&amp;nbsp;Second is From days and third one is End day.&lt;br /&gt;
&lt;br /&gt;
Please make sure XP_CMDSHELL is enabled in database instance as thisis require to enable it to delete database backup files to be deleted physically.&lt;br /&gt;
Here id query to enable it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE MASTER
GO

EXEC SP_CONFIGURE &amp;#39;show advanced options&amp;#39;,1
GO
EXEC SP_CONFIGURE &amp;#39;XP_CMDSHELL&amp;#39;,1
GO
RECONFIGURE
GO
&lt;/pre&gt;
&lt;br /&gt;
I have already told you as i have created script to delete the old DATABASEPROPERTY backups, please find below SP for the same.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteDBBackupOldFiles]
@BackupType char(1) = &amp;#39;D&amp;#39;, -- &amp;#39;D&amp;#39;-Full, &amp;#39;I&amp;#39;-Differential, &amp;#39;L&amp;#39;-Log backup type
@StartDayFromToday int,
@EndDayFromToday int
AS
BEGIN
SET NOCOUNT ON

DECLARE @IsFileExists int
DECLARE @DeletedFile VARCHAR(500)
DECLARE @OldFiles VARCHAR(500)

DECLARE OldFiles CURSOR FAST_FORWARD  FOR
SELECT 
bmf.physical_device_name
from msdb.dbo.backupset bs 
INNER JOIN msdb.dbo.backupmediafamily bmf
ON (bs.media_set_id=bmf.media_set_id)
WHERE DEVICE_TYPE = 2 
AND TYPE = @BackupType 
AND BACKUP_START_DATE &amp;lt; = GETDATE() - @StartDayFromToday
AND BACKUP_START_DATE &amp;gt; = GETDATE() - @EndDayFromToday
-- AND DATEDIFF(DAY,BACKUP_START_DATE,GETDATE()) &amp;gt; BETWEEN @StartDayFromToday and @EndDayFromToday 

OPEN OldFiles

FETCH NEXT FROM OldFiles INTO @OldFiles

WHILE @@FETCH_STATUS =0
BEGIN

EXEC XP_FILEEXIST @OldFiles,@IsFileExists OUTPUT
IF @IsFileExists=1
BEGIN

PRINT &amp;#39;Deleted File : &amp;#39; + @DeletedFile

SET @DeletedFile = &amp;#39;DEL &amp;#39; + @OldFiles

EXEC XP_CMDSHELL @DeletedFile

END
FETCH NEXT FROM OldFiles INTO @OldFiles
END
CLOSE OldFiles
DEALLOCATE OldFiles
END
&lt;/pre&gt;
&lt;br /&gt;
Finally after creating a stored procedures , it is time to run and clean the old database backups. After running it will remove all the old database baclups as per specified parameters. Like we have passed &amp;#39;D&amp;#39;,3 and 10 with SP, So i will delete Full database backups whicl are older then 3 days ago and 10 days before created.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;EXEC [DeleteDBBackupOldFiles] 
  @BackupType  = &amp;#39;D&amp;#39;,
  @StartDayFromToday = 3,
  @EndDayFromToday = 10
&lt;/pre&gt;
&lt;br /&gt;
Which method you are using for old databsee backup&amp;nbsp;maintenance?&amp;nbsp;You can read my earlier posts for delete files using &lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/23/ssis-delete-files-from-specified-folder-using-file-system-task-in-sql-server.aspx" target="_blank"&gt;&lt;span style="font-size:large;"&gt;File System Task&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/30/ssis-delete-files-from-specified-folder-using-script-task-in-sql-server.aspx" target="_blank"&gt;&lt;span style="font-size:large;"&gt;Script Task&lt;/span&gt;&lt;/a&gt; in SSIS.&lt;br /&gt;
&lt;/div&gt;</description></item><item><title>SSIS - Delete files from specified folder using Script Task in SQL Server</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2011/12/30/ssis-delete-files-from-specified-folder-using-script-task-in-sql-server.aspx</link><pubDate>Fri, 30 Dec 2011 14:13:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15096</guid><dc:creator>pprajapati</dc:creator><description>&lt;div dir="ltr" style="text-align:left;"&gt;
Earlier we have seen for the &lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/23/ssis-delete-files-from-specified-folder-using-file-system-task-in-sql-server.aspx" target="_blank"&gt;&lt;span style="font-size:large;"&gt;files deletion using File System Task&lt;/span&gt;&lt;/a&gt; in SSIS. We have used it with Foreach Loop Container. Used a variable to hold file names which are passed from earlier stage one by one and then finally used with File System Task to delete it.
&lt;br /&gt;&lt;br /&gt;
Now i am going to use Script Task to delete all files from specified folder. Here i have added script to get each files from specified folder and then delete them as you can see in the following steps. Lets start to follow them.
&lt;br /&gt;&lt;br /&gt;
1. Drag and drop Script Task.
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-nVX-crqx8u4/Tuit4tft8KI/AAAAAAAABHk/D85VIUUVYqs/s1600/deletefile2_0.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="219" src="http://2.bp.blogspot.com/-nVX-crqx8u4/Tuit4tft8KI/AAAAAAAABHk/D85VIUUVYqs/s640/deletefile2_0.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
2. Open script editor from the properties.
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-J4NnKAdqkxg/Tuit_WzHs4I/AAAAAAAABHs/baZbcqfdYaE/s1600/deletefile2_4.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="542" src="http://1.bp.blogspot.com/-J4NnKAdqkxg/Tuit_WzHs4I/AAAAAAAABHs/baZbcqfdYaE/s640/deletefile2_4.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
3. Apply attached script in editor and save it.
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-7u5qT_scahg/TuiuGa30jMI/AAAAAAAABH0/1L415i0up_g/s1600/deletefile2_1.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="307" src="http://1.bp.blogspot.com/-7u5qT_scahg/TuiuGa30jMI/AAAAAAAABH0/1L415i0up_g/s640/deletefile2_1.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;span style="color:#0b5394;"&gt;Please note here we need to import system.IO namespace.&lt;/span&gt;
&lt;br /&gt;&lt;br /&gt;
4. Turn on final step and run package. Files get deleted.
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-qC85bqr5Iio/TuiuZhS80mI/AAAAAAAABH8/wpbWRWtpBFc/s1600/deletefile2_3.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="428" src="http://4.bp.blogspot.com/-qC85bqr5Iio/TuiuZhS80mI/AAAAAAAABH8/wpbWRWtpBFc/s640/deletefile2_3.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
You can also find script code here,
&lt;br /&gt;&lt;br /&gt;
&lt;pre class="brush:sql"&gt;--// You need to apply below one line in &amp;quot;namespaces&amp;quot; region.
using System.IO;

--//You need to apply below lines inplace of &amp;nbsp;// TODO: Add your code here

&amp;nbsp;string directoryPath = @&amp;quot;E:\TestFolder&amp;quot;;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, &amp;quot;*.txt&amp;quot;);
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; foreach (string currFile in oldFiles)
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FileInfo currFileInfo = new FileInfo(currFile);
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; currFileInfo.Delete();
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }
&lt;/pre&gt;
&lt;br /&gt;&lt;br /&gt;
Hope you liked this post. Stay tuned for more.
&lt;/div&gt;</description></item><item><title>SSIS - Delete files from specified folder using File System Task in SQL Server</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2011/12/23/ssis-delete-files-from-specified-folder-using-file-system-task-in-sql-server.aspx</link><pubDate>Fri, 23 Dec 2011 13:51:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15058</guid><dc:creator>pprajapati</dc:creator><description>&lt;div dir="ltr" style="text-align:left;"&gt;
We know all the various methods to delete the particular or all files from the specified folder using some of the methods like &lt;a href="http://beyondrelational.com/justlearned/posts/347/sql-server-to-delete-old-backup-db-files.aspx"&gt;xp_delete_file&lt;/a&gt;,&amp;nbsp;&lt;a href="http://beyondrelational.com/justlearned/posts/841/delete-files-using-ole-automation-procedures-in-sql-server.aspx" target="_blank"&gt;Ole Automation Procedures&lt;/a&gt;&amp;nbsp;and with xp_cmdshell&amp;nbsp;command line&amp;nbsp;utility which we used for the old files archive or cleanup purpose.
&lt;br /&gt;&lt;br /&gt;
Here i am going to share some of the screens which delete the files with SSIS using&amp;nbsp;File System Task. Let me share them one by one.
&lt;br /&gt;&lt;br /&gt;
1. Folder having some test files.
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-r6HQtgHLuJk/TuNIn73mKFI/AAAAAAAABGc/KYjnv3JXtFg/s1600/deletefile1_0.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="145" src="http://1.bp.blogspot.com/-r6HQtgHLuJk/TuNIn73mKFI/AAAAAAAABGc/KYjnv3JXtFg/s640/deletefile1_0.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
2. Drag and drop Foreach Loop Container and File System Task. &amp;nbsp;Foreach Loop Container used to get all the files inside that folder one by one and process with File System Task.&amp;nbsp;Open the&amp;nbsp;Foreach Loop Container properties, goto Collection tab and select Foreach File Enumerator as specified in screen below.
&lt;br /&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-nNurwAREGlY/TuNI3kNlT_I/AAAAAAAABGk/F90wjygLZWA/s1600/deletefile1_2.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="337" src="http://4.bp.blogspot.com/-nNurwAREGlY/TuNI3kNlT_I/AAAAAAAABGk/F90wjygLZWA/s640/deletefile1_2.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
3. Select the folder from where need to delete the files. Also apply extension if you need.
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-F7Qvx85m79Q/TuNJLejQXSI/AAAAAAAABGs/WeaI-6aUi5s/s1600/deletefile1_3.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="440" src="http://1.bp.blogspot.com/-F7Qvx85m79Q/TuNJLejQXSI/AAAAAAAABGs/WeaI-6aUi5s/s640/deletefile1_3.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-5a8ZF2vrD5o/TuNJQihbuvI/AAAAAAAABG0/w25Zy8dbdkI/s1600/deletefile1_4.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="409" src="http://3.bp.blogspot.com/-5a8ZF2vrD5o/TuNJQihbuvI/AAAAAAAABG0/w25Zy8dbdkI/s640/deletefile1_4.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;/div&gt;
4. For the file assignment we need one variable and the values allocated from&amp;nbsp;Foreach Loop Container process. So assign it from variable mapping as per shot taken below.
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-psuEqXV2HiQ/TuNJb877eII/AAAAAAAABG8/TI2iF8i2fxY/s1600/deletefile1_5.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="376" src="http://1.bp.blogspot.com/-psuEqXV2HiQ/TuNJb877eII/AAAAAAAABG8/TI2iF8i2fxY/s640/deletefile1_5.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-8DoALKnzBqQ/TuNJcRobRcI/AAAAAAAABHE/Vj6sFu3zG58/s1600/deletefile1_6.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="323" src="http://1.bp.blogspot.com/-8DoALKnzBqQ/TuNJcRobRcI/AAAAAAAABHE/Vj6sFu3zG58/s640/deletefile1_6.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
5. Drag File System Task, select Delete File operation and define Source Connection.
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-8EJQ-aV1TWg/TuNJq9f6PZI/AAAAAAAABHM/AKwdMOjo9HM/s1600/deletefile1_8.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="371" src="http://4.bp.blogspot.com/-8EJQ-aV1TWg/TuNJq9f6PZI/AAAAAAAABHM/AKwdMOjo9HM/s640/deletefile1_8.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-VW8UoSXSvnA/TuNJrRpoVHI/AAAAAAAABHU/g-3i2FQX8fg/s1600/deletefile1_9.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="451" src="http://3.bp.blogspot.com/-VW8UoSXSvnA/TuNJrRpoVHI/AAAAAAAABHU/g-3i2FQX8fg/s640/deletefile1_9.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
6. Finally run package and files will get deleted.
&lt;br /&gt;&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-Ds3Ap6ej5sw/TuNJzXNyiTI/AAAAAAAABHc/x6C2VZlZL9U/s1600/deletefile1_10.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="465" src="http://4.bp.blogspot.com/-Ds3Ap6ej5sw/TuNJzXNyiTI/AAAAAAAABHc/x6C2VZlZL9U/s640/deletefile1_10.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;/div&gt;</description></item><item><title> SQL Server - Database Backup Statistics and History</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2011/12/16/sql-server-backup-statistics-and-history.aspx</link><pubDate>Fri, 16 Dec 2011 13:56:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15000</guid><dc:creator>pprajapati</dc:creator><description>&lt;div style="text-align:left;" dir="ltr"&gt;
Every DBA has a daily activity review or monitor database backups as these database backups used for the&amp;nbsp;restoration at other place and using for the database restore which used for reporting purpose or used in log shipping purpose. Because database backups are most important factor and first option in case of&amp;nbsp;disaster&amp;nbsp;recovery even whatever types of them because in this case transaction logs can reduce the data loses.
&lt;br /&gt;&lt;br /&gt;
You can read my earlier posts for&amp;nbsp;&lt;strong&gt;&lt;a target="_blank" href="http://beyondrelational.com/blogs/paresh/archive/2011/11/30/sql-server-2008-database-backup-compression-amazing-feature-for-dba.aspx"&gt;Database Backup Compression&lt;/a&gt;,&amp;nbsp;&lt;a target="_blank" href="http://beyondrelational.com/blogs/paresh/archive/2011/11/16/sql-server-database-backup-files-verification-and-details.aspx"&gt;Database Backup files Verification&lt;/a&gt; &lt;/strong&gt;,&amp;nbsp;&lt;strong&gt;&lt;a target="_blank" href="http://beyondrelational.com/blogs/paresh/archive/2011/08/20/sql-server-automated-all-databases-backups-script.aspx"&gt;Automated All Databases Backups Script&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;and&amp;nbsp;&lt;strong&gt;&lt;a target="_blank" href="http://beyondrelational.com/blogs/paresh/archive/2011/08/24/sql-server-split-database-full-backup-to-mupltiple-files.aspx"&gt;Split Database Full Backup to Multiple files&lt;/a&gt;.&lt;/strong&gt;
&lt;br /&gt;&lt;br /&gt;
I would like to share the script which help us to show the database backups status,&amp;nbsp;history of their when they are done based on schedule, at where are taking and when, backup types&amp;nbsp;, backups physical device and size of the database backups and time to perform backup and all other related backup statistics.&amp;nbsp;Here is the script to collect the database backup statistics and status information.
&lt;br /&gt;&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE MSDB
GO

SELECT 
bs.server_name AS Server, -- Server name
bs.database_name AS DatabseName , -- Database name
CASE bs.compatibility_level 
 WHEN 80 THEN &amp;#39;SQL Server 2000&amp;#39;
 WHEN 90 THEN &amp;#39;SQL Server 2005 &amp;#39;
 WHEN 100 THEN &amp;#39;SQL Server 2008&amp;#39;
 WHEN 110 THEN &amp;#39;SQL Server 2011&amp;#39;
END AS CompatibilityLevel , -- Return backup compatibility level
recovery_model AS Recoverymodel , -- Database recovery model
CASE bs.type 
 WHEN &amp;#39;D&amp;#39; THEN &amp;#39;Full&amp;#39; 
 WHEN &amp;#39;I&amp;#39; THEN &amp;#39;Differential&amp;#39; 
 WHEN &amp;#39;L&amp;#39; THEN &amp;#39;Log&amp;#39; 
 WHEN &amp;#39;F&amp;#39; THEN &amp;#39;File or filegroup&amp;#39; 
 WHEN &amp;#39;G&amp;#39; THEN &amp;#39;Differential file&amp;#39; 
 WHEN &amp;#39;P&amp;#39; THEN &amp;#39;P&amp;#39; 
 WHEN &amp;#39;Q&amp;#39; THEN &amp;#39;Differential partial&amp;#39; 
 END AS BackupType, -- Type of database baclup
bs.backup_start_date AS BackupstartDate, -- Backup start date
bs.backup_finish_date AS BackupFinishDate,  -- Backup finish date
bmf.physical_device_name AS PhysicalDevice, -- baclup Physical localtion
CASE device_type
 WHEN 2 THEN &amp;#39;Disk - Temporary&amp;#39;
 WHEN 102  THEN &amp;#39;Disk - Permanent&amp;#39;
 WHEN 5  THEN &amp;#39;Tape - Temporary&amp;#39;
 WHEN 105  THEN &amp;#39;Tape - Temporary&amp;#39;
 else &amp;#39;Other Device&amp;#39;
END AS DeviceType, -- Device type
bs.backup_size AS  [BackupSize(In bytes)],  -- Normal backup size (In bytes)
compressed_backup_size AS [ConmpressedBackupSize(In bytes)] -- Compressed backup size (In bytes)
FROM msdb.dbo.backupset bs 
INNER JOIN msdb.dbo.backupmediafamily bmf
ON (bs.media_set_id=bmf.media_set_id)
ORDER BY bs.backup_start_date DESC

GO
&lt;/pre&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div style="clear:both;text-align:center;" class="separator"&gt;
&lt;a style="clear:left;float:left;margin-bottom:1em;margin-right:1em;" href="http://2.bp.blogspot.com/-j1Yk4mkvcFs/TdhyJLO__XI/AAAAAAAAAlk/WdzopuNjIT4/s1600/Backup_Status_History.png"&gt;&lt;img width="640" src="http://2.bp.blogspot.com/-j1Yk4mkvcFs/TdhyJLO__XI/AAAAAAAAAlk/WdzopuNjIT4/s640/Backup_Status_History.png" height="325" border="0" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
The transact-SQL for backupmediafamily and backupset are accordingly are http://msdn.microsoft.com/en-us/library/ms190284.aspx and http://msdn.microsoft.com/en-us/library/ms186299.aspx
&lt;br /&gt;&lt;br /&gt;
Hope you liked this post.
&lt;/div&gt;</description></item></channel></rss>