|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 9:59 AM
Points: 236,
Visits: 149
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
Nice brain teaser of a question. Wow!
Though aren't the "None" and "0" choices the same? (I assume that 0 rows is no rows.)
Did it really need to be that complicated?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, April 11, 2013 12:58 AM
Points: 174,
Visits: 555
|
|
| I replied "None" because my mind was lost in the recursion and logic.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 1:13 AM
Points: 1,720,
Visits: 746
|
|
Nice question..
Complex select statements ..so I ignored it and guessed the answer with the where condition.
Regards, Rals.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Tuesday, January 01, 2013 11:12 PM
Points: 248,
Visits: 372
|
|
It is very difficult to understand... and not able to get logic behind this query.. 
KSB ----------------------------- Thousands of candles can be lit from a single candle, and the life of the candle will not be shortened. Knowledge and happiness never decreases by being shared.” - Buddha
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 1:32 AM
Points: 3,187,
Visits: 4,140
|
|
This is one of several examples of practical use of Tally table As for me, this is one of several the thousands examples of totally unformatted and obscure code. An example that would be convenient for the 'how-to-not-format-your-code' guide
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 4:56 AM
Points: 1,256,
Visits: 4,253
|
|
I selected None because I didn't understand the code and was pretty sure it would return a syntax error...I'm hoping the author was deliberately going for obfuscation here, because if he normally writes code like this then he could be in trouble! 
Oh, and I wouldn't say that "none" and "0" are the same--to my mind, "none" means the code threw an error while "0" means it works but just doesn't return any rows.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 11:51 AM
Points: 5,232,
Visits: 7,023
|
|
I got it right, by focusing on the WHERE clause (that alone dictates the number of rows), and hoping that there was no parentheses mismatch or similar error buried in the code.
The code seems overly complicated to me. While I agree that it is "one of several examples of practical use of Tally table.", I would much rather see a GOOD example of practical use of a Tally table.
Here is a much simpler and easier to understand way to get the same results. Note that I adapted this code from a snippet taken from Erland Sommarskog's website.
DECLARE @Text NVARCHAR(500); DECLARE @StringDelimiter CHAR(1);
SET @Text = 'This T-SQL will split these sentences into rows.' + 'How many rows will be returned?.' + 'M.a.y.b.e..n.o.n.e.?'; SET @StringDelimiter = '.';
-- Add delimiter before and after text; -- This removes the need for special code to handle start and end of string. DECLARE @TextPlus NVARCHAR(502) SET @TextPlus = @StringDelimiter + @Text + @StringDelimiter;
WITH Tally(Number) AS (SELECT 1 AS Number UNION ALL SELECT Number + 1 AS Number FROM Tally WHERE Number < LEN(@TextPlus)) SELECT SUBSTRING(@TextPlus, Number + 1, CHARINDEX(@StringDelimiter, @TextPlus, Number + 1) - Number - 1) AS SPLIT FROM Tally WHERE Number <= LEN(@TextPlus) - 1 AND SUBSTRING(@TextPlus, Number, 1) = @StringDelimiter ORDER BY Number OPTION (MAXRECURSION 0);
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 3:53 PM
Points: 1,060,
Visits: 4,162
|
|
I think this is an example of why people don't like to use OVER, since it's so hard to work out what might/will be returned! I'd like to see a simpler question or articles on how OVER works - any takers?
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: 2 days ago @ 3:21 AM
Points: 1,843,
Visits: 971
|
|
hugo explanation is good
Malleswarareddy I.T.Analyst MCITP(70-451)
|
|
|
|