<?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>sql queries &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/sql-queries/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Wed, 23 May 2018 19:10:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Server: Basic performance test between SELECT INTO and INSERT INTO SELECT</title>
		<link>https://www.dbrnd.com/2018/07/sql-server-basic-performance-test-between-select-into-and-insert-into-select/</link>
					<comments>https://www.dbrnd.com/2018/07/sql-server-basic-performance-test-between-select-into-and-insert-into-select/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 23 Jul 2018 16:16:07 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[INSERT INTO SELECT]]></category>
		<category><![CDATA[SELECT INTO]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[sqlcode]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7656</guid>

					<description><![CDATA[<div><img width="679" height="679" src="https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-679x679.jpg" class="attachment-post-image size-post-image wp-post-image" alt="" decoding="async" fetchpriority="high" srcset="https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-679x679.jpg 679w, https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-150x150.jpg 150w, https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-300x300.jpg 300w, https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-600x600.jpg 600w, https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad.jpg 800w" sizes="(max-width: 679px) 100vw, 679px" /></div>In this post, I am doing a basic performance test between SELECT INTO and INSERT INTO SELECT option of SQL Server. Database Developers are using these options very frequently for copying data between the tables. But SELECT INTO is little faster than INSERT INTO SELECT option. Note: I am testing using a very small number [&#8230;]]]></description>
										<content:encoded><![CDATA[<div><img width="679" height="679" src="https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-679x679.jpg" class="attachment-post-image size-post-image wp-post-image" alt="" decoding="async" srcset="https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-679x679.jpg 679w, https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-150x150.jpg 150w, https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-300x300.jpg 300w, https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad-600x600.jpg 600w, https://www.dbrnd.com/wp-content/uploads/2018/05/Good-vs-Bad.jpg 800w" sizes="(max-width: 679px) 100vw, 679px" /></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F07%2Fsql-server-basic-performance-test-between-select-into-and-insert-into-select%2F&amp;linkname=SQL%20Server%3A%20Basic%20performance%20test%20between%20SELECT%20INTO%20and%20INSERT%20INTO%20SELECT" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F07%2Fsql-server-basic-performance-test-between-select-into-and-insert-into-select%2F&amp;linkname=SQL%20Server%3A%20Basic%20performance%20test%20between%20SELECT%20INTO%20and%20INSERT%20INTO%20SELECT" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F07%2Fsql-server-basic-performance-test-between-select-into-and-insert-into-select%2F&amp;linkname=SQL%20Server%3A%20Basic%20performance%20test%20between%20SELECT%20INTO%20and%20INSERT%20INTO%20SELECT" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F07%2Fsql-server-basic-performance-test-between-select-into-and-insert-into-select%2F&amp;linkname=SQL%20Server%3A%20Basic%20performance%20test%20between%20SELECT%20INTO%20and%20INSERT%20INTO%20SELECT" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F07%2Fsql-server-basic-performance-test-between-select-into-and-insert-into-select%2F&amp;linkname=SQL%20Server%3A%20Basic%20performance%20test%20between%20SELECT%20INTO%20and%20INSERT%20INTO%20SELECT" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F07%2Fsql-server-basic-performance-test-between-select-into-and-insert-into-select%2F&amp;linkname=SQL%20Server%3A%20Basic%20performance%20test%20between%20SELECT%20INTO%20and%20INSERT%20INTO%20SELECT" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F07%2Fsql-server-basic-performance-test-between-select-into-and-insert-into-select%2F&#038;title=SQL%20Server%3A%20Basic%20performance%20test%20between%20SELECT%20INTO%20and%20INSERT%20INTO%20SELECT" data-a2a-url="https://www.dbrnd.com/2018/07/sql-server-basic-performance-test-between-select-into-and-insert-into-select/" data-a2a-title="SQL Server: Basic performance test between SELECT INTO and INSERT INTO SELECT"></a></p><p>In this post, I am doing a basic performance test between SELECT INTO and INSERT INTO SELECT option of SQL Server. </p>
<p>Database Developers are using these options very frequently for copying data between the tables. But SELECT INTO is little faster than INSERT INTO SELECT option. </p>
<p><em>Note: I am testing using a very small number of sample records. If you are testing with a huge number of records, you may find a big difference in the performance. </em></p>
<p><strong>Please check the below examples: </strong></p>
<p><strong>Set Statistics:</strong></p>
<pre class="crayon-plain-tag">SET STATISTICS IO ON
SET STATISTICS TIME ON</pre> </p>
<p><strong>Create sample tables:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_MyData (ID INT, Name VARCHAR(10))
CREATE TABLE tbl_Mytable (ID INT, Name VARCHAR(10))

INSERT INTO tbl_MyData VALUES (1,'dbrnd')
GO 1000000</pre> </p>
<p><strong>Perform INSERT INTO SELECT:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO  tbl_Mytable 
SELECT ID, Name 
FROM tbl_MyData</pre> </p>
<p><strong>Execution time:</strong></p>
<pre class="crayon-plain-tag">SQL Server Execution Times:
CPU time = 16 ms,  elapsed time = 37 ms.</pre> </p>
<p><strong>Perform SELECT INTO:</strong></p>
<pre class="crayon-plain-tag">SELECT ID, Name 
INTO tbl_MyTable2
FROM tbl_MyData</pre> </p>
<p><strong>Execution time:</strong></p>
<pre class="crayon-plain-tag">SQL Server Execution Times:
CPU time = 16 ms,  elapsed time = 21 ms.</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2018/07/sql-server-basic-performance-test-between-select-into-and-insert-into-select/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Extra Tabs and Spaces of object occupies more disk area</title>
		<link>https://www.dbrnd.com/2018/06/sql-server-extra-tabs-and-spaces-of-object-occupies-more-disk-area/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 25 Jun 2018 16:16:13 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Extra tabs and spaces]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[sqlcode]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7663</guid>

					<description><![CDATA[<div><img width="679" height="679" src="https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-679x679.png" class="attachment-post-image size-post-image wp-post-image" alt="" decoding="async" srcset="https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-679x679.png 679w, https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-150x150.png 150w, https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-300x300.png 300w, https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-600x600.png 600w, https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object.png 780w" sizes="(max-width: 679px) 100vw, 679px" /></div>In this post, I am sharing an interesting fact on the disk usage by extra tabs and spaces of SQL Server object&#8217;s definition. Many of you don&#8217;t know that extra spaces and tabs are occupying the disk in SQL Server. People are creating their own coding standard and giving a huge number of extra spaces [&#8230;]]]></description>
										<content:encoded><![CDATA[<div><img width="679" height="679" src="https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-679x679.png" class="attachment-post-image size-post-image wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-679x679.png 679w, https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-150x150.png 150w, https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-300x300.png 300w, https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object-600x600.png 600w, https://www.dbrnd.com/wp-content/uploads/2018/05/SQL-Server-Taken-Extra-Space-of-Object.png 780w" sizes="auto, (max-width: 679px) 100vw, 679px" /></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-extra-tabs-and-spaces-of-object-occupies-more-disk-area%2F&amp;linkname=SQL%20Server%3A%20Extra%20Tabs%20and%20Spaces%20of%20object%20occupies%20more%20disk%20area" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-extra-tabs-and-spaces-of-object-occupies-more-disk-area%2F&amp;linkname=SQL%20Server%3A%20Extra%20Tabs%20and%20Spaces%20of%20object%20occupies%20more%20disk%20area" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-extra-tabs-and-spaces-of-object-occupies-more-disk-area%2F&amp;linkname=SQL%20Server%3A%20Extra%20Tabs%20and%20Spaces%20of%20object%20occupies%20more%20disk%20area" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-extra-tabs-and-spaces-of-object-occupies-more-disk-area%2F&amp;linkname=SQL%20Server%3A%20Extra%20Tabs%20and%20Spaces%20of%20object%20occupies%20more%20disk%20area" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-extra-tabs-and-spaces-of-object-occupies-more-disk-area%2F&amp;linkname=SQL%20Server%3A%20Extra%20Tabs%20and%20Spaces%20of%20object%20occupies%20more%20disk%20area" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-extra-tabs-and-spaces-of-object-occupies-more-disk-area%2F&amp;linkname=SQL%20Server%3A%20Extra%20Tabs%20and%20Spaces%20of%20object%20occupies%20more%20disk%20area" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-extra-tabs-and-spaces-of-object-occupies-more-disk-area%2F&#038;title=SQL%20Server%3A%20Extra%20Tabs%20and%20Spaces%20of%20object%20occupies%20more%20disk%20area" data-a2a-url="https://www.dbrnd.com/2018/06/sql-server-extra-tabs-and-spaces-of-object-occupies-more-disk-area/" data-a2a-title="SQL Server: Extra Tabs and Spaces of object occupies more disk area"></a></p><p>In this post, I am sharing an interesting fact on the disk usage by extra tabs and spaces of SQL Server object&#8217;s definition.</p>
<p>Many of you don&#8217;t know that extra spaces and tabs are occupying the disk in SQL Server. People are creating their own coding standard and giving a huge number of extra spaces in the object definition. </p>
<blockquote><p>Nowadays, the disk is not that much costlier so few extra KBs/MBs we can afford. But I saw few servers, where companies are creating a separate database for each client and one database contained thousands of stored procedures. </p></blockquote>
<p><em>So I would suggest, please follow the best practices in your coding life.<br />
From tomorrow, I am going to share few articles on SQL Server Coding Standard and best practices.</em></p>
<p>Here, I created two stored procedures where one has extra tabs and other has normal spaces. You can easily get the size difference between these two stored procedures. </p>
<p><strong>Create sample Stored Procedure with extra Tabs and Spaces:</strong></p>
<pre class="crayon-plain-tag">CREATE PROCEDURE usp_ABC
AS
BEGIN
	DECLARE						@Name					VARCHAR(20)
	DECLARE						@Add					VARCHAR(100)	
END</pre> </p>
<p><strong>Create sample Stored Procedure without extra Tabs and Spaces:</strong></p>
<pre class="crayon-plain-tag">CREATE PROCEDURE usp_XYZ
AS
BEGIN
	DECLARE	@Name VARCHAR(20)
	DECLARE @Add VARCHAR(100)	
END</pre> </p>
<p><strong>Compare the size of above SPs:</strong></p>
<pre class="crayon-plain-tag">SELECT 
	[p].[name]
	,size = DATALENGTH([asm].[definition]) 
FROM sys.all_sql_modules AS asm
INNER JOIN sys.procedures AS p
ON [asm].[object_id] = [p].[object_id]
WHERE [p].[name] IN ('usp_ABC','usp_XYZ')</pre> </p>
<p><strong>Result:</strong></p>
<pre class="crayon-plain-tag">name          size
-----------  ---------
usp_ABC	      230
usp_XYZ       198</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server: Script to PINTABLE and UNPINTABLE (Keep data in Cache)</title>
		<link>https://www.dbrnd.com/2018/06/sql-server-script-to-pintable-and-unpintable-keep-data-in-cache/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 18 Jun 2018 16:16:45 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Data Cache]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[improve query performance]]></category>
		<category><![CDATA[PINTABLE]]></category>
		<category><![CDATA[Query Performanc]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[sqlcode]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[UNPINTABLE]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7659</guid>

					<description><![CDATA[In this post, I am sharing a script to perform PINTABLE and UNPINTABLE in SQL Server. Using PINTABLE, we can keep data in the data cache of SQL Server which boosts the speed of query execution. Once you PINTABLE in the data cache, it doesn&#8217;t load the whole table but it keeps only required a [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-script-to-pintable-and-unpintable-keep-data-in-cache%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20PINTABLE%20and%20UNPINTABLE%20%28Keep%20data%20in%20Cache%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-script-to-pintable-and-unpintable-keep-data-in-cache%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20PINTABLE%20and%20UNPINTABLE%20%28Keep%20data%20in%20Cache%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-script-to-pintable-and-unpintable-keep-data-in-cache%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20PINTABLE%20and%20UNPINTABLE%20%28Keep%20data%20in%20Cache%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-script-to-pintable-and-unpintable-keep-data-in-cache%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20PINTABLE%20and%20UNPINTABLE%20%28Keep%20data%20in%20Cache%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-script-to-pintable-and-unpintable-keep-data-in-cache%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20PINTABLE%20and%20UNPINTABLE%20%28Keep%20data%20in%20Cache%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-script-to-pintable-and-unpintable-keep-data-in-cache%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20PINTABLE%20and%20UNPINTABLE%20%28Keep%20data%20in%20Cache%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-script-to-pintable-and-unpintable-keep-data-in-cache%2F&#038;title=SQL%20Server%3A%20Script%20to%20PINTABLE%20and%20UNPINTABLE%20%28Keep%20data%20in%20Cache%29" data-a2a-url="https://www.dbrnd.com/2018/06/sql-server-script-to-pintable-and-unpintable-keep-data-in-cache/" data-a2a-title="SQL Server: Script to PINTABLE and UNPINTABLE (Keep data in Cache)"></a></p><p>In this post, I am sharing a script to perform PINTABLE and UNPINTABLE in SQL Server. </p>
<p>Using PINTABLE, we can keep data in the data cache of SQL Server which boosts the speed of query execution. </p>
<p>Once you PINTABLE in the data cache, it doesn&#8217;t load the whole table but it keeps only required a portion of the table. </p>
<p>If you have enough size of the data cache, you can try this option for a frequently used table. </p>
<blockquote class="wp-embedded-content" data-secret="Mb9pqYDm86"><p><a href="https://www.dbrnd.com/2017/01/sql-server-interview-how-internally-it-executes-a-sql-statement/">SQL Server Interview: How internally it executes a SQL Statement?</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2017/01/sql-server-interview-how-internally-it-executes-a-sql-statement/embed/#?secret=Mb9pqYDm86" data-secret="Mb9pqYDm86" width="600" height="338" title="&#8220;SQL Server Interview: How internally it executes a SQL Statement?&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>PINTABLE: </strong></p>
<pre class="crayon-plain-tag">DECLARE @DBID INT, @TableID INT 
SET @DBID = DB_ID('DatabaseName') 
SET @TableID = OBJECT_ID('DatabaseName.SchemaName.TableName') 
DBCC PINTABLE (@DBID, @TableID)</pre> </p>
<p><strong>UNPINTABLE:</strong></p>
<pre class="crayon-plain-tag">DECLARE @DBID INT, @TableID INT 
SET @DBID = DB_ID('DatabaseName') 
SET @TableID = OBJECT_ID('DatabaseName.SchemaName.TableName') 
DBCC UNPINTABLE (@DBID, @TableID)</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server 2014: Examples of Table Variable with Indexes</title>
		<link>https://www.dbrnd.com/2018/06/sql-server-2014-examples-of-table-variable-with-indexes/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 11 Jun 2018 16:16:29 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[SQL Sever 2014]]></category>
		<category><![CDATA[sqlcode]]></category>
		<category><![CDATA[Table Variable]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7653</guid>

					<description><![CDATA[I didn&#8217;t explore few features of SQL Server 2014. Recently, I got to know that in SQL Server 2014 we can create indexes on Table Variable. If you look into the differences between Temp Table and Table Variable, always you can find one difference like we can&#8217;t create an index on Table Variable and we [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-2014-examples-of-table-variable-with-indexes%2F&amp;linkname=SQL%20Server%202014%3A%20Examples%20of%20Table%20Variable%20with%20Indexes" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-2014-examples-of-table-variable-with-indexes%2F&amp;linkname=SQL%20Server%202014%3A%20Examples%20of%20Table%20Variable%20with%20Indexes" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-2014-examples-of-table-variable-with-indexes%2F&amp;linkname=SQL%20Server%202014%3A%20Examples%20of%20Table%20Variable%20with%20Indexes" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-2014-examples-of-table-variable-with-indexes%2F&amp;linkname=SQL%20Server%202014%3A%20Examples%20of%20Table%20Variable%20with%20Indexes" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-2014-examples-of-table-variable-with-indexes%2F&amp;linkname=SQL%20Server%202014%3A%20Examples%20of%20Table%20Variable%20with%20Indexes" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-2014-examples-of-table-variable-with-indexes%2F&amp;linkname=SQL%20Server%202014%3A%20Examples%20of%20Table%20Variable%20with%20Indexes" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-2014-examples-of-table-variable-with-indexes%2F&#038;title=SQL%20Server%202014%3A%20Examples%20of%20Table%20Variable%20with%20Indexes" data-a2a-url="https://www.dbrnd.com/2018/06/sql-server-2014-examples-of-table-variable-with-indexes/" data-a2a-title="SQL Server 2014: Examples of Table Variable with Indexes"></a></p><p>I didn&#8217;t explore few features of SQL Server 2014. Recently, I got to know that in SQL Server 2014 we can create indexes on Table Variable. </p>
<p>If you look into the differences between Temp Table and Table Variable, always you can find one difference like we can&#8217;t create an index on Table Variable and we can create an index on the Temp Table. </p>
<blockquote class="wp-embedded-content" data-secret="oa9njLGXKU"><p><a href="https://www.dbrnd.com/2016/02/sql-server-difference-between-temporary-table-and-table-variable/">SQL Server: Difference between Temporary Table and Table Variable</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/02/sql-server-difference-between-temporary-table-and-table-variable/embed/#?secret=oa9njLGXKU" data-secret="oa9njLGXKU" width="600" height="338" title="&#8220;SQL Server: Difference between Temporary Table and Table Variable&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>In this post, I am sharing few examples of Table variable with Indexes.<br />
Please check the below samples:</p>
<p><strong>Define Table variable with Primary key and Unique key:</strong></p>
<pre class="crayon-plain-tag">DECLARE @dbrnd TABLE
(
	ID INT PRIMARY KEY
	,Name NVARCHAR(10) UNIQUE
)</pre> </p>
<p><strong>Define Table variable with Non-Clustered index:</strong></p>
<pre class="crayon-plain-tag">DECLARE @dbrnd TABLE
(
	ID INT PRIMARY KEY
	,Name NVARCHAR(20)
	,INDEX idx_dbrnd_Name NONCLUSTERED (Name)
)</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server Coding Standards: Working with  Error Handling and Transaction Support</title>
		<link>https://www.dbrnd.com/2018/06/sql-server-coding-standards-working-with-error-handling-and-transaction-support/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 01 Jun 2018 16:16:39 +0000</pubDate>
				<category><![CDATA[SQL Server Coding Standards]]></category>
		<category><![CDATA[basic sql commands]]></category>
		<category><![CDATA[basic sql queries]]></category>
		<category><![CDATA[coding best practices]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql basics]]></category>
		<category><![CDATA[sql coding best practices]]></category>
		<category><![CDATA[sql commands]]></category>
		<category><![CDATA[sql database]]></category>
		<category><![CDATA[sql formatter]]></category>
		<category><![CDATA[sql language]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[sql queries for practice]]></category>
		<category><![CDATA[sql query formatter]]></category>
		<category><![CDATA[sql server format]]></category>
		<category><![CDATA[sqlcode]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7648</guid>

					<description><![CDATA[Prepared by Bihag Thaker The stored procedure must ensure its atomicity, i.e. it either executes all statements or none. Client code (caller of a stored procedure) expects it to be this way. It does not know how many statements are actually executed and may not implement any transaction support. In case of failure, the stored [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-coding-standards-working-with-error-handling-and-transaction-support%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20%20Error%20Handling%20and%20Transaction%20Support" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-coding-standards-working-with-error-handling-and-transaction-support%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20%20Error%20Handling%20and%20Transaction%20Support" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-coding-standards-working-with-error-handling-and-transaction-support%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20%20Error%20Handling%20and%20Transaction%20Support" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-coding-standards-working-with-error-handling-and-transaction-support%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20%20Error%20Handling%20and%20Transaction%20Support" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-coding-standards-working-with-error-handling-and-transaction-support%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20%20Error%20Handling%20and%20Transaction%20Support" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-coding-standards-working-with-error-handling-and-transaction-support%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20%20Error%20Handling%20and%20Transaction%20Support" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F06%2Fsql-server-coding-standards-working-with-error-handling-and-transaction-support%2F&#038;title=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20%20Error%20Handling%20and%20Transaction%20Support" data-a2a-url="https://www.dbrnd.com/2018/06/sql-server-coding-standards-working-with-error-handling-and-transaction-support/" data-a2a-title="SQL Server Coding Standards: Working with  Error Handling and Transaction Support"></a></p><p>Prepared by <a href="https://www.linkedin.com/in/sqlart/" rel="noopener" target="_blank">Bihag Thaker</a></p>
<blockquote><p>The stored procedure must ensure its atomicity, i.e. it either executes all statements or none. Client code (caller of a stored procedure) expects it to be this way. </p>
<p>It does not know how many statements are actually executed and may not implement any transaction support. In case of failure, the stored procedure must properly roll back all changes.</p></blockquote>
<blockquote>
<p>Before rolling back a transaction, it should check the value of @@TRANCOUNT </p>
<pre class="crayon-plain-tag">IF @@TRANCOUNT &gt; 1
ROLLBACK TRANSACTION</pre> </p></blockquote>
<blockquote><p>SQL Server 2005 and later versions should use BEGIN TRY…END TRY, BEGIN CATCH…END CATCH syntax for exception handling in stored procedures.</p></blockquote>
<blockquote><p>The stored procedure should not hide/suppress errors. In cases when business functionality requires a client code to call several stored procedures/queries in a row and considers the action as an atomic action, client code must be implementing transaction support, which normally relies on the fact that an exception is raised if stored procedure fails. </p>
<p>If stored procedure hides an error, client transaction support will not work. </p>
<p>To follow this standard, throw an exception with RAISERROR() function in BEGIN CATCH…END CATCH block, pass the error details to the application and let the application handle the error. Pass the Error Number with a @ErrorNumber output parameter.</p></blockquote>
<p>Following should be the general logic for implementing transaction along with proper exception handling:</p>
<pre class="crayon-plain-tag">BEGIN TRY
	BEGIN TRANSACTION
		--DML Statements go here within transaction.
	COMMIT TRANSACTION
	RETURN 0
END TRY
BEGIN CATCH
	DECLARE @ErrorNumber INT
	DECLARE @ErrorMessage NVARCHAR(4000);
	DECLARE @ErrorSeverity INT;
	DECLARE @ErrorState INT;
SELECT
	@ErrorNumber = ERROR_NUMBER(),
	@ErrorMessage = ERROR_MESSAGE(),
	@ErrorSeverity = ERROR_SEVERITY(),
	@ErrorState = ERROR_STATE();
	IF @@TRANCOUNT &gt; 0	
		ROLLBACK TRANSACTION
	RAISERROR(@ErrorMessage,@ErrorSeverity,@ErrorState)
	RETURN 1
END CATCH</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server Coding Standards: Working with Triggers</title>
		<link>https://www.dbrnd.com/2018/05/sql-server-coding-standards-working-with-triggers/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 31 May 2018 16:16:27 +0000</pubDate>
				<category><![CDATA[SQL Server Coding Standards]]></category>
		<category><![CDATA[basic sql commands]]></category>
		<category><![CDATA[basic sql queries]]></category>
		<category><![CDATA[coding best practices]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql basics]]></category>
		<category><![CDATA[sql coding best practices]]></category>
		<category><![CDATA[sql commands]]></category>
		<category><![CDATA[sql database]]></category>
		<category><![CDATA[sql formatter]]></category>
		<category><![CDATA[sql language]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[sql queries for practice]]></category>
		<category><![CDATA[sql query formatter]]></category>
		<category><![CDATA[sql server format]]></category>
		<category><![CDATA[sqlcode]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7646</guid>

					<description><![CDATA[Prepared by Bihag Thaker Prefix the User Defined Function Name with ‘trg_’. Try to include only letters in identifiers by using Pascal Casing and avoid the use of special characters in identifiers. Underscore (_) character may be used for element separation in an identifier. Here element means Application Prefix, type of Operation, Table Name or [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-triggers%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Triggers" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-triggers%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Triggers" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-triggers%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Triggers" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-triggers%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Triggers" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-triggers%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Triggers" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-triggers%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Triggers" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-triggers%2F&#038;title=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Triggers" data-a2a-url="https://www.dbrnd.com/2018/05/sql-server-coding-standards-working-with-triggers/" data-a2a-title="SQL Server Coding Standards: Working with Triggers"></a></p><p>Prepared by <a href="https://www.linkedin.com/in/sqlart/" rel="noopener" target="_blank">Bihag Thaker</a></p>
<blockquote><p>Prefix the User Defined Function Name with ‘trg_’.</p></blockquote>
<blockquote><p>Try to include only letters in identifiers by using Pascal Casing and avoid the use of special characters in identifiers. Underscore (_) character may be used for element separation in an identifier. </p>
<p>Here element means Application Prefix, type of Operation, Table Name or Entity Name etc. However, word separation in single element should be achieved with PascalCasing only. </p></blockquote>
<blockquote><p>Use proper, meaningful and self-explanatory identifiers for user-defined functions. Following naming convention should be used for trigger:</p>
<p>‘trg_’ + <TableName> + ‘_’ + <TriggerType> + ‘_’ + <Operation></p>
<p>Here, <TableName> is the name of the table on which the trigger is defined. <TriggerType> is the type of trigger. It can be either ‘AFTER’ or ‘INSTEADOF’. <Operation> is the underscore (_) separated list of operations for which trigger is defined. It can be INSERT, DELETE or UPDATE.</p>
<p>Some of the examples are:</p>
<pre class="crayon-plain-tag">trg_tblOrders_After_DELETE
trg_tblOrders_After_INSERT_UPDATE</pre> </p></blockquote>
<blockquote><p>If there is some alternative solution to using triggers then avoid creating frequent triggers on the table as they degrade the performance of the DML statements.</p></blockquote>
<blockquote><p>Check if the same functionality can be achieved using the constraints as constraints are faster than triggers. Only use triggers for complex business logic whose implementation is not possible by constraints.</p>
<p>If the trigger is specified for INSERT, UPDATE or DELETE, do not use OUTPUT clause in their respective DML statements that fire the trigger.</p></blockquote>
<p>Sample Trigger Code Snippet:</p>
<pre class="crayon-plain-tag">CREATE TRIGGER trg_TableName_AFTER_INSERT_UPDATE
ON [dbo].[tblCustomers]
AFTER INSERT,UPDATE
AS
/*
	**********************Creation Details**********************
	Stored Procedure Name : trg_TableName_AFTER_INSERT_UPDATE
	Purpose : Implements Business logic on INSERT and UPDATE
	Author : Author Name
	Created On : 2017/01/01
	-------------------------------------------------------------
	*****************************Revision Details*****************************
	Project/
	Revision No. Changed On  Changed By  Change Description
	------------ ----------  ----------  ------------------
	1234         2018/02/10   Mr. ABC    Business Logic Changed.
*/
BEGIN
	--Body of Trigger follows
END</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server Coding Standards: Working with User Defined Functions</title>
		<link>https://www.dbrnd.com/2018/05/sql-server-coding-standards-working-with-user-defined-functions/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 30 May 2018 16:16:15 +0000</pubDate>
				<category><![CDATA[SQL Server Coding Standards]]></category>
		<category><![CDATA[basic sql commands]]></category>
		<category><![CDATA[basic sql queries]]></category>
		<category><![CDATA[coding best practices]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql basics]]></category>
		<category><![CDATA[sql coding best practices]]></category>
		<category><![CDATA[sql commands]]></category>
		<category><![CDATA[sql database]]></category>
		<category><![CDATA[sql formatter]]></category>
		<category><![CDATA[sql language]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[sql queries for practice]]></category>
		<category><![CDATA[sql query formatter]]></category>
		<category><![CDATA[sql server format]]></category>
		<category><![CDATA[sqlcode]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7641</guid>

					<description><![CDATA[Prepared by Bihag Thaker Prefix the User Defined Function Name with ‘udf_’. Try to include only letters in identifiers by using Pascal Casing and avoid the use of special characters in identifiers. Underscore (_) character may be used for element separation in an identifier. Here element means Application Prefix, type of Operation, Table Name or [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-user-defined-functions%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20User%20Defined%20Functions" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-user-defined-functions%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20User%20Defined%20Functions" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-user-defined-functions%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20User%20Defined%20Functions" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-user-defined-functions%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20User%20Defined%20Functions" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-user-defined-functions%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20User%20Defined%20Functions" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-user-defined-functions%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20User%20Defined%20Functions" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-user-defined-functions%2F&#038;title=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20User%20Defined%20Functions" data-a2a-url="https://www.dbrnd.com/2018/05/sql-server-coding-standards-working-with-user-defined-functions/" data-a2a-title="SQL Server Coding Standards: Working with User Defined Functions"></a></p><p>Prepared by <a href="https://www.linkedin.com/in/sqlart/" rel="noopener" target="_blank">Bihag Thaker</a></p>
<blockquote><p>Prefix the User Defined Function Name with ‘udf_’.</p></blockquote>
<blockquote><p>Try to include only letters in identifiers by using Pascal Casing and avoid the use of special characters in identifiers. Underscore (_) character may be used for element separation in an identifier. </p>
<p>Here element means Application Prefix, type of Operation, Table Name or Entity Name etc. However, word separation in single element should be achieved with PascalCasing only. </p></blockquote>
<blockquote><p>Use proper, meaningful and self-explanatory identifiers for user defined functions. Following naming convention should be used for stored functions:</p>
<p>‘udf_’ + <ApplicationPrefix> + ‘_’ + <Operation> + <EntityName></p></blockquote>
<blockquote><p>Here, <ApplicationPrefix> part can be optional. <Operation> can be a verb like ‘Get’, ‘Check’, ‘Validate’ or ‘Return’ and so on. Some of the examples are:</p>
<pre class="crayon-plain-tag">udf_Person_ValidateEmailAddress()
udf_HR_CalculateNetSalary()</pre> </p></blockquote>
<blockquote><p>Use schemas to separate different sets of user defined functions across multiple applications when possible. For example, if schema Person is used instead of prefix ‘Person_’ and schema HR is used instead of prefix ‘HR_’, then above user defined functions should be as follows:</p>
<pre class="crayon-plain-tag">[Person].[udf_ValidateEmailAddress]()
[HR].[udf_CalculateNetSalary]()</pre> </p></blockquote>
<blockquote><p>Do not create Scalar-Valued Function extensively. Avoid use of Scalar-Valued Functions in queries as much as possible because they degrade the performance of the queries.</p>
<p>Do not create Scalar-Valued Function just for the purpose of code reusability. If same functionality can be achieved with some inline calculation, then avoid functions. Code Reusability should not come at application’s performance cost.</p></blockquote>
<blockquote><p>Do not create Multiline Table-Valued Function as they tend to degrade the performance of the queries. If the same functionality can be achieved with Inline Table-valued Function, then avoid using Multiline Table-valued Function.</p>
<p>While creating stored procedures, specify the name of the schema explicitly within which the stored procedure is to be created even if it is dbo.</p></blockquote>
<p>Following is the sample template of User Defined Function. It has been provided here only to have a basic idea of coding structure and standards that a stored procedure should follow:</p>
<pre class="crayon-plain-tag">CREATE FUNCTION [dbo].[udf_FunctionName]
(
	@Param1 INT
	,@Param2 INT
	,@Param3 VARCHAR(50)
) RETURNS BIT AS
/*
	**********************Creation Details**********************
	Stored Procedure Name : udf_FunctionName
	Purpose : Calculates and returns the value.
	Author : Author Name
	Created On : 2017/01/01
	*****************************Revision Details*****************************
	Project/
	Revision No. Changed On  Changed By  Change Description
	------------ ----------  ----------  ------------------
	1234         2018/01/01   Mr. ABC    Changed the calculation part.
	1235         2018/02/08   Mr. XYZ    Revert the previous change.
*/
BEGIN
	--Function Body goes here.
	 RETURN (0)
END</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server Coding Standards: Working with Stored Procedures</title>
		<link>https://www.dbrnd.com/2018/05/sql-server-coding-standards-working-with-stored-procedures/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 29 May 2018 16:16:09 +0000</pubDate>
				<category><![CDATA[SQL Server Coding Standards]]></category>
		<category><![CDATA[basic sql commands]]></category>
		<category><![CDATA[basic sql queries]]></category>
		<category><![CDATA[coding best practices]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql basics]]></category>
		<category><![CDATA[sql coding best practices]]></category>
		<category><![CDATA[sql commands]]></category>
		<category><![CDATA[sql database]]></category>
		<category><![CDATA[sql formatter]]></category>
		<category><![CDATA[sql language]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[sql queries for practice]]></category>
		<category><![CDATA[sql query formatter]]></category>
		<category><![CDATA[sql server format]]></category>
		<category><![CDATA[sqlcode]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7637</guid>

					<description><![CDATA[Prepared by Bihag Thaker Prefix the Stored Procedure Name with ‘usp_’. Try to include only letters in identifiers by using Pascal Casing and avoid the use of special characters in identifiers. Underscore (_) character may be used for element separation in an identifier. Here element means Application Prefix, type of Operation, Table Name or Entity [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-stored-procedures%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Stored%20Procedures" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-stored-procedures%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Stored%20Procedures" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-stored-procedures%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Stored%20Procedures" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-stored-procedures%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Stored%20Procedures" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-stored-procedures%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Stored%20Procedures" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-stored-procedures%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Stored%20Procedures" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-working-with-stored-procedures%2F&#038;title=SQL%20Server%20Coding%20Standards%3A%20Working%20with%20Stored%20Procedures" data-a2a-url="https://www.dbrnd.com/2018/05/sql-server-coding-standards-working-with-stored-procedures/" data-a2a-title="SQL Server Coding Standards: Working with Stored Procedures"></a></p><p>Prepared by <a href="https://www.linkedin.com/in/sqlart/" rel="noopener" target="_blank">Bihag Thaker</a></p>
<blockquote><p>Prefix the Stored Procedure Name with ‘usp_’.</p></blockquote>
<blockquote><p>Try to include only letters in identifiers by using Pascal Casing and avoid the use of special characters in identifiers. Underscore (_) character may be used for element separation in an identifier. </p>
<p>Here element means Application Prefix, type of Operation, Table Name or Entity Name etc. However, word separation in single element should be achieved with PascalCasing only. </p></blockquote>
<blockquote><p>Use proper, meaningful and self-explanatory identifiers for stored procedures. Stored procedures are generally used for different actions like INSERT, UPDATE, DELETE and SELECT operations.</p>
<p>Following naming convention should be used for stored procedures:<br />
‘usp_’ + <ApplicationPrefix> + ‘_’ + <Operation> + <TableName>/<EntityName></p></blockquote>
<blockquote><p>Here, <ApplicationPrefix> part can be optional. <Operation> can be a verb like ‘Add’, ‘Insert’, ‘Update’, ‘Change’, ‘Set’, ‘Delete’, ‘Remove’, ‘Select’ and ‘Get’ and so on. Some of the examples are:</p>
<pre class="crayon-plain-tag">usp_Sales_GetOrderDetails
usp_Sales_InsertOrderDetails
usp_Sales_UpdateOrderDetails
usp_Sales_DeleteOrderDetails</pre> </p></blockquote>
<blockquote><p>Use schemas to separate different sets of stored procedures across multiple applications when possible. For example, if schema Sales is used instead of ‘Sales_’ as Application Prefix, then above Stored Procedures should be as follows:</p>
<pre class="crayon-plain-tag">[Sales].[usp_ GetOrderDetails]
[Sales].[usp_ InsertOrderDetails]
[Sales].[usp_ UpdateOrderDetails]
[Sales].[usp_ DeleteOrderDetails]</pre> </p></blockquote>
<blockquote><p>Create stored procedures and try to implement functionalities through stored procedures wherever possible rather than using ad-hoc queries from the application.</p>
<p>While creating stored procedures, specify the name of the schema explicitly within which the stored procedure is to be created even if it is dbo.</p></blockquote>
<blockquote><p>Return the success or failure status from the stored procedure with the use of RETURN statement. Value 1 should be used for success and 0 for failure.</p></blockquote>
<blockquote><p>If a stored procedure is required to return some value, do not use RETURN statement. The RETURN statement is not intended for this purpose. Consider using OUTPUT parameters as required in stored procedures and pass output values from OUTPUT parameters to the application.</p>
<p>Use @ErrorCode OUTPUT parameter to communicate with the application any error occurred in the stored procedure.</p></blockquote>
<blockquote><p>When possible, consider single stored procedure call which returns multiple result sets to the application rather than calling stored procedure multiple times for each different result set. This reduces round trips to the database server and improves the application response time. </p></blockquote>
<blockquote><p>Do not write very lengthy stored procedures. Try to keep them as short as possible. </p>
<p>If business logic which needs to be implemented within a stored procedure is very lengthy, then try to break the business logic into different modules and implement each different module in its separate stored procedure and call those sub-stored procedures in the main stored procedure. </p></blockquote>
<blockquote><p>Use SET NOCOUNT ON at the beginning of the stored procedures whenever possible. This can improve the performance of the queries.</p></blockquote>
<blockquote><p>Consider declaring variables at the top of stored procedures.</p>
<p>Do not use temporary tables and table variables frequently. When possible, see if the same functionality can be achieved by using Table Expressions like derived tables and Common Table Expressions.</p>
<p>Avoid creating frequent temporary tables in IF conditions within the stored procedure.</p></blockquote>
<blockquote><p>Whenever intermediate result set needs to be stored within a stored procedure, consider implementing temporary table approach and table variable approach. Compare the results between two approaches and choose the one which is efficient.</p>
<p>Always use transactions in the stored procedures when multiple DML statements are performed within a unit of work.</p>
<p>Keep transactions as short as possible to reduce locking issues.</p>
<p>Do not use WITH RECOMPILE option with the stored procedures.</p></blockquote>
<p>Following is the sample template of a stored procedure. It has been provided here only to have a basic idea of coding structure and standards that a stored procedure should follow:</p>
<pre class="crayon-plain-tag">CREATE PROCEDURE [dbo].[usp_StoredProcedureName]
(
	@Parameter1 INT
	,@Parameter2 VARCHAR(50)
	,@ReturnValue1 INT OUTPUT
	,@ErrorCode INT = 0 OUTPUT
) AS
/*
	**********************Creation Details**********************
	Stored Procedure Name : [dbo].[usp_StoredProcedureName]
	Purpose : Insert a new record in TableName
	Author : Author Name
	Created On : 2017/01/01
	*****************************Revision Details*****************************
	Project/
	Revision No. Changed On  Changed By  Change Description
	------------ ----------  ----------  ------------------
	1234	     2018/01/10   Mr. ABC     New column 'NewColumnName' added.
	1235         2018/03/16   Mr. XYZ     Column 'ColumnName' deleted.
*/

BEGIN
	SET NOCOUNT ON
	BEGIN TRY
		BEGIN TRANSACTION
			--INSERT statement and other logic go here.
		COMMIT TRANSACTION
		RETURN 0
	END TRY
	BEGIN CATCH
		IF @@TRANCOUNT &gt; 0
		ROLLBACK TRANSACTION
		RETURN 1
	END CATCH
END</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server Coding Standards: General Guidelines and usage of Views</title>
		<link>https://www.dbrnd.com/2018/05/sql-server-coding-standards-general-guidelines-and-usage-of-views/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 28 May 2018 16:16:36 +0000</pubDate>
				<category><![CDATA[SQL Server Coding Standards]]></category>
		<category><![CDATA[basic sql commands]]></category>
		<category><![CDATA[basic sql queries]]></category>
		<category><![CDATA[coding best practices]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql basics]]></category>
		<category><![CDATA[sql coding best practices]]></category>
		<category><![CDATA[sql commands]]></category>
		<category><![CDATA[sql database]]></category>
		<category><![CDATA[sql formatter]]></category>
		<category><![CDATA[sql language]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[sql queries for practice]]></category>
		<category><![CDATA[sql query formatter]]></category>
		<category><![CDATA[sql server format]]></category>
		<category><![CDATA[sqlcode]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7629</guid>

					<description><![CDATA[Prepared by Bihag Thaker Prefix the View Name with ‘vw_’ Try to include only letters in identifiers by using Pascal Casing and avoid the use of special characters in identifiers. Underscore (_) character may be used for element separation in an identifier. Here element means Application Prefix, Group Name, Table Name or Entity Name etc. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-general-guidelines-and-usage-of-views%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20General%20Guidelines%20and%20usage%20of%20Views" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-general-guidelines-and-usage-of-views%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20General%20Guidelines%20and%20usage%20of%20Views" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-general-guidelines-and-usage-of-views%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20General%20Guidelines%20and%20usage%20of%20Views" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-general-guidelines-and-usage-of-views%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20General%20Guidelines%20and%20usage%20of%20Views" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-general-guidelines-and-usage-of-views%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20General%20Guidelines%20and%20usage%20of%20Views" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-general-guidelines-and-usage-of-views%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20General%20Guidelines%20and%20usage%20of%20Views" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-general-guidelines-and-usage-of-views%2F&#038;title=SQL%20Server%20Coding%20Standards%3A%20General%20Guidelines%20and%20usage%20of%20Views" data-a2a-url="https://www.dbrnd.com/2018/05/sql-server-coding-standards-general-guidelines-and-usage-of-views/" data-a2a-title="SQL Server Coding Standards: General Guidelines and usage of Views"></a></p><p>Prepared by <a href="https://www.linkedin.com/in/sqlart/" rel="noopener" target="_blank">Bihag Thaker</a></p>
<blockquote><p>Prefix the View Name with ‘vw_’</p></blockquote>
<blockquote><p>Try to include only letters in identifiers by using Pascal Casing and avoid the use of special characters in identifiers. Underscore (_) character may be used for element separation in an identifier. </p>
<p>Here element means Application Prefix, Group Name, Table Name or Entity Name etc. However, word separation in single element should be achieved with PascalCasing only. </p></blockquote>
<blockquote><p>Use proper, meaningful and self-explanatory identifiers for Views. Views are used for SELECT operations. </p>
<p>Following naming convention should be used for view: ‘vw_’ + <ApplicationPrefix> + ‘_’ + <Operation> + <TableName>/<EntityName><Description> Here, <ApplicationPrefix> part can be optional. <Operation> can generally a verb like ‘Get’ or ‘Select’ and so on. <Description> can be optional additional description. Some of the examples are:</p>
<pre class="crayon-plain-tag">vw_Sales_GetOrderDetailsForToday
vw_Sales_GetOrderDetailsTotalByCustomerID</pre> </p>
</blockquote>
<blockquote><p>Use schemas to separate different sets Views across multiple applications when possible. For example, if schema Sales is used instead of ‘Sales_’ as Application Prefix, then above Views should be as follows:</p>
<pre class="crayon-plain-tag">[Sales].[vw_ GetOrderDetailsForToday] 
[Sales].[vw_ GetOrderDetailsTotalByCustomerID]</pre> </p>
</blockquote>
<blockquote><p>When queries are frequently executed which require complex joins and calculations, then you should implement them in view. Consider creating views when same complex queries are executed from the different part of applications.</p>
<p>Whenever possible, create views that are schema bound.</p></blockquote>
<p>Following is the sample template of a stored procedure. It has been provided here only to have a basic idea of coding structure and standards that a stored procedure should follow:</p>
<pre class="crayon-plain-tag">CREATE VIEW [dbo].[vw_ViewName]
AS
/*
**********************Creation Details**********************
View Name : [dbo].[vw_ViewName]
Purpose : Get all Items along with totals.
Author : Author Name
Created On : 2017/01/01
*****************************Revision Details*****************************
Project/
Revision No. Changed On Changed By Change Description
------------ ---------- ---------- ------------------
1234         2017/08/08 Mr. ABC    Two Columns Column1 and Column2 added.
1235         2018/01/13 Mr. XYZ    Caculation added.
*/

--SELECT Query goes here</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server Coding Standards: Best practices with Constraints</title>
		<link>https://www.dbrnd.com/2018/05/sql-server-coding-standards-best-practices-with-constraints/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 27 May 2018 16:16:21 +0000</pubDate>
				<category><![CDATA[SQL Server Coding Standards]]></category>
		<category><![CDATA[basic sql commands]]></category>
		<category><![CDATA[basic sql queries]]></category>
		<category><![CDATA[coding best practices]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql basics]]></category>
		<category><![CDATA[sql coding best practices]]></category>
		<category><![CDATA[sql commands]]></category>
		<category><![CDATA[sql database]]></category>
		<category><![CDATA[sql formatter]]></category>
		<category><![CDATA[sql language]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[sql queries]]></category>
		<category><![CDATA[sql queries for practice]]></category>
		<category><![CDATA[sql query formatter]]></category>
		<category><![CDATA[sql server format]]></category>
		<category><![CDATA[sqlcode]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7626</guid>

					<description><![CDATA[Prepared by Bihag Thaker Data integrity is an important aspect of any database. Data integrity is implemented by using constraints. Use constraints wisely as per the requirement and nature of the application. Constraints enforce data integrity rules but using lot of constraints may realise overhead when performing DML operations on data. So, use them as [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-best-practices-with-constraints%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Best%20practices%20with%20Constraints" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-best-practices-with-constraints%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Best%20practices%20with%20Constraints" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-best-practices-with-constraints%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Best%20practices%20with%20Constraints" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-best-practices-with-constraints%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Best%20practices%20with%20Constraints" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-best-practices-with-constraints%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Best%20practices%20with%20Constraints" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-best-practices-with-constraints%2F&amp;linkname=SQL%20Server%20Coding%20Standards%3A%20Best%20practices%20with%20Constraints" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F05%2Fsql-server-coding-standards-best-practices-with-constraints%2F&#038;title=SQL%20Server%20Coding%20Standards%3A%20Best%20practices%20with%20Constraints" data-a2a-url="https://www.dbrnd.com/2018/05/sql-server-coding-standards-best-practices-with-constraints/" data-a2a-title="SQL Server Coding Standards: Best practices with Constraints"></a></p><p>Prepared by <a href="https://www.linkedin.com/in/sqlart/" rel="noopener" target="_blank">Bihag Thaker</a></p>
<p>Data integrity is an important aspect of any database. Data integrity is implemented by using constraints.</p>
<p>Use constraints wisely as per the requirement and nature of the application. Constraints enforce data integrity rules but using lot of constraints may realise overhead when performing DML operations on data. </p>
<p>So, use them as per the requirement of the application. Generally, you should use constraints to enforce the data integrity. Following are some of the guidelines which should be followed:</p>
<blockquote><p>Creating a clustered primary key constraint is generally the best practice. You should have one primary key constraint preferably clustered on every table in your database.</p></blockquote>
<blockquote><p>To maintain data integrity, use unique constraints when a column is not expected to hold duplicate values in any case. For example, a unique constraint should be defined on Social Security Number.</p></blockquote>
<blockquote><p>Apply referential integrity with foreign key constraints whenever possible to establish relationships between primary table and foreign table to maintain data integrity.</p></blockquote>
<blockquote><p>Always, try to apply NOT NULL constraints on columns wherever possible and keep minimum columns nullable as possible. This disallows columns to have null values and simplifies the queries and other DML operations.</p></blockquote>
<blockquote><p>When it is required for a column to allow NULL values, try to define a default value by creating a default constraint on that column.</p></blockquote>
<blockquote><p>When a column is expected to hold a specific domain of values of either a set of predefined values or a range of values, then apply check constraints to maintain the data integrity on such columns.</p></blockquote>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
