<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bahramov's Personal Blog &#187; SQL Server</title>
	<atom:link href="http://www.bahramov.com/tag/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bahramov.com</link>
	<description>Computers, databases, networks and virtualization</description>
	<lastBuildDate>Fri, 24 Jun 2011 12:05:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>T-SQL: Find logical name and physical location of system databases</title>
		<link>http://www.bahramov.com/2009/11/03/t-sql-find-logical-name-and-physical-location-of-system-databases/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/11/03/t-sql-find-logical-name-and-physical-location-of-system-databases/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 14:01:04 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/2009/11/03/t-sql-find-logical-name-and-physical-location-of-system-databases/</guid>
		<description><![CDATA[The following T-SQL statements will print the logical name and physical location of system database files:
USE master;
GO
SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'master')

SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'tempdb')

SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'model')

SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'msdb')

Result:

]]></description>
			<content:encoded><![CDATA[<p>The following T-SQL statements will print the logical name and physical location of system database files:</p>
<pre class="code"><span style="color: blue">USE master</span><span style="color: gray">;
</span><span style="color: blue">GO
SELECT </span>name<span style="color: gray">, </span>physical_name <span style="color: blue">AS </span>CurrentLocation<span style="color: gray">, </span>state_desc
<span style="color: blue">FROM </span><span style="color: green">sys</span><span style="color: gray">.</span><span style="color: green">master_files
</span><span style="color: blue">WHERE </span>database_id <span style="color: gray">= </span><span style="color: magenta">DB_ID</span><span style="color: gray">(</span><span style="color: red">N'master'</span><span style="color: gray">)

</span><span style="color: blue">SELECT </span>name<span style="color: gray">, </span>physical_name <span style="color: blue">AS </span>CurrentLocation<span style="color: gray">, </span>state_desc
<span style="color: blue">FROM </span><span style="color: green">sys</span><span style="color: gray">.</span><span style="color: green">master_files
</span><span style="color: blue">WHERE </span>database_id <span style="color: gray">= </span><span style="color: magenta">DB_ID</span><span style="color: gray">(</span><span style="color: red">N'tempdb'</span><span style="color: gray">)

</span><span style="color: blue">SELECT </span>name<span style="color: gray">, </span>physical_name <span style="color: blue">AS </span>CurrentLocation<span style="color: gray">, </span>state_desc
<span style="color: blue">FROM </span><span style="color: green">sys</span><span style="color: gray">.</span><span style="color: green">master_files
</span><span style="color: blue">WHERE </span>database_id <span style="color: gray">= </span><span style="color: magenta">DB_ID</span><span style="color: gray">(</span><span style="color: red">N'model'</span><span style="color: gray">)

</span><span style="color: blue">SELECT </span>name<span style="color: gray">, </span>physical_name <span style="color: blue">AS </span>CurrentLocation<span style="color: gray">, </span>state_desc
<span style="color: blue">FROM </span><span style="color: green">sys</span><span style="color: gray">.</span><span style="color: green">master_files
</span><span style="color: blue">WHERE </span>database_id <span style="color: gray">= </span><span style="color: magenta">DB_ID</span><span style="color: gray">(</span><span style="color: red">N'msdb'</span><span style="color: gray">)
</span></pre>
<p><strong>Result:</strong></p>
<p><a href="http://www.bahramov.com/wp-content/uploads/2009/11/image.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" title="image"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.bahramov.com/wp-content/uploads/2009/11/image_thumb.png" width="600" height="300" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/11/03/t-sql-find-logical-name-and-physical-location-of-system-databases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a compressed SQL Server Database backup</title>
		<link>http://www.bahramov.com/2009/03/19/creating-a-compressed-sql-server-database-backup/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/03/19/creating-a-compressed-sql-server-database-backup/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 18:23:25 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[batch]]></category>
		<category><![CDATA[scheduled tasks]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/?p=72</guid>
		<description><![CDATA[In this post I will describe how to create a compressed backup of any SQL Server database in Windows. This is especially useful if you have Express or MSDE edition of SQL Server where there&#8217;s no SQL Agent. Letâ€™s look a this example:

First we will create a folder, where we want to keep or database [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will describe how to create a compressed backup of any SQL Server database in Windows. This is especially useful if you have Express or MSDE edition of SQL Server where there&#8217;s no SQL Agent. Letâ€™s look a this example:</p>
<ol>
<li>First we will create a folder, where we want to keep or database backups. Let&#8217;s call it C:\DbBackup.</li>
<li>Now, weâ€™ll create a sql script that will backup our database. I will use Northwind database in this example. The created backup file will have the name Northwind_[current date]. bak Open a Notepad and paste the following T-SQL code:</li>
<pre class="brush: sql">-- Create backup of NorthWind Database
-- add current date to the file name and
-- save it inside C:\DbBackup folder
--
DECLARE @str NVARCHAR(255)
SET @str=N&#039;C:\DbBackup\Northwind_&#039;+CONVERT(VARCHAR, GETDATE(), 105)+&#039;.bak&#039;
BACKUP DATABASE Northwind TO DISK=@str</pre>
<li>Save file as NorthwindBackup.sql</li>
</ol>
<p><span id="more-72"></span><br />
Another thing that we want to do is to compress the backup file so that it takes less space on our hard drive. Thereâ€™re many software out there both commercial, like <a href="http://www.rarlabs.com" target="_blank">WinRAR</a>, <a href="http://www.winzip.com">WinZIP </a>and free â€“ <a href="http://www.7-zip.org/">7-Zip</a>. Also, you can use the COMPRESS.EXE utility from Windows NT/2000/ZP/2003 Resource Kit available for download from Microsoft site, to create a compressed archive. In this post I will show how to create archive with Compress.exe and Rar.exe utilities.</p>
<p>Create a new text file and write the following code [if using Compress.exe]:</p>
<pre class="brush: php">sqlcmd -E -i NorthwindBackup.sql -o Result.txt
compress.exe -Z -R NorthWind_*.bak NorthWind_*.zip
ren *.ba_ *.zip del *.bak *.zi_</pre>
<p>Save the file as NorthWindBackup.bat in C:\DbBackup.</p>
<p>OR</p>
<p>Create a new text file and write the following code [if using rar.exe]:</p>
<pre class="brush: php">sqlcmd -E -i NorthwindBackup.sql -o Result.txt
rar.exe a -df -inul -ag[dd_mm_yyyy] C:\DbBackup\NorthWind.rar C:\DbBackup\*.bak</pre>
<p>Save the file as NorthWindBackup.bat in C:\DbBackup.</p>
<p>When you run the NorthWindBackup.bat file it will first pass the NorthWindBackup.sql script to â€˜<a href="http://technet.microsoft.com/library/ms162773.aspx">sqlcmd.exe</a>â€™. <a href="http://technet.microsoft.com/library/ms162773.aspx">sqlcmd.exe</a>- is a command line utility of Microsoft SQL Server 2005/2008 and is available in all versions. If you are using a Microsoft SQL Server 2000 then you will have to use <a href="http://technet.microsoft.com/library/ms162806.aspx">osql.exe</a> instead of sqlcmd.exe. The usage syntax of these utilities is very similar and you can read more about their usage in BOL (<a href="http://technet.microsoft.com/library/ms162773.aspx" target="_blank">sqlcmd syntax</a> and <a href="http://technet.microsoft.com/library/ms162806.aspx" target="_blank">osql syntax</a>). In my example utility starts with the default SQL Server parameters and the result is written to Result.txt. After that the backup file is compressed and the uncompressed backup file is deleted.</p>
<p>Further, you will need to create a Scheduled Task that will execute the NorthWindBackup.bat at certain time. For this reason you may use the <a href="http://support.microsoft.com/kb/308569" target="_blank">Windows Scheduled Task</a> from visual interface or create a task directly from the command line interface ofÂ  <a href="http://technet.microsoft.com/en-us/library/cc772785.aspx" target="_blank">Schtasks.exe</a> utility.</p>
<p>Here&#8217;s a command that will create a Scheduled Task to be run every day at 8:00 PM from Local System account:</p>
<pre class="brush: plain">schtasks /create /tn &quot;NorthWindBackup&quot; /tr c:\DbBackup\NorthWindBackupR.bat /sc daily /st 20:00:00 /ed 12/12/2010 /ru &quot;NT AUTHORITY\SYSTEM&quot;</pre>
<p>Sample files can be downloaded here: <a href="http://bahramov.com/wp-content/uploads/2009/03/DbBackup.zip"><img src="http://www.bahramov.com/images/icons/winzip7064.png" alt="Zip" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/03/19/creating-a-compressed-sql-server-database-backup/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Determine Free Disk Space in SQL Server with T-SQL Code</title>
		<link>http://www.bahramov.com/2009/03/18/determine-free-disk-space-in-sql-server-with-t-sql-code/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/03/18/determine-free-disk-space-in-sql-server-with-t-sql-code/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 21:29:17 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/?p=61</guid>
		<description><![CDATA[From: www.mssqltips.com
Problem
At our organization we need to check for a minimum amount of free space before proceeding with some processes that run on SQL Server 2000, 2005 and 2008 SQL Server instances. Do you know of a way to find out the free disk space and then fail the process if it does not meet [...]]]></description>
			<content:encoded><![CDATA[<p>From: <a href="http://www.mssqltips.com/tip.asp?tip=1706">www.mssqltips.com</a></p>
<hr /><span style="text-decoration: underline;"><strong>Problem<br />
</strong></span>At our organization we need to check for a minimum amount of free space before proceeding with some processes that run on SQL Server 2000, 2005 and 2008 SQL Server instances. Do you know of a way to find out the free disk space and then fail the process if it does not meet the minimum requirements?  Can you provide some sample code?</p>
<p><span style="text-decoration: underline;"><strong>Solution<br />
</strong></span>Checking for free disk space before proceeding with a process is a wise move if disk space is tight or a high percentage of the drive is needed for the process.  It is disconcerting to have a process run for hours only to fail towards the end of the process due to insufficient disk space.  Although a few different options are available to check for disk space (CLR, WMI, PowerShell, etc.) in SQL Server, let&#8217;s see how we can use the xp_fixeddrives extended stored procedure which is available in SQL Server 2000 to 2008.</p>
<hr /><em>Sample Stored Procedure to Assess the Free Disk Space on a SQL Server Disk Drive</em></p>
<p>In the sample stored procedure below, it is accepting a parameter for the minimum amount of megabytes (MB) free on a specific disk drive, then executing the master.sys.xp_fixeddrives extended stored procedure into a temporary table.  Once the data is in the temporary table the current amount of free disk space is compared to the minimum amount of free disk space to determine if the process should continue or raise an error.<br />
<span id="more-61"></span><br />
One item to keep in mind is that between SQL Server 2000 and SQL Server 2005/2008 the owner for the xp_fixeddrives extended stored procedure changed.  In SQL Server 2000, xp_fixeddrives was owned by dbo and in SQL Server 2005/2008 the owner is sys.  Due to this ownership change, two stored procedures are provided below.  One for SQL Server 2005/2008 and a second for SQL Server 2000.</p>
<div>
<pre><em><strong>*** NOTE *** - SQL Server 2008 and 2005 Version</strong></em></pre>
</div>
<pre class="brush: sql">
CREATE PROCEDURE dbo.spExec_SufficientDiskSpace @MinMBFree int, @Drive char(1) AS
/*
----------------------------------------------------------------------------
-- Object Name: dbo.spExec_SufficientDiskSpace
-- Project: Admin Scripts
-- Business Process: Monthly Sales Reports
-- Purpose: Validate sufficient disk space
-- Detailed Description: Validate sufficient disk space based on based on the
-- @MBfree and @Drive parameters
-- Database: Admin
-- Dependent Objects: master.sys.xp_fixeddrives
-- Called By: Admin Scripts
-- Upstream Systems: Unknown
-- Downstream Systems: Unknown
--
--------------------------------------------------------------------------------------
-- Rev | CMR | Date Modified | Developer | Change Summary
--------------------------------------------------------------------------------------
-- 001 | N\A | 03.05.2009 | MSSQLTips | Original code
--
*/

SET NOCOUNT ON

-- 1 - Declare variables
DECLARE @MBfree int
DECLARE @CMD1 varchar(1000)

-- 2 - Initialize variables
SET @MBfree = 0
SET @CMD1 = &#039;&#039;

-- 3 - Create temp tables
CREATE TABLE #tbl_xp_fixeddrives
(Drive varchar(2) NOT NULL,
[MB free] int NOT NULL)

-- 4 - Populate #tbl_xp_fixeddrives
INSERT INTO #tbl_xp_fixeddrives(Drive, [MB free])
EXEC master.sys.xp_fixeddrives

-- 5 - Initialize the @MBfree value
SELECT @MBfree = [MB free]
FROM #tbl_xp_fixeddrives
WHERE Drive = @Drive

-- 6 - Determine if sufficient fre space is available
IF @MBfree &gt; @MinMBFree
BEGIN
RETURN
END
ELSE
BEGIN
RAISERROR (&#039;*** ERROR *** - Insufficient disk space.&#039;, 16, 1)
END

-- 7 - DROP TABLE #tbl_xp_fixeddrives
DROP TABLE #tbl_xp_fixeddrives

SET NOCOUNT OFF
GO
</pre>
<div>
<pre><em><strong>*** NOTE *** - SQL Server 2000 Version</strong></em></pre>
</div>
<pre class="brush: sql">CREATE PROCEDURE dbo.spExec_SufficientDiskSpace @MinMBFree int, @Drive char(1) AS
/*
----------------------------------------------------------------------------
-- Object Name: dbo.spExec_SufficientDiskSpace
-- Project: Admin Scripts
-- Business Process: Monthly Sales Reports
-- Purpose: Validate sufficient disk space
-- Detailed Description: Validate sufficient disk space based on based on the
-- @MBfree and @Drive parameters
-- Database: Admin
-- Dependent Objects: master.sys.xp_fixeddrives
-- Called By: Admin Scripts
-- Upstream Systems: Unknown
-- Downstream Systems: Unknown
--
--------------------------------------------------------------------------------------
-- Rev | CMR | Date Modified | Developer | Change Summary
--------------------------------------------------------------------------------------
-- 001 | N\A | 03.05.2009 | MSSQLTips | Original code
--
*/

SET NOCOUNT ON

-- 1 - Declare variables
DECLARE @MBfree int
DECLARE @CMD1 varchar(1000)

-- 2 - Initialize variables
SET @MBfree = 0
SET @CMD1 = &#039;&#039;

-- 3 - Create temp tables
CREATE TABLE #tbl_xp_fixeddrives
(Drive varchar(2) NOT NULL,
[MB free] int NOT NULL)

-- 4 - Populate #tbl_xp_fixeddrives
INSERT INTO #tbl_xp_fixeddrives(Drive, [MB free])
EXEC master.dbo.xp_fixeddrives

-- 5 - Initialize the @MBfree value
SELECT @MBfree = [MB free]
FROM #tbl_xp_fixeddrives
WHERE Drive = @Drive

-- 6 - Determine if sufficient fre space is available
IF @MBfree &gt; @MinMBFree
BEGIN
RETURN
END
ELSE
BEGIN
RAISERROR (&#039;*** ERROR *** - Insufficient disk space.&#039;, 16, 1)
END

-- 7 - DROP TABLE #tbl_xp_fixeddrives
DROP TABLE #tbl_xp_fixeddrives

SET NOCOUNT OFF

GO</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/03/18/determine-free-disk-space-in-sql-server-with-t-sql-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

