CREATE FUNCTION  DBO.SFZ_YZ(@SID varchar(100))  
RETURNS bit  AS  
BEGIN
    Declare  @varId bit
    Declare  @I int
    set @varId=1
     if ((len(@SID)=18) or (len(@SID)=15)) 
     begin 
         set @I = 1
         WHILE (@I<= len(@SID) )
         begin              if (@I<18 and  (substring(@SID, @I, 1)<'0' or substring(@SID, @I, 1)>'9'))  
                Set @varId = 0                 if (@I=18 and @varId = 1 and upper(substring(@SID, 18, 1))='X' )
                 Set @varId = 1              
              else  if (@I=18 and (substring(@SID, @I, 1)<'0' or substring(@SID, @I, 1)>'9'))
                 Set @varId = 0                set @I=@I+1
         end           
     end
     else
     begin
         set @varId=0
     end
     RETURN @varId
END

解决方案 »

  1.   

    CREATE OR REPLACE FUNCTION SFZ_YZ(SID IN VARCHAR2)
    RETURN NUMBER IS 
      VARID  NUMBER;
      I      NUMBER;
    BEGIN
      VARID:=1;
      if(length(sid)=18) or(length(sid)=15) then 
        i:=1;
        while(i<length(sid)) loop
           if(i<18 and (substr(sid,i,1)<'0' or substr(sid,i,1)<'9')) then 
             varid:=0;
           end if;
           if(i=18 and varid=1 and upper(substr(sid,18,1))='X') then 
             varid:=1;
           elsif(i=18 and (substr(sid,i,1)<'0' or substr(sid,i,1)>'9' )) then 
             varid:=0;   
           end if;
           I:=I+1;  
        end loop;
      elsif 
        varid:=0;
      end if;
      return varid;
    END SFZ_YZ;
      

  2.   

    条件写错了,应该如下:
    CREATE OR REPLACE FUNCTION SFZ_YZ(SID IN VARCHAR2)
    RETURN NUMBER IS 
      VARID  NUMBER;
      I      NUMBER;
    BEGIN
      VARID:=1;
      if(length(sid)=18) or(length(sid)=15) then 
        i:=1;
        while(i<length(sid)) loop
           if(i<18 and (substr(sid,i,1)<'0' or substr(sid,i,1)>'9')) then 
             varid:=0;
           end if;
           if(i=18 and varid=1 and upper(substr(sid,18,1))='X') then 
             varid:=1;
           elsif(i=18 and (substr(sid,i,1)<'0' or substr(sid,i,1)>'9' )) then 
             varid:=0;   
           end if;
           I:=I+1;  
        end loop;
      elsif 
        varid:=0;
      end if;
      return varid;
    END SFZ_YZ;
      

  3.   

    抱歉,有一个小错误,应如下:
    CREATE OR REPLACE FUNCTION SFZ_YZ(SID IN VARCHAR2)
    RETURN NUMBER IS 
      VARID  NUMBER;
      I      NUMBER;
    BEGIN
      VARID:=1;
      if(length(sid)=18) or(length(sid)=15) then 
        i:=1;
        while(i<length(sid)) loop
           if(i<18 and (substr(sid,i,1)<'0' or substr(sid,i,1)>'9')) then 
             varid:=0;
           end if;
           if(i=18 and varid=1 and upper(substr(sid,18,1))='X') then 
             varid:=1;
           elsif(i=18 and (substr(sid,i,1)<'0' or substr(sid,i,1)>'9' )) then 
             varid:=0;   
           end if;
           I:=I+1;  
        end loop;
      else
        varid:=0;
      end if;
      return varid;
    END SFZ_YZ;
      

  4.   

    倒数第四行 varid:=0; 编译的时候系统提示:出现符号"="在需要下列之一时:.(* @ % & = - + < / > at in      is mod not rem then <an exponent (**) > or != or  .....)
    这是什么问题?
      

  5.   

    可以了  谢谢你!   我们要把SQL全部换成ORACLE  遇到的都是类似这样的问题   好头疼呀!