<?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>Network Today Database Tutorials</title>
	<atom:link href="http://www.networktoday.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.networktoday.org</link>
	<description>Database Tutorials HTML HTTP Javascript jQuery Linux MS SQL MySQL Optimization Performance Perl Tutorial PHP Tutorial SEO Tutorial SQL Tutorial WebDesign Windows Windows 7 Windows Server 2008 Windows Vista</description>
	<lastBuildDate>Tue, 21 Feb 2012 11:55:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SQL Server DATEDIFF() Function</title>
		<link>http://www.networktoday.org/sql-server-datediff-function/</link>
		<comments>http://www.networktoday.org/sql-server-datediff-function/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 11:53:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[SQL Tutorial]]></category>
		<category><![CDATA[DATEDIFF]]></category>
		<category><![CDATA[SQL DATEDIFF]]></category>
		<category><![CDATA[SQL Server DATEDIFF]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=370</guid>
		<description><![CDATA[Definition and Usage The DATEDIFF() function returns the time between two dates. Syntax DATEDIFF(datepart,startdate,enddate) Where startdate and enddate are valid date expressions and datepart can be one of the following: datepart Abbreviation year yy, yyyy quarter qq, q month mm, m dayofyear dy, y day dd, d week wk, ww weekday dw, w hour hh [...]]]></description>
			<content:encoded><![CDATA[<h2>Definition and Usage</h2>
<p>The DATEDIFF() function returns the time between two dates.</p>
<h3><strong>Syntax</strong></h3>
<div>
<div>DATEDIFF(datepart,startdate,enddate)</div>
</div>
<p>Where startdate and enddate are valid date expressions and datepart can be one of the following:</p>
<table>
<tbody>
<tr>
<th align="left">datepart</th>
<th align="left">Abbreviation</th>
</tr>
<tr>
<td>year</td>
<td>yy, yyyy</td>
</tr>
<tr>
<td>quarter</td>
<td>qq, q</td>
</tr>
<tr>
<td>month</td>
<td>mm, m</td>
</tr>
<tr>
<td>dayofyear</td>
<td>dy, y</td>
</tr>
<tr>
<td>day</td>
<td>dd, d</td>
</tr>
<tr>
<td>week</td>
<td>wk, ww</td>
</tr>
<tr>
<td>weekday</td>
<td>dw, w</td>
</tr>
<tr>
<td>hour</td>
<td>hh</td>
</tr>
<tr>
<td>minute</td>
<td>mi, n</td>
</tr>
<tr>
<td>second</td>
<td>ss, s</td>
</tr>
<tr>
<td>millisecond</td>
<td>ms</td>
</tr>
<tr>
<td>microsecond</td>
<td>mcs</td>
</tr>
<tr>
<td>nanosecond</td>
<td>ns</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<hr />
<h3><strong>Example</strong></h3>
<p>Now we want to get the number of days between two dates.</p>
<p>We use the following SELECT statement:</p>
<div>
<div>SELECT DATEDIFF(day,&#8217;2008-06-05&#8242;,&#8217;2008-08-05&#8242;) AS DiffDate</div>
</div>
<p>Result:</p>
<table>
<tbody>
<tr>
<th align="left">DiffDate</th>
</tr>
<tr>
<td>61</td>
</tr>
</tbody>
</table>
<h3><strong>Example</strong></h3>
<p>Now we want to get the number of days between two dates (notice that the second date is &#8220;earlier&#8221; than the first date, and will result in a negative number).</p>
<p>We use the following SELECT statement:</p>
<div>
<div>SELECT DATEDIFF(day,&#8217;2008-08-05&#8242;,&#8217;2008-06-05&#8242;) AS DiffDate</div>
</div>
<p>Result:</p>
<table>
<tbody>
<tr>
<th align="left">DiffDate</th>
</tr>
<tr>
<td>-61</td>
</tr>
</tbody>
</table>
<p>Reference &#8211; http://www.w3schools.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/sql-server-datediff-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ALTER TABLE Statement</title>
		<link>http://www.networktoday.org/alter-table-statement/</link>
		<comments>http://www.networktoday.org/alter-table-statement/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 09:54:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[SQL Tutorial]]></category>
		<category><![CDATA[add a column]]></category>
		<category><![CDATA[add multiple columns]]></category>
		<category><![CDATA[ALTER TABLE]]></category>
		<category><![CDATA[DROP COLUMN]]></category>
		<category><![CDATA[modify column]]></category>
		<category><![CDATA[modify multiple columns]]></category>
		<category><![CDATA[sql rename table]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=366</guid>
		<description><![CDATA[The ALTER TABLE statement allows you to rename an existing table. It can also be used to add, modify, or drop a column from an existing table. &#160; Renaming a table The basic syntax for renaming a table is: ALTER TABLE table_name RENAME TO new_table_name; For example: ALTER TABLE suppliers RENAME TO vendors; This will [...]]]></description>
			<content:encoded><![CDATA[<p>The ALTER TABLE statement allows you to rename an existing table. It can also be used to add, modify, or drop a column from an existing table.</p>
<p>&nbsp;</p>
<h2>Renaming a table</h2>
<p>The basic syntax for renaming a table is:</p>
<blockquote><p>ALTER TABLE table_name<br />
RENAME TO new_table_name;</p></blockquote>
<p>For example:</p>
<blockquote><p>ALTER TABLE suppliers<br />
RENAME TO vendors;</p></blockquote>
<p>This will rename the <em>suppliers</em> table to <em>vendors</em>.</p>
<p>&nbsp;</p>
<h2>Adding column(s) to a table</h2>
<p><strong>Syntax #1</strong></p>
<p>To add a column to an existing table, the ALTER TABLE syntax is:</p>
<blockquote><p>ALTER TABLE table_name<br />
ADD column_name column-definition;</p></blockquote>
<p>For example:</p>
<blockquote><p>ALTER TABLE supplier<br />
ADD supplier_name  varchar2(50);</p></blockquote>
<p>This will add a column called <em>supplier_name</em> to the <em>supplier</em> table.</p>
<p>&nbsp;</p>
<p><strong>Syntax #2</strong></p>
<p>To add multiple columns to an existing table, the ALTER TABLE syntax is:</p>
<blockquote>
<table width="245" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="3" width="243">ALTER TABLE table_name</td>
</tr>
<tr>
<td width="38">ADD (</td>
<td width="71">column_1</td>
<td width="130">column-definition,</td>
</tr>
<tr>
<td width="38"></td>
<td width="71">column_2</td>
<td width="130">column-definition,</td>
</tr>
<tr>
<td width="38"></td>
<td width="71">&#8230;</td>
<td width="130"></td>
</tr>
<tr>
<td width="38"></td>
<td width="71">column_n</td>
<td width="130">column_definition );</td>
</tr>
</tbody>
</table>
</blockquote>
<p>For example:</p>
<blockquote>
<table width="251" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="3" width="249">ALTER TABLE supplier</td>
</tr>
<tr>
<td width="38">ADD (</td>
<td width="100">supplier_name</td>
<td width="107">varchar2(50),</td>
</tr>
<tr>
<td width="38"></td>
<td width="100">city</td>
<td width="107">varchar2(45) );</td>
</tr>
</tbody>
</table>
</blockquote>
<p>This will add two columns (<em>supplier_name</em> and <em>city</em>) to the <em>supplier</em> table.</p>
<p>&nbsp;</p>
<h2>Modifying column(s) in a table</h2>
<p><strong>Syntax #1</strong></p>
<p>To modify a column in an existing table, the ALTER TABLE syntax is:</p>
<blockquote><p>ALTER TABLE table_name<br />
MODIFY column_name column_type;</p></blockquote>
<p>For example:</p>
<blockquote><p>ALTER TABLE supplier<br />
MODIFY supplier_name   varchar2(100)     not null;</p></blockquote>
<p>This will modify the column called <em>supplier_name</em> to be a data type of varchar2(100) and force the column to not allow null values.</p>
<p>&nbsp;</p>
<p><strong>Syntax #2</strong></p>
<p>To modify multiple columns in an existing table, the ALTER TABLE syntax is:</p>
<blockquote>
<table width="249" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="3" width="243">ALTER TABLE table_name</td>
</tr>
<tr>
<td width="65">MODIFY (</td>
<td width="67">column_1</td>
<td width="111">column_type,</td>
</tr>
<tr>
<td width="65"></td>
<td width="67">column_2</td>
<td width="111">column_type,</td>
</tr>
<tr>
<td width="65"></td>
<td width="67">&#8230;</td>
<td width="111"></td>
</tr>
<tr>
<td width="65"></td>
<td width="67">column_n</td>
<td width="111">column_type );</td>
</tr>
</tbody>
</table>
</blockquote>
<p>For example:</p>
<blockquote>
<table width="338" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="5" width="319">ALTER TABLE supplier</td>
</tr>
<tr>
<td width="62">MODIFY (</td>
<td width="98">supplier_name</td>
<td width="97">varchar2(100)</td>
<td colspan="2" width="75">not null,</td>
</tr>
<tr>
<td width="62"></td>
<td width="98">city</td>
<td width="97">varchar2(75)</td>
<td width="38"></td>
<td width="37">);</td>
</tr>
</tbody>
</table>
</blockquote>
<p>This will modify both the <em>supplier_name</em> and <em>city</em> columns.</p>
<p>&nbsp;</p>
<h2>Drop column(s) in a table</h2>
<p><strong>Syntax #1</strong></p>
<p>To drop a column in an existing table, the ALTER TABLE syntax is:</p>
<blockquote><p>ALTER TABLE table_name<br />
DROP COLUMN column_name;</p></blockquote>
<p>For example:</p>
<blockquote><p>ALTER TABLE supplier<br />
DROP COLUMN supplier_name;</p></blockquote>
<p>This will drop the column called <em>supplier_name</em> from the table called <em>supplier</em>.</p>
<p>&nbsp;</p>
<h2>Rename column(s) in a table<br />
(NEW in Oracle 9i Release 2)</h2>
<p><strong>Syntax #1</strong></p>
<p>Starting in Oracle 9i Release 2, you can now rename a column.</p>
<p>To rename a column in an existing table, the ALTER TABLE syntax is:</p>
<blockquote><p>ALTER TABLE table_name<br />
RENAME COLUMN old_name to new_name;</p></blockquote>
<p>For example:</p>
<blockquote><p>ALTER TABLE supplier<br />
RENAME COLUMN supplier_name to sname;</p></blockquote>
<p>This will rename the column called <em>supplier_name</em> to <em>sname</em>.</p>
<p><em>Acknowledgements</em>: Thanks to Dave M., Craig A., and Susan W. for contributing to this solution!</p>
<p>&nbsp;</p>
<p>Practice Exercise #1:</p>
<p>Based on the <em>departments</em> table below, rename the <em>departments</em> table to <em>depts</em>.</p>
<blockquote>
<table width="385" border="0" cellspacing="0">
<tbody>
<tr>
<td colspan="4">CREATE TABLE departments</td>
</tr>
<tr>
<td width="8">(</td>
<td width="123">department_id</td>
<td width="120">number(10)</td>
<td width="126">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">department_name</td>
<td width="120">varchar2(50)</td>
<td width="126">not null,</td>
</tr>
<tr>
<td></td>
<td colspan="3">CONSTRAINT departments_pk PRIMARY KEY (department_id)</td>
</tr>
<tr>
<td width="8">);</td>
<td width="123"></td>
<td width="120"></td>
<td width="126"></td>
</tr>
</tbody>
</table>
</blockquote>
<p><strong>Solution</strong>:</p>
<p>The following ALTER TABLE statement would rename the <em>departments</em> table to <em>depts</em>:</p>
<blockquote><p>ALTER TABLE departments<br />
RENAME TO depts;</p></blockquote>
<p>&nbsp;</p>
<p>Practice Exercise #2:</p>
<p>Based on the <em>employees</em> table below, add a column called <em>salary</em> that is a number(6) datatype.</p>
<blockquote>
<table width="407" border="0" cellspacing="0">
<tbody>
<tr>
<td colspan="4">CREATE TABLE employees</td>
</tr>
<tr>
<td width="8">(</td>
<td width="123">employee_number</td>
<td width="120">number(10)</td>
<td width="148">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">employee_name</td>
<td width="120">varchar2(50)</td>
<td width="148">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">department_id</td>
<td width="120">number(10),</td>
<td width="148"></td>
</tr>
<tr>
<td></td>
<td colspan="3">CONSTRAINT employees_pk PRIMARY KEY (employee_number)</td>
</tr>
<tr>
<td width="8">);</td>
<td width="123"></td>
<td width="120"></td>
<td width="148"></td>
</tr>
</tbody>
</table>
</blockquote>
<p><strong>Solution</strong>:</p>
<p>The following ALTER TABLE statement would add a <em>salary</em> column to the <em>employees</em> table:</p>
<blockquote><p>ALTER TABLE employees<br />
ADD salary number(6);</p></blockquote>
<p>&nbsp;</p>
<p>Practice Exercise #3:</p>
<p>Based on the <em>customers</em> table below, add two columns &#8211; one column called <em>contact_name</em> that is a varchar2(50) datatype and one column called <em>last_contacted</em> that is a date datatype.</p>
<blockquote>
<table width="350" border="0" cellspacing="0">
<tbody>
<tr>
<td colspan="4">CREATE TABLE customers</td>
</tr>
<tr>
<td width="8">(</td>
<td width="123">customer_id</td>
<td width="120">number(10)</td>
<td width="91">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">customer_name</td>
<td width="120">varchar2(50)</td>
<td width="91">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">address</td>
<td width="120">varchar2(50),</td>
<td width="91"></td>
</tr>
<tr>
<td></td>
<td>city</td>
<td>varchar2(50),</td>
<td></td>
</tr>
<tr>
<td></td>
<td>state</td>
<td>varchar2(25),</td>
<td></td>
</tr>
<tr>
<td></td>
<td>zip_code</td>
<td>varchar2(10),</td>
<td></td>
</tr>
<tr>
<td></td>
<td colspan="3">CONSTRAINT customers_pk PRIMARY KEY (customer_id)</td>
</tr>
<tr>
<td width="8">);</td>
<td width="123"></td>
<td width="120"></td>
<td width="91"></td>
</tr>
</tbody>
</table>
</blockquote>
<p><strong>Solution</strong>:</p>
<p>The following ALTER TABLE statement would add the <em>contact_name</em> and <em>last_contacted</em> columns to the <em>customers</em> table:</p>
<blockquote>
<table width="251" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="3" width="249">ALTER TABLE customers</td>
</tr>
<tr>
<td width="38">ADD (</td>
<td width="100">contact_name</td>
<td width="107">varchar2(50),</td>
</tr>
<tr>
<td width="38"></td>
<td width="100">last_contacted</td>
<td width="107">date );</td>
</tr>
</tbody>
</table>
</blockquote>
<p>&nbsp;</p>
<p>Practice Exercise #4:</p>
<p>Based on the <em>employees</em> table below, change the <em>employee_name</em> column to a varchar2(75) datatype.</p>
<blockquote>
<table width="407" border="0" cellspacing="0">
<tbody>
<tr>
<td colspan="4">CREATE TABLE employees</td>
</tr>
<tr>
<td width="8">(</td>
<td width="123">employee_number</td>
<td width="120">number(10)</td>
<td width="148">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">employee_name</td>
<td width="120">varchar2(50)</td>
<td width="148">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">department_id</td>
<td width="120">number(10),</td>
<td width="148"></td>
</tr>
<tr>
<td></td>
<td colspan="3">CONSTRAINT employees_pk PRIMARY KEY (employee_number)</td>
</tr>
<tr>
<td width="8">);</td>
<td width="123"></td>
<td width="120"></td>
<td width="148"></td>
</tr>
</tbody>
</table>
</blockquote>
<p><strong>Solution</strong>:</p>
<p>The following ALTER TABLE statement would change the datatype for the <em>employee_name</em> column to varchar2(75):</p>
<blockquote><p>ALTER TABLE employees<br />
MODIFY employee_name varchar2(75);</p></blockquote>
<p>&nbsp;</p>
<p>Practice Exercise #5:</p>
<p>Based on the <em>customers</em> table below, change the <em>customer_name</em> column to NOT allow null values and change the <em>state</em> column to a varchar2(2) datatype.</p>
<blockquote>
<table width="350" border="0" cellspacing="0">
<tbody>
<tr>
<td colspan="4">CREATE TABLE customers</td>
</tr>
<tr>
<td width="8">(</td>
<td width="123">customer_id</td>
<td width="120">number(10)</td>
<td width="91">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">customer_name</td>
<td width="120">varchar2(50),</td>
<td width="91"></td>
</tr>
<tr>
<td width="8"></td>
<td width="123">address</td>
<td width="120">varchar2(50),</td>
<td width="91"></td>
</tr>
<tr>
<td></td>
<td>city</td>
<td>varchar2(50),</td>
<td></td>
</tr>
<tr>
<td></td>
<td>state</td>
<td>varchar2(25),</td>
<td></td>
</tr>
<tr>
<td></td>
<td>zip_code</td>
<td>varchar2(10),</td>
<td></td>
</tr>
<tr>
<td></td>
<td colspan="3">CONSTRAINT customers_pk PRIMARY KEY (customer_id)</td>
</tr>
<tr>
<td width="8">);</td>
<td width="123"></td>
<td width="120"></td>
<td width="91"></td>
</tr>
</tbody>
</table>
</blockquote>
<p><strong>Solution</strong>:</p>
<p>The following ALTER TABLE statement would modify the <em>customer_name</em> and <em>state</em> columns accordingly in the <em>customers</em> table:</p>
<blockquote>
<table width="351" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="3">ALTER TABLE customers</td>
</tr>
<tr>
<td width="62">MODIFY (</td>
<td width="119">customer_name</td>
<td width="170">varchar2(50) not null,</td>
</tr>
<tr>
<td width="62"></td>
<td width="119">state</td>
<td width="170">varchar2(2) );</td>
</tr>
</tbody>
</table>
</blockquote>
<p>&nbsp;</p>
<p>Practice Exercise #6:</p>
<p>Based on the <em>employees</em> table below, drop the <em>salary</em> column.</p>
<blockquote>
<table width="407" border="0" cellspacing="0">
<tbody>
<tr>
<td colspan="4">CREATE TABLE employees</td>
</tr>
<tr>
<td width="8">(</td>
<td width="123">employee_number</td>
<td width="120">number(10)</td>
<td width="148">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">employee_name</td>
<td width="120">varchar2(50)</td>
<td width="148">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">department_id</td>
<td width="120">number(10),</td>
<td width="148"></td>
</tr>
<tr>
<td></td>
<td>salary</td>
<td>number(6),</td>
<td></td>
</tr>
<tr>
<td></td>
<td colspan="3">CONSTRAINT employees_pk PRIMARY KEY (employee_number)</td>
</tr>
<tr>
<td width="8">);</td>
<td width="123"></td>
<td width="120"></td>
<td width="148"></td>
</tr>
</tbody>
</table>
</blockquote>
<p><strong>Solution</strong>:</p>
<p>The following ALTER TABLE statement would drop the <em>salary</em> column from the <em>employees</em> table:</p>
<blockquote><p>ALTER TABLE employees<br />
DROP COLUMN salary;</p></blockquote>
<p>&nbsp;</p>
<p>Practice Exercise #7:</p>
<p>Based on the <em>departments</em> table below, rename the <em>department_name</em> column to <em>dept_name</em>.</p>
<blockquote>
<table width="385" border="0" cellspacing="0">
<tbody>
<tr>
<td colspan="4">CREATE TABLE departments</td>
</tr>
<tr>
<td width="8">(</td>
<td width="123">department_id</td>
<td width="120">number(10)</td>
<td width="126">not null,</td>
</tr>
<tr>
<td width="8"></td>
<td width="123">department_name</td>
<td width="120">varchar2(50)</td>
<td width="126">not null,</td>
</tr>
<tr>
<td></td>
<td colspan="3">CONSTRAINT departments_pk PRIMARY KEY (department_id)</td>
</tr>
<tr>
<td width="8">);</td>
<td width="123"></td>
<td width="120"></td>
<td width="126"></td>
</tr>
</tbody>
</table>
</blockquote>
<p><strong>Solution</strong>:</p>
<p>The following ALTER TABLE statement would rename the <em>department_name</em> column to <em>dept_name</em> in the <em>departments</em> table:</p>
<blockquote><p>ALTER TABLE departments<br />
RENAME COLUMN department_name to dept_name;<br />
&nbsp;</p></blockquote>
<p>Reference &#8211; http://www.techonthenet.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/alter-table-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL SERVER – How to Rename a Column Name or Table Name</title>
		<link>http://www.networktoday.org/sql-server-how-to-rename-a-column-name-or-table-name/</link>
		<comments>http://www.networktoday.org/sql-server-how-to-rename-a-column-name-or-table-name/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 10:51:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[sp_RENAME]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL SERVER]]></category>
		<category><![CDATA[SQL SERVER Name Table]]></category>
		<category><![CDATA[SQL SERVER Rename Column]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=362</guid>
		<description><![CDATA[I often get request from blog reader for T-SQL script to rename database table column name or rename table itself. The script for renaming any column : sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN' The script for renaming any object (table, sp etc) : sp_RENAME '[OldTableName]' , '[NewTableName]' This article demonstrates two examples of renaming database object. [...]]]></description>
			<content:encoded><![CDATA[<p>I often get request from blog reader for T-SQL script to rename database table column name or rename table itself.</p>
<p>The script for renaming any column :<br />
<code>sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'</code></p>
<p><code></code> The script for renaming any object (table, sp etc) :<br />
<code>sp_RENAME '[OldTableName]' , '[NewTableName]'</code></p>
<p>This article demonstrates two examples of renaming database object.</p>
<ol>
<li>Renaming database table column to new name.</li>
<li>Renaming database table to new name.</li>
</ol>
<p>In both the cases we will first see existing table. Rename the object. Test object again with new name.</p>
<p><strong>1. Renaming database table column to new name.</strong></p>
<p>Example uses AdventureWorks database. A small table with name “<strong>Table_ First</strong>” is created. Table has two fields ID and Name.</p>
<p><img src="http://www.pinaldave.com/bimg/spRename1.gif" alt="" width="493" height="421" /></p>
<p>Now, to change the Column Name from “Name” to “NameChange” we can use command:</p>
<p><code>USE AdventureWorks<br />
GO<br />
sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN'<br />
GO </code></p>
<p>Following Fig. show use of SP_RENAME Command</p>
<p><img src="http://www.pinaldave.com/bimg/spRename2.gif" alt="" width="493" height="215" /></p>
<p>You can see the column name “Name” is now changed to “NameChange”.</p>
<p><code>USE AdventureWorks<br />
GO<br />
SELECT *<br />
FROM Table_First<br />
GO</code></p>
<p>Following fig. verify that the column name has been changed.</p>
<p><img src="http://www.pinaldave.com/bimg/spRename3.gif" alt="" width="493" height="255" /> <strong></strong></p>
<p><strong>2.Renaming database table to new name.</strong></p>
<p>We can change the table name too with the same command.</p>
<p><code>sp_RENAME 'Table_First', 'Table_Last'<br />
GO</code></p>
<p>Following fig. Shows how we can change Table Name.</p>
<p><img src="http://www.pinaldave.com/bimg/spRename4.gif" alt="" width="493" height="243" /></p>
<p>Now, the table name “Table_First” is renamed as “Table_Last”.</p>
<p>“Table_First” will no longer be available in database. We can verify this by running script:</p>
<p><code>USE AdventureWorks<br />
GO<br />
SELECT *<br />
FROM Table_First<br />
GO<br />
</code></p>
<p>The Messages shows an error “Invalid object name ‘Table_First’.”</p>
<p>To check that the new renamed table exist in database run script:</p>
<p><code>USE AdventureWorks<br />
GO<br />
SELECT *<br />
FROM Table_Last<br />
GO</code></p>
<p><img src="http://www.pinaldave.com/bimg/spRename6.gif" alt="" width="490" height="269" /></p>
<p>You can see the same data now available in new table named “Table_Last”</p>
<p>Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/sql-server-how-to-rename-a-column-name-or-table-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server</title>
		<link>http://www.networktoday.org/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/</link>
		<comments>http://www.networktoday.org/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 03:37:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[bulk insert csv error]]></category>
		<category><![CDATA[Msg 4861]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL SERVER]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=358</guid>
		<description><![CDATA[bulk insert csv sql server Server: Msg 4861, Level 16, State 1, Line 1 This is very common request recently – How to import CSV file into SQL Server? How to load CSV file into SQL Server Database Table? How to load comma delimited file into SQL Server? Let us see the solution in quick [...]]]></description>
			<content:encoded><![CDATA[<p>bulk insert csv sql server Server: Msg 4861, Level 16, State 1, Line 1</p>
<p>This is very common request recently – How to import CSV file into SQL Server? How to load CSV file into SQL Server Database Table? How to load comma delimited file into SQL Server? Let us see the solution in quick steps.</p>
<p>CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values.</p>
<p>Create TestTable</p>
<p><code>USE TestData<br />
GO<br />
CREATE TABLE CSVTest<br />
(ID INT,<br />
FirstName VARCHAR(40),<br />
LastName VARCHAR(40),<br />
BirthDate SMALLDATETIME)<br />
GO</code></p>
<p>Create CSV file in drive C: with name csvtest.txt with following content. The location of the file is C:\csvtest.txt</p>
<p>1,James,Smith,19750101</p>
<p>2,Meggie,Smith,19790122</p>
<p>3,Robert,Smith,20071101</p>
<p>4,Alex,Smith,20040202</p>
<p><img src="http://www.pinaldave.com/bimg/csv1.gif" alt="" width="362" height="231" /></p>
<p>Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.</p>
<p><code>BULK<br />
INSERT CSVTest<br />
FROM 'c:\csvtest.txt'<br />
WITH<br />
(<br />
FIELDTERMINATOR = ',',<br />
ROWTERMINATOR = '\n'<br />
)<br />
GO<br />
--Check the content of the table.<br />
SELECT *<br />
FROM CSVTest<br />
GO<br />
--Drop the table to clean up database.<br />
SELECT *<br />
FROM CSVTest<br />
GO</code></p>
<p><img src="http://www.pinaldave.com/bimg/csv2.gif" alt="" width="352" height="624" /></p>
<p>Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Alter a Replicated Article in SQL Server 2005</title>
		<link>http://www.networktoday.org/how-to-alter-a-replicated-article-in-sql-server-2005/</link>
		<comments>http://www.networktoday.org/how-to-alter-a-replicated-article-in-sql-server-2005/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 11:27:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[add a column on a Replicated Table]]></category>
		<category><![CDATA[alter a column on a Replicated Table]]></category>
		<category><![CDATA[Alter a Replicated Article]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=353</guid>
		<description><![CDATA[Introduction One of the fairly frequently asked questions on the Microsoft replication discussion boards concerns how to alter a replicated article. For simple cases this is quite straightforward &#8211; adding a column is achieved using sp_repladdcolumn, while sp_repldropcolumn is used to drop a column. However, what if we want to change an existing column &#8211; [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>One of the fairly frequently asked questions on the Microsoft replication discussion boards concerns how to alter a replicated article. For simple cases this is quite straightforward &#8211; adding a column is achieved using <em>sp_repladdcolumn</em>, while <em>sp_repldropcolumn</em> is used to drop a column. However, what if we want to <em> change</em> <em> an existing column</em> &#8211; e.g. change the Fname varchar(50) column below to Fname varchar(100)?</p>
<p align="center"><img src="http://www.replicationanswers.com/graphics/table.JPG" alt="" border="0" /></p>
<p><a href="http://www.replicationanswers.com/AddColumn.asp"> Another article</a> I&#8217;ve written illustrates how this can be achieved in SQL Server 2000; the article lists 2 alternative methods, both of which are rather &#8216;clunky&#8217; and result in a huge amount of transactions being replicated. While the article illustrates a useable workaround for DDL changes to replicated tables, there is no correspondingly obvious way of reactively replicating a change to a replicated view or other programming object. So, if we publish a view, when the view definition is changed on the publisher, the new definition may be subsequently pumped to the subscribers using <em>sp_addscriptexec</em>, reinitializing or manually connecting to the subscriber and issuing an &#8216;alter view&#8217; statement. However, the problem is that there is no way in SQL 2000 to automatically pick up the view definition changes, and the onus lies on the constant diligence of the DBA to prevent any non-synchronization between the view definition on the publisher and its subscribers. The new DDL replication features now available in SQL Server 2005 may be used to overcome these and other issues and this current article explains how to take advantage of this functionality.</p>
<h2>Implementation</h2>
<p>In SQL Server 2005 replication, DDL changes may be replicated for tables, views, stored procedures, functions and triggers. To take advantage of this new methodology is straightforward. For example, adding a column is now done using &#8216;ALTER TABLE&#8217; syntax eg</p>
<p align="center"><em>ALTER TABLE tPersonnel ADD Salary INT NULL</em></p>
<p>Note that the column above is nullable and that this is a requirement &#8211; either the new column is nullable or has a default constraint.</p>
<p>During synchronization of the distribution/merge agent, this change is replicated to the subscriber as the same &#8216;ALTER TABLE&#8217; statement, along with the requisite stored procedure changes (transactional) and metadata/trigger changes (merge). These DDL changes must always be made at the publisher and never at the subscriber, and this also applies to republishing scenarios, where the schema change will be automatically propagated from the publisher to the publisher-subscriber and finally to the subscriber.</p>
<p>Note that the SQL Management Studio graphical interface may be used to directly add or drop a column to the replicated table, but unfortunately, altering a column is disallowed. On changing an existing column, the GUI chooses to drop and recreate the table behind the scenes, resulting in the error message below:</p>
<p align="center"><img src="http://www.replicationanswers.com/graphics/AlterSchema/ErrorMessage.JPG" alt="" border="0" /></p>
<p>(In case you&#8217;re wondering, the &#8216;save text file&#8217; button refers to the error message text rather than the necessary TSQL script to run). This is followed by a standard &#8216;User cancelled out of save dialog&#8217; message. So, to make the DDL change mentioned initially, we open a query window and run</p>
<p align="center"><em>ALTER TABLE tPersonnel ALTER COLUMN FName VARCHAR(100) NULL</em></p>
<p>and the changes are replicated as painlessly as when we add a column.</p>
<p>Which DDL changes may now be replicated? Basically almost all aspects of the &#8216;ALTER TABLE&#8217; syntax are supported. Adding / dropping / changing a column comes in to this category but remember that index creation uses &#8216;CREATE INDEX&#8217; so isn&#8217;t included and we&#8217;ll still need to use <em>sp_addscriptexec</em> for this. The exceptions are:</p>
<p>(a) as you would expect for transactional replication, altering a primary key is disallowed and results in an error (&#8216;Msg 4929, Level 16, State 1, Line 1 Cannot alter the table &#8216;tPersonnel&#8217; because it is being published for replication. Msg 3727, Level 16, State 0, Line 1 Could not drop constraint. See previous errors.&#8217;)</p>
<p>(b) although it doesn&#8217;t result in an error, adding an identity column to a published table is not supported, because it can result in non-convergence when the column is replicated to the Subscriber. Altering or dropping identity columns that are managed by replication is also not supported.</p>
<p>The SQL Server 2000 procedure <em>sp_repladdcolumn</em> is still available but is deprecated and is only recommended for exceptional circumstances, namely SQL Server 2000 publishers,  SQL Server 2000 republishing subscribers and SQL Server 2000 merge subscribers.</p>
<h2>Enabling / Disabling</h2>
<p>To control DDL replication, there is a publication property on the &#8216;Subscription Options&#8217; section (see below). It can be enabled or disabled on the fly without causing a required reinitialization, and by default is enabled. Once disabled, schema changes are still possible and are not replicated. To achieve the same end in TSQL, use <em>sp_addpublication</em> / <em>sp_addmergepublication</em> / <em>sp_changepublication</em> / <em>sp_changemergepublication</em>  specifying a value of 0 for the parameter @replicate_ddl.</p>
<p align="center"><img src="http://www.replicationanswers.com/graphics/altertable.JPG" alt="" width="578" height="508" border="0" /></p>
<p>The one exception to consider is that &#8216;ALTER TABLE DROP COLUMN&#8217; is always replicated to all subscriptions which replicate the column, regardless of the value of the @replicate_ddl parameter.</p>
<h2>Working with Different Versions of SQL Server</h2>
<p>How does all this tie in if you have multiple versions of SQL Server? The most common combinations will involve a SQL Server 2005 Pub\Dist and a SQL Server 2000 Subscriber and vice versa. The table below shows the resulting behaviour if we add a column:</p>
<table width="86%" border="1" cellspacing="0" align="center">
<tbody>
<tr>
<td align="left" width="28%"><strong>Publisher\Distributor</strong></td>
<td align="left" width="20%"><strong>Subscriber</strong></td>
<td align="left" width="102%"><strong>Behaviour</strong></td>
</tr>
<tr>
<td align="left" width="28%">SQL 2000</td>
<td align="left" width="20%">SQL 2005</td>
<td align="left" width="102%">Alter Table does nothing to the subscriber while <em>sp_repladdcolumn</em> will add the column (both same as 2000 -&gt; 2000)</td>
</tr>
<tr>
<td align="left" width="28%">SQL 2005</td>
<td align="left" width="20%">SQL 2000</td>
<td align="left" width="102%">Alter Table replicates the change in transactional but not merge (as mentioned above, we still use <em>sp_repladdcolumn</em>  and <em>sp_repldropcolumn</em> for these cases)</td>
</tr>
</tbody>
</table>
<h2>Finally &#8211; Error-Handling</h2>
<p>According to BOL setting @replicate_ddl to 0 is the recommended method of making certain changes to avoid errors and enforced reinitialization, with the example being the addition of a foreign key to a publication table when the related table doesn&#8217;t exist on the subscriber. As far as I can tell this BOL example is obsolete as far as Transactional replication is concerned, as the code used to create the FK constraint (stored in the <em>MSreplCommands</em> table) is prefixed with an &#8216;IF EXISTS&#8217; clause which looks for the related table, thus preventing this problem from occurring. The -SkipErrors method would be the first choice to try if DDL replication causes an error, but so far I haven&#8217;t found a way of testing this as I can&#8217;t initiate the error itself <img src='http://www.networktoday.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . However, in the case of merge replication the schema change command stored in <em>sysmergeschemachange</em> has no such &#8216;IF EXISTS&#8217; check so this can indeed result in the error mentioned in BOL. In this case there is a &#8216;get out of jail&#8217; card you can play if you are ever in this situation &#8211; the stored procedure <em>sp_markpendingschemachange</em> (in conjunction with <em>sp_enumeratependingschemachanges</em>) will mark schema changes to be skipped so synchronization can continue.</p>
<p>&nbsp;</p>
<p>Reference &#8211; http://www.replicationanswers.com</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/how-to-alter-a-replicated-article-in-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Altering a column on a Replicated Table</title>
		<link>http://www.networktoday.org/altering-a-column-on-a-replicated-table/</link>
		<comments>http://www.networktoday.org/altering-a-column-on-a-replicated-table/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 11:01:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[Alter Replicated Table]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=349</guid>
		<description><![CDATA[Sometimes the schema of a replicated table needs altering. There are many reasons this might be the case eg possibly the datatype has been incorrectly chosen, or a default is missing, or we want to rename a column. Attempting to change the table schema directly will result in the error &#8220;Cannot alter/drop the table &#8216;tablename&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes the schema of a replicated table needs altering. There are many reasons this might be the case eg possibly the datatype has been incorrectly chosen, or a default is missing, or we want to rename a column. Attempting to change the table schema directly will result in the error</p>
<p><em> &#8220;Cannot alter/drop the table &#8216;tablename&#8217; because it is being published for replication&#8221;.</em></p>
<p>So, how to change an existing column without breaking replication? Consider if we wanted to make the following schema change:</p>
<p><img src="http://www.sqlservercentral.com/articles/Replication/alteringacolumnonareplicatedtable/1666/BeforeTable.GIF" alt="" width="212" height="97" border="0" /> to <img src="http://www.sqlservercentral.com/articles/Replication/alteringacolumnonareplicatedtable/1666/AfterTable.GIF" alt="" width="216" height="99" border="0" /></p>
<p align="left">The method we choose depends in part on the replication type and size of the table, but there are 2 main options:</p>
<h3>(a) altering the subscriptions</h3>
<pre>  exec sp_dropsubscription   @publication =  'tTestFNames' 
     ,  @article =  'tEmployees' 
     ,  @subscriber =  'RSCOMPUTER'
     ,  @destination_db =  'testrep' 

  exec sp_droparticle  @publication =  'tTestFNames'
     ,  @article =  'tEmployees'

  alter table tEmployees alter column Forename varchar(100) null

  exec sp_addarticle  @publication =  'tTestFNames' 
     ,  @article =  'tEmployees' 
     ,  @source_table =  'tEmployees' 

  exec sp_addsubscription  @publication =  'tTestFNames'
     ,  @article =  'tEmployees'
     ,  @subscriber =  'RSCOMPUTER' 
     ,  @destination_db =  'testrep'</pre>
<p>For <strong> snapshot replication </strong> this is the obvious choice. We drop the subscription to this article, drop the article, then change the table. Afterwards the process is reversed. The next time the snapshot agent is run, it&#8217;ll pick up the new schema without any issues.</p>
<p>For <strong> transactional replication</strong> we may choose to proceed using the script above. However, we must be more careful in this case. By default, an insert, update or delete statement performed on the publisher is propagated to the subscriber in the form of a stored procedure call. By changing the column definition, we may need to change the related stored procedures on all the subscribers. Addition of a default would be fine, but changing the datatype itself as above would require the stored procedure arguments to be modified. For the example table above, these 3 procedures exist on the subscriber in the form:</p>
<p>sp_MSins_tEmployees,<br />
sp_MSupd_tEmployees,<br />
sp_MSdel_tEmployees.</p>
<p>They can be generated at the publisher using sp_scriptpublicationcustomprocs but this would of course require the system to be quiesced, i.e. during this (quick) change there shouldn&#8217;t be any alterations made to the publisher&#8217;s data and all the subscribers should be completely synchronized.</p>
<p>This is not ideal, and there is also a hidden problem here waiting to be discovered. Usually, when you add a new article to an existing publication in transactional replication, running the snapshot agent will create a snapshot of just the new article. In our case, it&#8217;ll also create a snapshot of the &#8216;tEmployees&#8217; table. So, to avoid all the issues and complications mentioned above, it&#8217;s simplest to run the snapshot agent immediately after executing sp_addsubscription and then synchronize.</p>
<p>In <strong> merge replication</strong>, there is no possibility of dropping the subscription on a per article basis using the script above, as there is in transactional and snapshot replication. If we drop the subscription entirely including all other articles (sp_dropmergesubscription), then try to run sp_dropmergearticle there will be an error if the snapshot has already been run, so we have to set @forceinvalid_snapshot to 1, make the table change on the publisher then read the article and subscriptions and initialize which would necessitate a new snapshot generation of <em> all</em> articles in this publication. A nosync initialization is possible, but this can be extremely restrictive for future changes, and I&#8217;ll leave that for another article.</p>
<h3>(b) altering the table in-place</h3>
<p>OK, in some cases the table is large and we don&#8217;t want to run a new snapshot &#8211; either of the individual table (transactional) or of the whole publication (merge) &#8211; so there is an alternative method. We might use the built in stored procedures sp_repladdcolumn and sp_repldropcolumn to make the changes (note that these procedures limit the subscribers to be SQL Server 2000 only). Using these procedures we can add a dummy column to hold the data, remove the old column, add in the correct definition of the original column then transfer back the data. Now the script becomes:</p>
<pre>  exec sp_repladdcolumn  @source_object =  'tEmployees'
     ,  @column =  'TempForename' 
     ,  @typetext =  'varchar(100) NULL' 
     ,  @publication_to_add =  'tTestFNames' 

  update tEmployees set TempForename = Forename

  exec sp_repldropcolumn  @source_object =  'tEmployees' 
     ,  @column =  'Forename' 

  exec sp_repladdcolumn  @source_object =  'tEmployees'
     ,  @column =  'Forename' 
     ,  @typetext =  'varchar(100) NULL' 
     ,  @publication_to_add =  'tTestFNames' 

  update tEmployees set Forename = TempForename

  exec sp_repldropcolumn  @source_object =  'tEmployees' 
     ,  @column =  'TempForename'</pre>
<p>Although the above script can be used for transactional replication or merge replication, the internal methodology is different due to the differing nature of these 2 techniques. For merge replication, details of the rows updated are kept in MSmerge_contents, and if a particular row has been changed once or a hundred times, there will still only be one entry in this system table, while in transactional replication, 100 updates to a row is propagated as 100 subscriber updates. This means merge has an advantage over transactional because we need to perform 2 updates to each row to make the schema change.</p>
<p>Paul Ibison, SQL Server MVP, 2005, <a href="http://ssisblog.replicationanswers.com/">http://ssisblog.replicationanswers.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/altering-a-column-on-a-replicated-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server – Creating and using Dump Devices for Backups</title>
		<link>http://www.networktoday.org/sql-server-creating-and-using-dump-devices-for-backups/</link>
		<comments>http://www.networktoday.org/sql-server-creating-and-using-dump-devices-for-backups/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 10:55:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[Backup & Recovery]]></category>
		<category><![CDATA[Catalog Views]]></category>
		<category><![CDATA[Management Studio]]></category>
		<category><![CDATA[SQLServer]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=346</guid>
		<description><![CDATA[A dump device is simply a logical device which redirects to a specified physical device. The main advantage of using a dump device for backups is it reduces the change required to backup code. For example, if you are backing up to a Network location. If you have hard-coded the backup location then the network [...]]]></description>
			<content:encoded><![CDATA[<p>A dump device is simply a logical device which redirects to a specified physical device. The main advantage of using a dump device for backups is it reduces the change required to backup code. For example, if you are backing up to a Network location. If you have hard-coded the backup location then the network location change will require a code change as well.</p>
<p>&nbsp;</p>
<p>You can create a dump device using SSMS or T-SQL.</p>
<p><strong><span style="text-decoration: underline;">To create a dump device using SSMS:</span></strong></p>
<p>1. Expand &#8220;<strong>Server Objects</strong>&#8221; in<strong> Object Explorer</strong>,</p>
<p>2. Right Click on &#8220;<strong>Backup Devices</strong>&#8221; and choose &#8220;<strong>New Backup Device</strong>&#8221;</p>
<p>&nbsp;</p>
<p>3. Provide a logical Device name and the physical backup file location for dump device</p>
<p>&nbsp;</p>
<p>4. Click &#8220;<strong>OK</strong>&#8221; to create the dump device.</p>
<p><strong></strong></p>
<p><strong><span style="text-decoration: underline;">To create a dump device using T-SQL:</span></strong></p>
<p>To create a dump device using T-SQL you can use system stored procedure <strong>sp_addumpdevice.</strong></p>
<p>USE [master]</p>
<p>GO</p>
<p>EXEC master.dbo.sp_addumpdevice</p>
<p>@devtype          = N&#8217;disk&#8217;,</p>
<p>@logicalname      = N&#8217;MyBackups&#8217;,</p>
<p>@physicalname     = N&#8217;C:\Database\Backup\BackupDisk.bak&#8217;</p>
<p>GO</p>
<p><span style="text-decoration: underline;">Result Set:</span></p>
<p>(1 row(s) affected)</p>
<p>&nbsp;</p>
<p><strong><span style="text-decoration: underline;">View all dump devices:</span></strong></p>
<p>You can get the list of existing dump devices on server using <strong>sys.backup_devices</strong> catalog view:</p>
<p>SELECT      *</p>
<p>FROM        sys.backup_devices</p>
<p>GO</p>
<p>Result Set:</p>
<p><strong>Name          type   type_desc     physical_name</strong></p>
<p><strong>———–   —–  ———–   ——————————–</strong></p>
<p>MyBackups     2      DISK          C:\Database\Backup\BackupDisk.bak</p>
<p>&nbsp;</p>
<p>(1 row(s) affected)</p>
<p>&nbsp;</p>
<p><strong><span style="text-decoration: underline;">Using the dump device for BACKUP:</span></strong></p>
<p>Once the dump device is created you can use it to store backups, The dump device will be listed in &#8220;<strong>Select Backup Destination</strong>&#8221; dialog box:</p>
<p>&nbsp;</p>
<p>You can use it in T-SQL as below:</p>
<p>/*</p>
<p>BACKUP DATABASE [SqlAndMe]</p>
<p>TO DISK = N&#8217;C:\Database\Backup\SqlAndMeBackup.bak&#8217;</p>
<p>WITH NOFORMAT, NOINIT, NAME = N&#8217;SqlAndMe – Full Backup&#8217;,</p>
<p>SKIP, NOREWIND, NOUNLOAD,  STATS = 10</p>
<p>GO</p>
<p>*/</p>
<p>&nbsp;</p>
<p>BACKUP DATABASE [SqlAndMe]</p>
<p>TO <strong>[MyBackups]</strong></p>
<p>WITH NOFORMAT, NOINIT, NAME = N&#8217;SqlAndMe – Full Backup&#8217;,</p>
<p>SKIP, NOREWIND, NOUNLOAD,  STATS = 10</p>
<p>GO</p>
<p><span style="text-decoration: underline;">Result Set:</span></p>
<p>10 percent processed.</p>
<p>20 percent processed.</p>
<p>30 percent processed.</p>
<p>40 percent processed.</p>
<p>50 percent processed.</p>
<p>60 percent processed.</p>
<p>70 percent processed.</p>
<p>80 percent processed.</p>
<p>90 percent processed.</p>
<p>Processed 44680 pages for database &#8216;SqlAndMe&#8217;, file &#8216;SqlAndMe&#8217; on file 2.</p>
<p>100 percent processed.</p>
<p>Processed 1 pages for database &#8216;SqlAndMe&#8217;, file &#8216;SqlAndMe_log&#8217; on file 2.</p>
<p>BACKUP DATABASE successfully processed 44681 pages in 23.542 seconds (14.827 MB/sec).</p>
<p>&nbsp;</p>
<p><strong>Changing Physical File location for dump devices:</strong></p>
<p>To change the physical file location for dump device, you need to drop and recreate the dump device. If you need to move the current physical file to a new location, it can be done using Windows Explorer or any other File Manager. The physical file is not locked unless a BACKUP/RESTORE is in progress.</p>
<p>USE [master]</p>
<p>GO</p>
<p>EXEC master.dbo.sp_dropdevice</p>
<p>@logicalname = N&#8217;MyBackups&#8217;</p>
<p>GO</p>
<p>&nbsp;</p>
<p>EXEC master.dbo.sp_addumpdevice</p>
<p>@devtype          = N&#8217;disk&#8217;,</p>
<p>@logicalname      = N&#8217;MyBackups&#8217;,</p>
<p>@physicalname     = N&#8217;C:\NewLocation\BackupDisk.bak&#8217;</p>
<p>GO</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>SELECT      *</p>
<p>FROM        sys.backup_devices</p>
<p>GO</p>
<p><span style="text-decoration: underline;">Result Set:</span></p>
<p>Device dropped.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Name          type   type_desc     physical_name</p>
<p>————  —— ————  ———————–</p>
<p>MyBackups     2      DISK          C:\NewLocation\BackupDisk.bak</p>
<p>&nbsp;</p>
<p>(1 row(s) affected)</p>
<p>&nbsp;</p>
<p>Hope This Helps! Cheers!</p>
<p><strong>Reference :</strong> Vishal (<a href="http://sqlandme.com/">http://SqlAndMe.com</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/sql-server-creating-and-using-dump-devices-for-backups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server – Row count for all views / tables</title>
		<link>http://www.networktoday.org/sql-server-row-count-for-all-views-tables/</link>
		<comments>http://www.networktoday.org/sql-server-row-count-for-all-views-tables/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 10:50:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[Row count for all tables]]></category>
		<category><![CDATA[Row count for all views]]></category>
		<category><![CDATA[SQL Server count all]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=343</guid>
		<description><![CDATA[Getting row count for all tables in a database is straight forward. You can display row count for all tables by joining sys.objects and sys.partitions as below: USE   [AdventureWorks2008R2] GO &#160; SELECT      SCHEMA_NAME(A.schema_id) + &#8216;.&#8217; + A.Name, SUM(B.rows) AS &#8216;RowCount&#8217; FROM        sys.objects A INNER JOIN sys.partitions B ON A.object_id = B.object_id WHERE       A.type [...]]]></description>
			<content:encoded><![CDATA[<p>Getting row count for all tables in a database is straight forward. You can display row count for all tables by joining sys.objects and sys.partitions as below:</p>
<p>USE   [AdventureWorks2008R2]</p>
<p>GO</p>
<p>&nbsp;</p>
<p>SELECT      SCHEMA_NAME(A.schema_id) + &#8216;.&#8217; +</p>
<p>A.Name, SUM(B.rows) AS &#8216;RowCount&#8217;</p>
<p>FROM        sys.objects A</p>
<p>INNER JOIN sys.partitions B ON A.object_id = B.object_id</p>
<p>WHERE       A.type = &#8216;U&#8217;</p>
<p>GROUP BY    A.schema_id, A.Name</p>
<p>GO</p>
<p><span style="text-decoration: underline;">Result Set:</span></p>
<p>Person.Address                    78456</p>
<p>Person.AddressType                18</p>
<p>dbo.AWBuildVersion                1</p>
<p>dbo.BCPTest                       5</p>
<p>Production.BillOfMaterials        8037</p>
<p>Person.BusinessEntity             41554</p>
<p>Person.BusinessEntityAddress      78456<br />
…</p>
<p>However, for views row count is not available in sys.partitions. To get the row count for a view; you must query the view itself.</p>
<p>USE   [AdventureWorks2008R2]</p>
<p>GO</p>
<p>&nbsp;</p>
<p>SELECT COUNT(*) FROM HumanResources.vEmployee</p>
<p>GO</p>
<p><span style="text-decoration: underline;">Result Set:</span></p>
<p>290</p>
<p>&nbsp;</p>
<p>(1 row(s) affected)</p>
<p>This can be encapsulated in a stored procedure to query all available views and then display the result set. The procedure can be created as:</p>
<p>USE   [AdventureWorks2008R2]</p>
<p>GO</p>
<p>&nbsp;</p>
<p>CREATE PROCEDURE dbo.ViewsRowCount</p>
<p>AS</p>
<p>BEGIN<br />
SET NOCOUNT ON</p>
<p>CREATE TABLE #tempRowCount</p>
<p>(</p>
<p>Name        VARCHAR(100),</p>
<p>Row_Count   INT</p>
<p>)</p>
<p>&nbsp;</p>
<p>DECLARE     @SQL VARCHAR(MAX)</p>
<p>SET         @SQL = &#8221;</p>
<p>SELECT @SQL = @SQL + &#8216;INSERT INTO #tempRowCount SELECT &#8221;&#8217; +</p>
<p>SCHEMA_NAME(schema_id) + &#8216;.&#8217; + name + &#8221;&#8217;, COUNT(*) FROM &#8216; +</p>
<p>SCHEMA_NAME(schema_id) + &#8216;.&#8217; + name +</p>
<p>CHAR(13) FROM sys.objects WHERE type = &#8216;V&#8217;</p>
<p>EXEC (@SQL)</p>
<p>&nbsp;</p>
<p>SELECT      Name, Row_Count</p>
<p>FROM        #tempRowCount</p>
<p>END</p>
<p>GO</p>
<p>Once created this stored procedure returns row count for all views in database as bellow:</p>
<p>USE   [AdventureWorks2008R2]</p>
<p>GO</p>
<p>&nbsp;</p>
<p>EXEC  dbo.ViewsRowCount</p>
<p>GO</p>
<p><span style="text-decoration: underline;">Result Set:</span></p>
<p><strong><span style="text-decoration: underline;">Name</span>                           <span style="text-decoration: underline;">Row_Count</span></strong></p>
<p>dbo.vApplicationSpecialists    3</p>
<p>Person.vAdditionalContactInfo  10</p>
<p>HumanResources.vEmployee       290</p>
<p>Sales.vIndividualCustomer      18508</p>
<p>Sales.vPersonDemographics      19972</p>
<p>HumanResources.vJobCandidate   13</p>
<p>…</p>
<p>&nbsp;</p>
<p>Reference &#8211; http://sqlandme.com</p>
<p>http://sqlandme.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/sql-server-row-count-for-all-views-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Attach a single MDF file – Database</title>
		<link>http://www.networktoday.org/attach-a-single-mdf-file-database/</link>
		<comments>http://www.networktoday.org/attach-a-single-mdf-file-database/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 10:41:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[Backup & Recovery]]></category>
		<category><![CDATA[Management Studio]]></category>
		<category><![CDATA[single MDF]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[SQLServer 2005]]></category>
		<category><![CDATA[SQLServer 2008]]></category>
		<category><![CDATA[SQLServer 2008 R2]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=340</guid>
		<description><![CDATA[If you want to attach a database to a SQL Instance you need both the data file (.mdf) and the log file (.ldf). Without both files you cannot attach a database. Let’s try doing that… To attach a database from Management Studio: Right click on Database node and choose attach: &#160; This will open the [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to attach a database to a SQL Instance you need both the data file (.mdf) and the log file (.ldf). Without both files you cannot attach a database. Let’s try doing that…</p>
<p>To attach a database from Management Studio:</p>
<p>Right click on Database node and choose attach:</p>
<p>&nbsp;</p>
<p>This will open the “Attach Databases” dialogue box, where you can select the .mdf file using “Add…” button:</p>
<p>&nbsp;</p>
<p>Note, that is says “Not Found” for log file, if you click on “OK”, it will raise an error:</p>
<p>&nbsp;</p>
<p>The detailed error message can be seen by click on “Message” link:</p>
<p>&nbsp;</p>
<p>Moral of the story, you can’t attach single .mdf  using GUI. You need to use T-SQL to achieve this:</p>
<p>– © 2011 – Vishal (http://SqlAndMe.com)</p>
<p>&nbsp;</p>
<p>CREATE DATABASE [TestDB]</p>
<p>ON (FILENAME = &#8216;C:\123\TestDB.mdf&#8217;)</p>
<p>FOR ATTACH_REBUILD_LOG ;</p>
<p>&nbsp;</p>
<p>On successful completion, you will get the below message:</p>
<p>File activation failure. The physical file name “C:\Program Files\Microsoft SQL Server\MSSQL11.DENALI\MSSQL\DATA\TestDB_1.ldf” may be incorrect.</p>
<p>New log file &#8216;C:\123\TestDB_log.ldf&#8217; was created.</p>
<p>Hope This Helps! Cheers!</p>
<p><strong>Reference :</strong> Vishal (http://SqlAndMe.com)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/attach-a-single-mdf-file-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL SERVER – Attach mdf file without ldf file in Database</title>
		<link>http://www.networktoday.org/sql-server-attach-mdf-file-without-ldf-file-in-database/</link>
		<comments>http://www.networktoday.org/sql-server-attach-mdf-file-without-ldf-file-in-database/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 10:37:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[mdf single file]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Backup and Restore]]></category>
		<category><![CDATA[SQL Data Storage]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL SERVER]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.networktoday.org/?p=336</guid>
		<description><![CDATA[Background Story: One of my friends recently called up and asked me if I had spare time to look at his database and give him a performance tuning advice. Because I had some free time to help him out, I said yes. I asked him to send me the details of his database structure and [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Background Story:</strong><br />
One of my friends recently called up and asked me if I had spare time to look at his database and give him a performance tuning advice. Because I had some free time to help him out, I said yes. I asked him to send me the details of his database structure and sample data. He said that since his database is in a very early stage and is small as of the moment, so he told me that he would like me to have a complete database. My response to him was “Sure! In that case, take a backup of the database and send it to me. I will restore it into my computer and play with it.”</p>
<p>He did send me his database; however, his method made me write this quick note here. Instead of taking a full backup of the database and sending it to me, he sent me only the .mdf (primary database file). In fact, I asked for a complete backup (I wanted to review file groups, files, as well as few other details).  Upon calling my friend,  I found that he was not available. Now,  he left me with only a .mdf file. As I had some extra time, I decided to checkout his database structure and get back to him regarding the full backup, whenever I can get in touch with him again.</p>
<p><img title="image" src="http://sqlandme.files.wordpress.com/2011/04/image_thumb36.png?w=314&amp;h=113" alt="image" width="314" height="113" border="0" /></p>
<p><strong>Technical Talk: </strong><br />
If the database is shutdown gracefully and there was no abrupt shutdown (power outrages, pulling plugs to machines, machine crashes or any other reasons), it is <em>possible </em>(there’s no guarantee) to attach .mdf file only to the server. Please note that there can be many more reasons for a database that is not getting attached or restored. In my case, the database had a clean shutdown and there were no complex issues. I was able to recreate a transaction log file and attached the received .mdf file.</p>
<p>There are multiple ways of doing this. I am listing all of them here. Before using any of them, please consult the Domain Expert in your company or industry. Also, never attempt this on live/production server without the presence of a Disaster Recovery expert.<strong> </strong></p>
<p><code>USE [master]<br />
GO<br />
-- Method 1: I use this method<br />
EXEC sp_attach_single_file_db @dbname='TestDb',<br />
@physname=N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDb.mdf'<br />
GO<br />
-- Method 2:<br />
CREATE DATABASE TestDb ON<br />
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDb.mdf')<br />
FOR ATTACH_REBUILD_LOG<br />
GO</code></p>
<p>Method 2: If one or more log files are missing, they are recreated again.</p>
<p>There is one more method which I am demonstrating here but<em> I</em> <em>have not used myself before</em>. According to Book Online, it will work only if there is one log file that is missing. If there are more than one log files involved, all of them are required to undergo the same procedure.</p>
<p><code>-- Method 3:<br />
CREATE DATABASE TestDb ON<br />
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDb.mdf')<br />
FOR ATTACH<br />
GO<br />
</code></p>
<p>&nbsp;</p>
<p>Reference: <strong>Pinal Dave (</strong><strong>http://blog.SQLAuthority.com</strong><strong>)</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.networktoday.org/sql-server-attach-mdf-file-without-ldf-file-in-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

