34,575
社区成员
发帖
与我相关
我的任务
分享
declare @s varchar(10),@as char(1),@s1 char(1),@i int,@j int
select @s = 'bcdef',@as = 'a'
declare @t table(s varchar(20))
declare @t1 table(s varchar(20))
select @i = 1,@s = left(@s,len(@s)-1)
while @i <=len(@s)
begin
insert @t1 select substring(@s,@i,1)
set @i = @i +1
end
insert @t select * from @t1 where s <> left(@s,1)
set @i = 1
while @i <len(@s)
begin
insert @t select b.s+a.s from @t1 a,@t b where charindex(a.s,b.s)=0 and a.s <> substring(@s,@i+1,1)
delete @t where len(s)=@i
set @i = @i +1
end
update @t set s = s +@as
select * from @t
/*
s
--------------------
edcba
decba
cdeba
edbca
debca
dbeca
cebda
ebcda
cbeda
*/
declare @t table(col varchar(2))
insert @t select 'b'
insert @t select 'c'
insert @t select 'd'
select a.col + '-'+ b.col+'-'+c.col+'-a'
from @t a,@t b,@t c
where a.col <> b.col and b.col <> c.col and a.col <> c.col
and a.col <> 'b'
and b.col <> 'c'
and c.col <> 'd'
/*
-----------
c-d-b-a
d-b-c-a
(所影响的行数为 2 行)
*/
declare @t table(col varchar(2))
insert @t select 'a'
insert @t select 'b'
insert @t select 'c'
insert @t select 'd'
select a.col + '-'+ b.col+'-'+c.col+'-'+d.col
from @t a,@t b,@t c,@t d
where a.col <> b.col and b.col <> c.col and c.col <> d.col and a.col <> c.col and a.col <> d.col and
b.col <> d.col and d.col = 'a'
and a.col <> 'b'
and b.col <> 'c'
and c.col <> 'd'
/*
b-c-d-e
-----------
c-d-b-a
d-b-c-a
(所影响的行数为 2 行)
*/
declare @t table(col varchar(2))
insert @t select 'a'
insert @t select 'b'
insert @t select 'c'
insert @t select 'd'
insert @t select 'e'
select
a.col+'-'+b.col+'-'+c.Col+'-'+d.Col
from
@T a,@T b,@T c,@T d
where
d.Col='a'
and
c.Col<>d.Col
and
b.Col<>d.Col and b.Col<>c.Col
and
a.Col<>b.Col and a.COl<>c.Col and a.COl<>d.Col
declare @t table(col varchar(2))
insert @t select 'a'
insert @t select 'b'
insert @t select 'c'
insert @t select 'd'
insert @t select 'e'
select a.col + '-'+ b.col+'-'+c.col+'-'+d.col
from @t a,@t b,@t c,@t d
where a.col <> b.col and b.col <> c.col and c.col <> d.col and a.col <> c.col and a.col <> d.col and
b.col <> d.col and d.col = 'a'
/*
-----------
b-d-c-a
b-e-c-a
b-c-d-a
b-e-d-a
b-c-e-a
b-d-e-a
c-d-b-a
c-e-b-a
c-b-d-a
c-e-d-a
c-b-e-a
c-d-e-a
d-c-b-a
d-e-b-a
d-b-c-a
d-e-c-a
d-b-e-a
d-c-e-a
e-c-b-a
e-d-b-a
e-b-c-a
e-d-c-a
e-b-d-a
e-c-d-a
(所影响的行数为 24 行)
*/