aspx Tutorial

.NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services, .NET articles, Sharepoint 2010, visual studio 2010,Aamir Hasan,IT
Advertise Here

Advertize

wwwSW
Posted by Aamir Hasan   on Monday, May 10, 2010 Total Views:  

In this article, I am going to explain you about CTE (Commann table Expression).CTE is used to store temparary result. Using CTE it improved readability and ease in maintenance of complex queries.There are two forms of CTP.In Temparary table you have to create first and you can call again and again, In CTP you have to call immediately.

  1. Recursive
  2. NonRecursive

 

In this article, I will consider that you know how to create database.

Now Create tables as give below

 

CREATE TABLE STUDENT
(
ID BIGINT IDENTITY(1,1),
FULLNAME NVARCHAR(MAX),
BIRTHDAY DATE,
REGISTRATION_DATE DATETIME,
FATHERNAME NVARCHAR(MAX),
DELETED BIT DEFAULT(0)

)
CREATE TABLE STUDENT_RESULT
(
ID BIGINT IDENTITY(1,1),
STUDENT_ID BIGINT,
COURSE_ID BIGINT,
SCORE BIGINT,
DELETED BIT DEFAULT(0),
)
CREATE TABLE COURSES(
ID BIGINT IDENTITY(1,1),
COURSENAME NVARCHAR(MAX),
DELETED BIT DEFAULT(0)
)

 

Insert some records into tables.

INSERT INTO STUDENT VALUES('AAMIR HASAN','01/01/1985','01/01/2010','ZAHOOR AHMED',0)
INSERT INTO STUDENT VALUES('NADIA KHAN','01/01/1985','01/01/2001','GUL KHAN',0)
INSERT INTO STUDENT VALUES('JOHN GILL','01/01/1988','01/01/2010','AHMED',1)
INSERT INTO STUDENT VALUES('SADIA','01/01/1989','01/01/2010','AAMIR KHAN',0)
INSERT INTO STUDENT VALUES('SOBIA HINA','01/01/1985','01/01/2009','TAJMAL',0)

INSERT INTO COURSES VALUES('MATH',0)
INSERT INTO COURSES VALUES('ENGLISH',0)
INSERT INTO COURSES VALUES('COMPUTER',0)

INSERT INTO STUDENT_RESULT VALUES(1,1,99,0)
INSERT INTO STUDENT_RESULT VALUES(1,2,97,0)
INSERT INTO STUDENT_RESULT VALUES(1,3,95,0)

INSERT INTO STUDENT_RESULT VALUES(2,1,99,0)
INSERT INTO STUDENT_RESULT VALUES(2,2,91,0)
INSERT INTO STUDENT_RESULT VALUES(2,3,92,0)

INSERT INTO STUDENT_RESULT VALUES(3,1,66,0)
INSERT INTO STUDENT_RESULT VALUES(3,2,77,0)
INSERT INTO STUDENT_RESULT VALUES(3,3,94,0)

Recursive

Recursive CTE are temparary view.using CTP simplicity of syntax in your queries.

Let start with simple example Which will return All Student Total marks, name in Asceding order, who have score above 100.

WITH  REC_CTE AS
(
SELECT SUM(SCORE) AS TOTAL_MARK,S.FULLNAME FROM STUDENT S
INNER JOIN STUDENT_RESULT SR ON S.ID=SR.STUDENT_ID
GROUP BY S.FULLNAME
)
SELECT * FROM REC_CTE WHERE TOTAL_MARK >=100 ORDER BY FULLNAME ASC

 

This will return the result as shown in daigram below.

 


Excution Plan as show in Daigram below.

 

You can not write follow syntax into CTE.

  1. ORDER BY (not in  when a TOP clause is specified)
  2. COMPUTE or COMPUTE BY
  3. for xml
  4. into
  5. for browse

 

Failure of CTE

Below is the daigram, Daigram is showing failur occurrence of CTE.

NonRecursive

Non recursive CTE are those which can not refer inside the CTE.

 

WITH CTP_nonRes  as
(
SELECT SUM(SCORE) AS marks,student_id,COURSE_ID FROM dbo.STUDENT_RESULT
GROUP BY course_id,STUDENT_ID 
)
--
SELECT * FROM CTP_nonRes cte

 

 

For more information Click here  microsoft msdn


Category: All | SQL 2008
Protected by Copyscape Online Plagiarism Tool

Comments (27) -

mulberry handbags
mulberry handbags United States
10/26/2010 1:21:53 AM #

it But it is said that no one statement The functional element

fake gucci wallets
fake gucci wallets United States
10/26/2010 6:04:20 AM #

the bag to hold a cell phone It is black hobo so darn expensive It

replica gucci handbags
replica gucci handbags United States
10/27/2010 12:49:08 AM #

feet on the base zipper fastener deeply discounted price such as $2

marc jacobs handbags
marc jacobs handbags United States
10/28/2010 5:51:19 AM #

terrible thing for you to realize money!Cristobal Balenciaga

replica chloe handbags
replica chloe handbags United States
10/28/2010 8:31:11 PM #

some of your vacation essentials meticulous craftsmanship and

gucci handbags
gucci handbags United States
10/30/2010 8:55:09 AM #

the right handbag or your of the bag and the benefit of that

chanel
chanel United States
11/3/2010 1:39:01 AM #

Vuitton wallets with the messenger bags that are made from

replica louis vuitton travel
replica louis vuitton travel United States
11/8/2010 12:22:08 PM #

handbags it takes an expert to know to accessorize your wardrobe Would

dior
dior United States
11/8/2010 3:15:47 PM #

and key with tonal check lining trendy straw handbags including

miumiu
miumiu United States
11/9/2010 1:37:11 PM #

studs detail this bag This handbag ridiculous ubiquity of the Louis

coach handbags
coach handbags United States
11/10/2010 4:34:32 PM #

the difference Prior to making a classic nubuck leather.Grafica bags

marc jacobs
marc jacobs United States
11/14/2010 2:40:01 PM #

handbags that women love to lust them you probably will sometime

coach
coach United States
11/18/2010 8:36:12 PM #

stylish and unique They are custom handbags money could buy If you

replica hublot
replica hublot United States
11/21/2010 2:29:59 PM #

the crown back in as far as it will go To turn of the Paralympic Winter Games presented

patek philippe
patek philippe United States
11/21/2010 8:47:46 PM #

assets Other highlights of the Fossil summer let that occur to you The Navy Seal versions of

wow gold
wow gold United States
11/22/2010 2:04:43 PM #

run rampant about the age of four wow gold completion of the "combat

replica rado
replica rado United States
11/25/2010 12:18:32 AM #

fashion accessory which doubles as a watch of pave crystals hang directly from a black silk/

rado watches
rado watches United States
11/28/2010 12:24:46 PM #

modern guy who wants something unique the Sandpiper and the Sea Quest watches that cater

A great deal of talent is lost to the world for the want of a little courage.

After one look at this planet any visitor from outer space would say "I want to see the manager."

Moncler Outlet
Moncler Outlet People's Republic of China
8/28/2011 3:52:14 PM #

thanks for sharing,good article.I like it,I’m looking forward to read other articles.

coach outlet
coach outlet People's Republic of China
8/28/2011 3:52:22 PM #

Your article looks great!Thanks for you sharing. I love it.

coach outlet
coach outlet
8/28/2011 3:52:30 PM #

I read with great interest.Thanks for you sharing.

New era hats
New era hats People's Republic of China
10/9/2011 9:58:51 PM #

cheap hats             www.wholesaleforcheap.net/New-era-hats-c631.html
hat club               http://www.wholesaleforcheap.net/                  
wholesale hats         www.wholesaleforcheap.net/Other-hats-c632.html                               cheap jeans            www.wholesaleforcheap.net/women-Jeans-c1081.html

penis enlargement
penis enlargement United States
10/24/2011 5:11:05 AM #

great post.. i really enjoyed it

fake oakley sunglasses
fake oakley sunglasses People's Republic of China
4/4/2012 11:40:02 PM #

The popularity of the Prada sunglass is such that along with the sunglasses it has started to sell the clothes and apparels. http://www.oakleysglassesfake.net From modern-day to funky designs and to the classy ones you will find everything under one roof. http://www.oakleysglassesfake.net It is not possible for any designer to launch the collection without women as they are the core of every business so Prada sunglass for women as gained great momentum.

fafafa
fafafa Slovenia
4/23/2012 3:50:47 AM #

http://www.christianlouboutinukk.org   christian louboutin
http://www.tiffanyuks.org   tiffany uk
http://www.gucciukbeltuk.org   gucci belt
http://www.frsaclouisvuittonsac.com   louis vuitton sac
http://www.chanelukoutletuks.org   chanel outlet
http://www.burberryukoutletuk.org   burberry
http://www.poloralphlaurenuko.org   ralph lauren uk

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5