March 5, 2014 at 10:41 pm
is there any way to have a stored procedure or function like below c# code:
Comment :"i want to convert this c# code to sql server t-sql "
public static IEnumerable<String> GetWords(Int32 length)
{
if (length <= 0)
yield break;
for (Char c = '!'; c <= '~'; c++)
{
if (length > 1)
{
foreach (String restWord in GetWords(length - 1))
yield return c + restWord;
}
else
yield return "" + c;
}
}
March 6, 2014 at 1:56 am
Try recursive TVF
create function testrec (@l int )
returns @t table
(c varchar(100))
as
begin
if @l > 0
begin
insert @t
select z.c + isnull(x.c,'')
from
(select top (ascii('~') - ascii('!') +1)
char (ROW_NUMBER() over (order by (select null)) + ascii('!') - 1 ) c
from sys.all_columns ) z
left join testrec(@l-1) x on 1=1;
end
return
end
go
select * from testrec (2)
But guess building dynamic plain join may have better perfomance
March 6, 2014 at 1:39 pm
Eskandari,
Sorry but I don't speak C#. What is the desired result?
Are you just trying to identify the strings between "!" and "~" ??
If I were to pass in the string "abc!def~ghi!jkl~mnop" what should the output look like?
I'm asking because the best SQL solution often bears no resemblance to the procedural code found in your C# routine.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
March 6, 2014 at 6:10 pm
Scratch that previous post. Coffee deficit disorder.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
March 6, 2014 at 11:20 pm
OP's c# code generates all the words of given length L consisting of characters from the set {'!',..,'~'} .
When L<=0 it generates empty set of words.
March 7, 2014 at 12:12 am
c# code generates all the words of given length L
March 7, 2014 at 1:39 pm
Are you looking for a solution to this specific problem, or are you looking specifically to use recursion? And do you need all the output words in a table, or in query output?
March 7, 2014 at 7:39 pm
Eskandari (3/5/2014)
is there any way to have a stored procedure or function like below c# code:Comment :"i want to convert this c# code to sql server t-sql "
public static IEnumerable<String> GetWords(Int32 length)
{
if (length <= 0)
yield break;
for (Char c = '!'; c <= '~'; c++)
{
if (length > 1)
{
foreach (String restWord in GetWords(length - 1))
yield return c + restWord;
}
else
yield return "" + c;
}
}
Interesting. If Length=8, then result = 6.1 Quadrillion Values. That makes for one helluva rainbow table. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
March 8, 2014 at 1:41 pm
Like Jeff said, you want a lot of time and disk space to go over 5 in length. However, this problem is what CROSS JOINS in SQL do well.
Just for fun, I threw together a recursive CTE that does a series of CROSS JOINS and compared it to a dynamic SQL solution that builds the appropriate number of CROSS JOINS and runs once. The dynamic SQL solution is by far the fastest. (See attachment.) Someone else may find a more elegant solution. Best of luck.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply