sql 中 字段解析

CREATE PROCEDURE R_SoghDate
@FilterStr varchar(255)  

AS
--解析查询条件
declare @SubFilter Varchar(255), --  
        @Var Varchar(64),        --字段名
        @Val Varchar(64),      --解析后值
         @SoghDate varchar(10)     --赋值

   WHILE(LEN(@FilterStr)>0)
   begin
     IF CHARINDEX ('and'   , @FilterStr) = 0
     begin
       set  @SubFilter = @FilterStr;
       set @FilterStr = ''
     end
     ELSE
     begin
       set  @SubFilter = LEFT(@FilterStr, CHARINDEX( 'and' ,  @FilterStr )-1 )
       set  @FilterStr = SUBSTRING(@FilterStr, CHARINDEX( 'and', @FilterStr) + 2, LEN(@FilterStr) - CHARINDEX ('and',  @FilterStr) - 2)
     end
   
     set @Var = LEFT(@SubFilter, CHARINDEX( '=', @SubFilter)-1)
     set @Var = RTRIM(LTRIM(@Var))
     set @Val = SUBSTRING(@SubFilter, CHARINDEX( '=', @SubFilter)+1, LEN(@SubFilter) - CHARINDEX ('=',  @SubFilter))
     if CHARINDEX ('|',  @Val) <> 0
        set @Val = RIGHT(@Val, LEN(@Val) - CHARINDEX ( '|' ,  @Val ))
     if CHARINDEX ('|',  @Val) <> 0
        set @Val = LEFT(@Val, CHARINDEX ( '|' ,  @Val )-1)

     --按字段名给变量赋值
     if(@Var = 'SoghDate')
        set @SoghDate = @Val
    end 

你可能感兴趣的:(数据库)