Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 can somone explain the CTE code below

Author  Topic 

summer12in
Starting Member

6 Posts

Posted - 2015-01-09 : 01:46:43
am very new in sql programming .i have 2 questions based on the below mentioned query

WITH CTE_Stars AS
(
SELECT CONVERT(VARCHAR(10), '*') Stars

UNION ALL

SELECT CONVERT(VARCHAR(10), CTE_Stars.Stars+'*') Stars
FROM CTE_Stars
WHERE LEN(Stars) < 6
)
SELECT *
FROM CTE_Stars
Can someone explain how the above query is working and second this query does not work without convert function why..? Why do we need to convert '*' to character? By default * datatype is character only.

This query is giving output in 5 rows and that to without a loop.. how is this possible

Samarjeet

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-09 : 07:00:07
under the covers I select is a loop. Sql generates code that itterates over the dataset. In your case your cte is what is called a recursive CTE. And those recursive calls essentially form the loop. The reason the cast to varchar 10 is to hold a strain greater than one character. If I read it right.

BTW, that's a terrible way to get that job done!
Go to Top of Page

summer12in
Starting Member

6 Posts

Posted - 2015-01-09 : 13:13:09
Great explanation..i got it now ...


Samarjeet
Go to Top of Page
   

- Advertisement -