SQL FUNCTION LIKE SEARCH ISSUE

随声附和 提交于 2020-01-16 19:43:37

问题


please help to fix my sql function!

in my sql function i have one temporary tables

parameter passing to function @searchstring ---- "'%carrots%' AND tmp.ItemDescription like '%baby%'"

Declare  @tempvalues table  (itemdescription nvarchar(max))

@tempvalues  VALUES

-----------------------

CARROTS, BABY, CLASS I, FRESH
CARROTS, BABY, FROZEN
CARROTS, CLASS I, FRESH

------------------

so in function iam passing value for searching data using like query and if matches i need to insert data in another table like

in below temptable if its matching i need to insert 1 as corresponding line

Declare  @tempfinal table  (itemdescription nvarchar(max),matchcontains int)

**insert into @tempfinal select ItemDescription,'1'as matchcontains  from  @tempvalues tmp where tmp.ItemDescription like @searchstring**  

but nothing is inserting in @tempfinal

i need to search all words match

please help to find a solution

SAMPLE FUNCTION

CREATE FUNCTION dbo.UDF_PredictItems_testfreetextdemo  
(  
 @FTSExpression    NVARCHAR(4000) 
)
RETURNS @Result TABLE   
(

  itemdescription  NVARCHAR(4000),
  matchcontains int

)  
AS  
BEGIN           
 Declare  @tempvalues table  (itemdescription nvarchar(max))                    
 insert into @tempvalues values ('CARROTS, BABY, CLASS I, FRESH') ,
 ('CARROTS, BABY, FROZEN 2.5 KGS') ,
 ('CAKE, CARROT, FROZEN 1 KGS') ,
 ('CARROTS, CLASS I, FRESH')  


 declare @containExpression nvarchar(max)
 set @containExpression =replace(replace(replace(@FTSExpression,' ','<>'),'><',''),'<>',' ') 
 set @containExpression=(Select  replace(@FTSExpression, ' ', '%'))   
 set @containExpression=(Select  replace(@containExpression, ' ', ' AND tmp.ItemDescription like ')) 
 set @containExpression=(select '''%'+@containExpression+'%''')  




  declare @tempfinal table(ItemDescription nvarchar(max),matchcontains int)      
  insert into @tempfinal select ItemDescription,CAST('1' AS INT) as matchcontains  from  @tempvalues tmp where tmp.ItemDescription like @containExpression    

   insert into @Result ( itemdescription,matchcontains )           
   select  itemdescription,0 from @tempfinal

   return;
   END

CALLING FUNCTION

SELECT * from UDF_PredictItems_testfreetextdemo('Carrots baby')

回答1:


I've checked and LIKE operator as you described that works fine for me.

Declare  @tempvalues table  (itemdescription nvarchar(max))

INSERT INTO @tempvalues  
VALUES ('CARROTS'), ('BABY'), ('CLASS I'), ('FRESH'),('CARROTSI'), ('BABYI'), ('FROZENI'), ('CARROTSI'), ('CLASS I'), ('FRESH')

DECLARE @searchstring NVARCHAR(50) = '%carrots%'
SELECT * FROM @tempvalues WHERE itemdescription LIKE @searchstring

Output:

CARROTS

CARROTSI

CARROTSI

Check my result here

Updated

So the problem not in LIKE operator, As @Antonios Katopodis's answer, you should insert int instead of varchar type

Updated after fixing

Your problem here

set @containExpression=(select '''%'+@containExpression+'%''')

You should fix by this way

set @containExpression=(select '%'+@containExpression+'%')  




回答2:


Your query is

INSERT INTO @tempfinal
SELECT 
   ItemDescription,
  '1' as matchcontains
FROM @tempvalues tmp
WHERE tmp.ItemDescription like @searchstring**

The problem with this is that you try to insert a char into an int field. So try this:

INSERT INTO @tempfinal
SELECT 
   ItemDescription,
  CAST('1' AS INT) as matchcontains
FROM @tempvalues tmp
WHERE tmp.ItemDescription like @searchstring**

Or

INSERT INTO @tempfinal
SELECT 
   ItemDescription,
  1 as matchcontains
FROM @tempvalues tmp
WHERE tmp.ItemDescription like @searchstring**


来源:https://stackoverflow.com/questions/59541654/sql-function-like-search-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!