set@i=0 while @i<=15 begin set@j=15-@i /* 当前岸 */ if ((@i>=8) ornot ((@i&6=6) or (@i&3=3))) and /* 对岸 */ ((@j>=8) ornot ((@j&6=6) or (@j&3=3))) begin insert STATE values (@i) end set@i=@i+1 end
insert PATH select S1.state, S2.state, "" from STATE S1, STATE S2 where ( (S1.state &~S2.state >=8) and (S2.state &~S1.state =0) and ( (S1.state &~S2.state &7=0) or (S1.state &~S2.state &7=1) or (S1.state &~S2.state &7=2) or (S1.state &~S2.state &7=4) ) ) or ( (S2.state &~S1.state >=8) and (S1.state &~S2.state =0) and ( (S2.state &~S1.state &7=0) or (S2.state &~S1.state &7=1) or (S2.state &~S1.state &7=2) or (S2.state &~S1.state &7=4) ) )
/* 架构名.函数名 */ createfunction dbo.to_zh( @num1int ) returnsvarchar(100) as begin declare@ans1varchar(100) declare@ans2varchar(100) declare@discvarchar(10) declare@indexvarchar(10) set@disc= "人狼羊菜" set@index= "8421" set@ans1= "" set@ans2= "" declare@iint set@i=1 while(@i<=4) begin if ((@num1& (ASCII(SUBSTRING(@index, @i, 1)) - ASCII("0"))) >0) begin set@ans1=@ans1+SUBSTRING(@disc, @i, 1) end else begin set@ans2=@ans2+SUBSTRING(@disc, @i, 1) end set@i=@i+1 end return@ans1+ "|" +@ans2 end
取出一个结果进行输出
master..spt_values 是系统中一个表
1 2 3 4 5 6 7 8 9 10
select dbo.to_zh(M.item) from ( selectSUBSTRING(ans.path, number, CHARINDEX(',', ans.path +',', number) - number) as item from ANSWER ans, master..spt_values /* 查第 1 条记录*/ where ID =1 and number >=1 and number < len(ans.path) and type ='p' andSUBSTRING(','+ ans.path, number, 1)=',' ) M