Demo entry 6780132

oho

   

Submitted by anonymous on Dec 23, 2018 at 07:44
Language: SQL. Code size: 2.4 kB.

create function decompose_function(@y varchar(255))
returns @fdsets TABLE (fdname varchar(20),L varchar(10),R varchar(10),lv int) as
BEGIN
      declare @i int --循环次数
      declare @j int --取字符的个数
      declare @k int --取字符的位置
      declare @t varchar(20) --临时存放字符变量
      declare @fd varchar(20) --存放一个函数依赖的变量
      declare @len int        --输入函数依赖集的字符总数
      declare @text varchar(255)
      
      declare @row int   --表的行数
      declare @a varchar(20) --临时存放字符变量
      declare @b varchar(20) --临时存放字符变量
      declare @c varchar(20) --临时存放字符变量
      declare @s varchar(2)  --临时存放符号
      
      set @text=@y
      set @i=0
      set @j=0
      set @k=1
      set @len=(select LEN(@text))
      
      while @i<=@len
         begin
           set @i=@i+1;
           set @t=SUBSTRING(@text,@i,1)
           if @t='→'
             begin
              declare @fl char(10)
              declare @i1 int
              set @fl=SUBSTRING(@text,@k,@j)
              set @i1=@i+1
             end
           if @t=',' 
             begin
               
              insert into @fdsets(fdname,L,R,LV) 
              values(SUBSTRING(@text,@k,@j),@fl,SUBSTRING(@text,@i1,@j-LEN(@fl)-1),1)
              
              set @k=@i+1
              set @j=0
             end
           else 
             begin 
              set @j=@j+1
             end
             
         end
      insert into @fdsets(fdname,L,R,LV) 
      values(SUBSTRING(@text,@k,@j),@fl,SUBSTRING(@text,@i1,@j-LEN(@fl)-1),1)

Level_2:   --将函数依赖的右部简化为单属性

      declare div2 cursor local SCROLL for --用cursor_row的话要有scroll
      select L,R from @fdsets where lv=1 --定义的一层函数依赖的游标     
      open div2
      declare @count int
      set @count=0
      
      while (@count<@@CURSOR_ROWS)
      begin
        fetch  next from div2 into @b,@c
        declare @len1 int
        set @len1=len(@b)
        print @len1
        set @row=LEN(@c)
        set @i=0
        set @s='→'

        while @i<@row
           begin
              set @i=@i+1;
              set @a=(SUBSTRING(@b,1,@len1)+@s+SUBSTRING(@c,@i,1))
              insert into @fdsets values (@a,@b,SUBSTRING(@c,@i,1),2)
           end
        set @count=@count+1

      
      end
      close div2
      deallocate div2   
      

return   
end

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).