好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SQL之存储过程增加模糊搜索和条件搜索

-- 简单演示

首先,讲下我这里表内容的意思,取自企业客户的iPlanDefault表内容,包含自制,采购,委外,是属于类型,客户需要报表显示出来,

我做了cast when else  条件判断, 这里就不过多说

create procedutre 自定义存储过程的名字,申明 @cinvcode和@invname参数

 create procedure gzhh_invcost 
(@cinvcode nvarchar(  60 )= ‘‘ ,@invname nvarchar( 60 )= ‘‘  )
  as   
begin
IF not object_id(N  ‘  tempdb..##tmp_inv  ‘  ) IS NULL DROP TABLE ##tmp_inv
  select  inv.cinvcode,cinvname+cInvStd  as  nameStd,iPlanDefault, 1  ilevel , inv.cinvcode cinvcodex,inv.iPlanDefault iPlanDefaultx,cast( 1.00   as   float  ) qty
,cast(  0.00   as   money) cost 
,  case  when inv.iPlanDefault= 1  then isnull(cidefine1, 0 )+isnull(cidefine2, 0 )  else   0   end costx
into ##tmp_inv
  from   inventory inv 
left join inventory_extradefine ine on inv.cinvcode = ine.cinvcode 
  where  inv.cInvName like  ‘  %  ‘ +@invname+ ‘  %  ‘  and inv.cinvcode like  ‘  %  ‘ +@cinvcode+ ‘  %  ‘  or (inv.cinvcode= @invname)
end 

 

 执行存储过程

传一个参数,

exec gzhh_invcost ‘11254040004‘,‘‘

  

 

 

 不传参数

  exec gzhh_invcost ‘‘,‘‘

 

 

 

 

方法:把一种条件搜索,一种不加条件模糊搜索,or满足其中一种就可执行  ,而‘%‘+@invname+‘%‘,加了引号,其实sql编译,字符串解析格式

 where  inv.cInvName like  ‘  %  ‘ +@invname+ ‘  %  ‘  and inv.cinvcode like  ‘  %  ‘ +@cinvcode+ ‘  %  ‘  or (inv.cinvcode=@invname)

 



SQL之存储过程增加模糊搜索和条件搜索

标签:内容   null   字段   inf   html   var   obj   span   ext   

查看更多关于SQL之存储过程增加模糊搜索和条件搜索的详细内容...

  阅读:43次