2011-12-21 9 views
7

Ho bisogno di una funzione in sql server per creare tutte le parole modificate nell'esempio seguente; per la parola di input con lunghezza n deve creare 2^n parola modificata; Ad esempio, se l'ingresso della funzione ècome creare 2^n parola cambiata da word con lunghezza n in sql server

"I" 

l'output della funzione dovrebbe essere

I 
- 

l'ingresso della funzione è

"am" 

l'uscita della funzione dovrebbe essere

am 
-m 
a- 
-- 

l'ingresso della funzione è

"sql" 

l'output della funzione deve essere

sql 
-ql 
s-l 
sq- 
--l 
s-- 
-q- 
--- 

risposta

9

Si può fare questo con un tavolo numeri (master..spt_values) e stuff in un ciclo.

declare @Word varchar(10) = 'sql' 

declare @T table 
(
    Word varchar(10) 
) 

insert into @T values (@Word) 

while not exists(select * 
       from @T 
       where Word = replicate('-', len(@Word))) 
begin    
    insert into @T(Word) 
    select distinct stuff(T.Word, N.number, 1, '-') 
    from @T as T 
    cross join 
     master..spt_values as N 
    where N.type = 'P' and 
     N.number between 1 and len(@Word) and 
     stuff(T.Word, N.number, 1, '-') not in (select Word from @T) 
end   

select * 
from @T 

http://data.stackexchange.com/stackoverflow/q/122334/

Oppure si può utilizzare un reqursive CTE

declare @Word varchar(10) = 'sql' 

;with C as 
(
    select @Word as Word, 
     0 as Iteration 
    union all 
    select cast(stuff(Word, N.number, 1, '-') as varchar(10)), 
     Iteration + 1 
    from C 
    cross join 
     master..spt_values as N 
    where N.type = 'P' and 
     N.number between 1 and len(@Word) and 
     Iteration < len(@Word) 
) 
select distinct Word 
from C 

http://data.stackexchange.com/stackoverflow/q/122337/

Aggiornamento

La versione ricorsiva CTE è molto lento come POI inserito da OP in un commento. Usando una parola con 7 lettere ci sono 960800 righe restituite dal CTE.

+0

+1 e hai ottenuto l'output in modo che il PO vuole –

+0

@ConradFrix - L'ordine non era :) intenzionale. –

+2

seconda risposta è buona, ma il tempo per la stringa di input "esempio" è 28 secondi, la prima risposta è molto buona e il tempo per la stringa di input "example" è 0 secondi – jozi

6

Questo CTE ricorsiva

declare @input varchar(25) 

set @input = 'SQL' 
;WITH cte 
    AS (SELECT Stuff(@input, v.NUMBER, 1, '-') OUTPUT, 
       0        LEVEL 
     FROM MASTER..spt_values v 
     WHERE TYPE = 'P' 
       AND NUMBER BETWEEN 1 AND Len(@input) 
     UNION ALL 
     SELECT Stuff(cte.OUTPUT, v.NUMBER, 1, '-') OUTPUT, 
       cte.LEVEL + 1      AS LEVEL 
     FROM MASTER..spt_values v, 
       cte 
     WHERE TYPE = 'P' 
       AND cte.LEVEL + 1 < Len(@input) 
       AND NUMBER BETWEEN 1 AND Len(@input)) SELECT DISTINCT OUTPUT 
FROM cte 
UNION 
SELECT @INPUT 
ORDER BY OUTPUT 

produce il seguente output

---  
--l  
-q-  
-ql  
s--  
s-l  
sq-  
sql 

Lascio a voi per mettere in una funzione.

vederlo lavorare a questo data.se query

+0

Devo solo fare +1 su questo :) –

+0

è molto buono ma per "esempio" stringa di input 35 secondi è in esecuzione – jozi

Problemi correlati