函数里面包含很多常用代码,适合新手

CREATE  function   fun_get_parametevalue(@typesprc varchar(50),@parameter varchar(10))
returns numeric(18,6)
as
begin
    declare @i  int    
    declare @error int,@errorstr varchar(200)                                                                                                                                                  
    declare @astr varchar(5000),@typestr varchar(2000),@typeno varchar(300),@typename varchar(400)
    declare @pastr varchar(200),@ptypestr varchar(2000) 
    set @error=0                                                                             
    set @typeno='' 
    if(PATINDEX('%[吖-座]%',@typesprc)<>0 or charindex(@parameter,@typesprc collate Chinese_PRC_CS_AS_WS)=0 )
    begin            
        return 1            
    end 
    set @i=1                 
    set @typestr=@typesprc 
    set @astr='' 
    while @i<=6                                                                         
    begin                                                               
        if    charindex('-',@typestr)>0                                                                        
        begin                                                                          
            if @astr='' 
                set @astr=left(@typestr,charindex('-',@typestr)-1)                                                                  
            else                                                                
                set @astr=@astr+'-'+left(@typestr,charindex('-',@typestr)-1)                                                                          
            set @typestr=right(@typestr,len(@typestr)-charindex('-',@typestr))
        end                                                        
        else                                                                           
        begin                                                                
            if @astr='' 
                set @astr=@typestr                                                                
            else                                                                
                set @astr=@astr+'-'+@typestr 
            set @typestr=''                                                                        
        end              
        if exists(select 1 from sa_baojiatype  with (nolock) where   typeno collate Chinese_PRC_CS_AS_WS=@astr)                                                                   
        begin              
            select @typeno=typeno from sa_baojiatype  with (nolock) where typeno collate Chinese_PRC_CS_AS_WS=@astr                             
            set @pastr=@astr         
            set @ptypestr=@typestr         
        end                                                                  
        set @i=@i+1                                  
    end
    if isnull(@typeno,'')<>''
    begin
        declare @id int 
        select @id=a.id from (
            select ROW_NUMBER()over(order by a.autoid) as id,a.fieldname,isnull(a.kxx,0)as kxx ,a.autoid,ysleibie,isnull(a.xxys,0) as xxys,isnull(bjval,0)as bjval,isnull(bjmin,0)as  bjmin
            from sa_baojiatypelist4 a with (nolock) 
            inner join sa_baojiatype b with (nolock) on a.billno=b.billno
            where b.typeno=@typeno  and a.ysleibie>=0 and a.fieldname<>'代码'
        ) a
        where a.fieldname collate Chinese_PRC_CS_AS_WS=@parameter
        if(isnull(@id,'')<>'')
        begin
            set @typestr=@ptypestr
            set @astr=@typestr
            declare @sstr varchar(100),@dstr  varchar(100)
            select @sstr=thfield,@dstr=destfield from  sa_baojiatype with (nolock) where  typeno=  @pastr                          
            if ISNULL(@sstr,'')<>'' and ISNULL(@dstr,'')<>''   
                select @typestr=REPLACE(@typestr,@sstr,@dstr)    
            declare @jj int 
            set @jj=0
            while @jj < @id
            begin
                if(charindex('-',@typestr)>0 )
                begin
                    set @astr=left(@typestr,charindex('-',@typestr)-1)
                    set @typestr=right(@typestr,len(@typestr)-charindex('-',@typestr))
                end
                else
                begin
                    set @astr=@typestr
                    set   @typestr=''  
                end
                set @jj=@jj+1
            end
            set @astr=ltrim(rtrim(@astr))     
            if charindex(@parameter,@astr)>0       
            begin                                            
                if  dbo.StrComp(substring(@astr,1,len(@parameter)),@parameter)=1  and @parameter<>@astr                                 
                    set @astr=substring(@astr,charindex(@parameter,@astr)+len(@parameter),len(@astr)-charindex(@parameter,@astr))                                                                                         
            end
        end
    end
    if (@error=0 and isnull(@astr,'')<>'' and PATINDEX('%[^0-9|.|-|+]%',@astr)=0)
        return  cast(@astr as float(20))
    return 1
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值