<?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>Common Table Expression &#8211; Sibeesh Passion</title>
	<atom:link href="https://mail.sibeeshpassion.com/tag/common-table-expression/feed/" rel="self" type="application/rss+xml" />
	<link>https://mail.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Tue, 10 Jul 2018 10:36:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Common Table Expression &#8211; Sibeesh Passion</title>
	<link>https://mail.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Stored Procedure With Common Table Expression Or CTE</title>
		<link>https://mail.sibeeshpassion.com/stored-procedure-with-common-table-expression-or-cte-2/</link>
					<comments>https://mail.sibeeshpassion.com/stored-procedure-with-common-table-expression-or-cte-2/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Mon, 29 Feb 2016 00:00:31 +0000</pubDate>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Common Table Expression]]></category>
		<category><![CDATA[CTE]]></category>
		<category><![CDATA[Paging Query In SQL]]></category>
		<category><![CDATA[ROW_NUMBER]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11304</guid>

					<description><![CDATA[In this post we will see how to use common table expression or CTE in our SQL Server. There are so many situations that you may need to use a common table expression. I had a situation of returning ROW_NUMBER variable value for my paging query in SQL, for this I used CTE. A common table expression is actually a temporary result set or a table whose scope is defined or limited to the current statement. In this post I will explain the same in detail. I hope you will like this. Background I had a situation of using a [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In this post we will see how to use common table expression or CTE in our <a href="http://sibeeshpassion.com/category/database/sql/" target="_blank">SQL</a> Server. There are so many situations that you may need to use a common table expression. I had a situation of returning <em>ROW_NUMBER</em> variable value for my paging query in SQL, for this I used CTE. A common table expression is actually a temporary result set or a table whose scope is defined or limited to the current statement.  In this post I will explain the same in detail. I hope you will like this. </p>
<p><strong>Background</strong></p>
<p>I had a situation of using a paging query for one of my application which actually load data to a grid on demand, like when user scrolls or do an y paging. For this I needed to create a stored procedure which accepts page offset as a parameter and return the data accordingly. I used Common Table Expression for the same. </p>
<p><strong>When to use a CTE</strong></p>
<p>There are some situations that you may need to use a CTE, few of them are listed below. </p>
<li>When you are working with recursive queries.</li>
<li>When you need to reference a temporary variable in your query.</li>
<li>You can create temporary views by using CTE, so that you do not need to store the details as view.</li>
<p><strong>Using the code</strong></p>
<p>I hope you all got an idea about CTE, now we can see the basic structure of a common table expression. </p>
<p>[sql]<br />
WITH CTE_Name(Column_Names,&#8230;) AS<br />
  (<br />
	&#8211;Select Query<br />
  )<br />
SELECT *<br />
FROM CTE_Name<br />
WHERE Column_Names1&gt;=Your Condition<br />
END<br />
[/sql]</p>
<p>With the above structure I have created my own stored procedure as follows. </p>
<p>[sql]<br />
USE [TrialsDB]<br />
GO<br />
/****** Object:  StoredProcedure [dbo].[usp_Get_SalesOrderDetailPage]    Script Date: 25-Feb-16 12:53:07 PM ******/<br />
SET ANSI_NULLS ON<br />
GO<br />
SET QUOTED_IDENTIFIER ON<br />
GO<br />
&#8212; =============================================<br />
&#8212; Author:		&lt;Author,Sibeesh Venu&gt;<br />
&#8212; Create date: &lt;Create Date, 18-Feb-2016&gt;<br />
&#8212; Description:	&lt;Description,To fetch SalesOrderDetail Page Wise&gt;<br />
&#8212; =============================================<br />
ALTER PROCEDURE [dbo].[usp_Get_SalesOrderDetailPage] @pageOffset int=0 AS BEGIN &#8212; SET NOCOUNT ON added to prevent extra result sets from<br />
 &#8212; interfering with SELECT statements.</p>
<p>SET NOCOUNT ON;</p>
<p>WITH CTE_Sales(SlNo, SalesOrderID,SalesOrderDetailID,CarrierTrackingNumber,OrderQty,ProductID,UnitPrice,ModifiedDate) AS<br />
  ( SELECT ROW_NUMBER() over (<br />
                              ORDER BY ModifiedDate DESC) AS SlNo,<br />
                        SalesOrderID,<br />
                        SalesOrderDetailID,<br />
                        CarrierTrackingNumber,<br />
                        OrderQty,<br />
                        ProductID,<br />
                        UnitPrice,<br />
                        ModifiedDate<br />
   FROM dbo.SalesOrderDetail)<br />
SELECT *<br />
FROM CTE_Sales<br />
WHERE SlNo&gt;=@pageOffset<br />
  AND SlNo&lt;@pageOffset+10 END</p>
<p>&#8211;[usp_Get_SalesOrderDetailPage] 4<br />
[/sql]</p>
<p>As you can see in the select query I am using a temporary column <em>SlNo</em> which actually a result of <em>ROW_NUMBER()</em>. So to use this query in a where condition I was forced to use the CTE. Now let us run our stored procedure and see the output.</p>
<p><strong>Output</strong></p>
<div id="attachment_11305" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Stored-Procedure-With-Common-Table-Expression-Or-CTE-e1456392062638.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-11305" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Stored-Procedure-With-Common-Table-Expression-Or-CTE-e1456392062638.png" alt="Stored Procedure With Common Table Expression Or CTE" width="650" height="335" class="size-full wp-image-11305" srcset="/wp-content/uploads/2016/02/Stored-Procedure-With-Common-Table-Expression-Or-CTE-e1456392062638.png 650w, /wp-content/uploads/2016/02/Stored-Procedure-With-Common-Table-Expression-Or-CTE-e1456392062638-300x155.png 300w, /wp-content/uploads/2016/02/Stored-Procedure-With-Common-Table-Expression-Or-CTE-e1456392062638-400x206.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11305" class="wp-caption-text">Stored Procedure With Common Table Expression Or CTE</p></div>
<p><strong>Conclusion</strong></p>
<p>Did I miss anything that you may think which is needed? Did you try CTE in your query? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<p><strong>Your turn. What do you think?</strong></p>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/stored-procedure-with-common-table-expression-or-cte-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
