create function fun_getygqxisexist(@vYgbm varchar(15),@vQxbm varchar(15)) 
returns int
as
begin
  declare @Sql nvarchar(4000)
  declare @count int
  declare @da int
  declare @yg int    select @da=count(f_qxbm) from tbda_qx where f_qxbm like @vQxbm+'%' 
    select @yg=count(f_qxbm) from tbqx_yg where f_ygbm=@vYgbm and f_qxbm like @vQxbm+'%'
    select @count=case when @da=@yg then 2 when @yg=0 then 0 else 1 end
  return(isnull(@count,0))
end意图是like后面是否需要加引号,还是像上面这样就是正确的

解决方案 »

  1.   

    create function fun_getygqxisexist(@vYgbm varchar(15),@vQxbm varchar(15)) 
    returns int
    as
    begin
      declare @Sql nvarchar(4000)
      declare @count int
      declare @da int
      declare @yg int    select @da='count(f_qxbm) from tbda_qx where f_qxbm like '''+ @vQxbm+'%''' 
        select @yg='count(f_qxbm) from tbqx_yg where f_ygbm=@vYgbm and f_qxbm like '''+ @vQxbm+'%'''
        select @count=case when +@da=@yg then 2 when @yg=0 then 0 else 1 end
      return(isnull(@count,0))
    end
      

  2.   

    --我錯了, 樓主的SQL是正確的
      

  3.   

    正确
    ps:
    在存储过程里面需要动态组建sql语句时需要后面加引号
      

  4.   

    写法没错. 如果从任意处匹配的话like '%' + @vQxbm + '%'