SQL 功能类似搜索问题
SQL FUNCTION LIKE SEARCH ISSUE
请帮助修复我的 sql 功能!
在我的 sql 函数中我有一个临时的 tables
传递给函数@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
------------------
所以在函数中,我传递值以使用类似查询搜索数据,如果匹配,我需要将数据插入另一个 table
喜欢
在温度以下table如果它匹配我需要插入1作为相应的行
Declare @tempfinal table (itemdescription nvarchar(max),matchcontains int)
**insert into @tempfinal select ItemDescription,'1'as matchcontains from @tempvalues tmp where tmp.ItemDescription like @searchstring**
但没有在@tempfinal 中插入任何内容
我需要搜索所有匹配的词
请大家帮忙解决
示例函数
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
调用函数
SELECT * from UDF_PredictItems_testfreetextdemo('Carrots baby')
您的查询是
INSERT INTO @tempfinal
SELECT
ItemDescription,
'1' as matchcontains
FROM @tempvalues tmp
WHERE tmp.ItemDescription like @searchstring**
问题在于您试图将 char 插入 int 字段。
所以试试这个:
INSERT INTO @tempfinal
SELECT
ItemDescription,
CAST('1' AS INT) as matchcontains
FROM @tempvalues tmp
WHERE tmp.ItemDescription like @searchstring**
或者
INSERT INTO @tempfinal
SELECT
ItemDescription,
1 as matchcontains
FROM @tempvalues tmp
WHERE tmp.ItemDescription like @searchstring**
我已经检查过 LIKE
您描述的运算符对我来说工作正常。
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
输出:
CARROTS
CARROTSI
CARROTSI
查看我的结果here
已更新
所以问题不在 LIKE
运算符中,正如@Antonios Katopodis 的回答,您应该插入 int
而不是 varchar
type
修复后更新
你的问题在这里
set @containExpression=(select '''%'+@containExpression+'%''')
你应该通过这种方式修复
set @containExpression=(select '%'+@containExpression+'%')
请帮助修复我的 sql 功能!
在我的 sql 函数中我有一个临时的 tables
传递给函数@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
------------------
所以在函数中,我传递值以使用类似查询搜索数据,如果匹配,我需要将数据插入另一个 table 喜欢
在温度以下table如果它匹配我需要插入1作为相应的行
Declare @tempfinal table (itemdescription nvarchar(max),matchcontains int)
**insert into @tempfinal select ItemDescription,'1'as matchcontains from @tempvalues tmp where tmp.ItemDescription like @searchstring**
但没有在@tempfinal 中插入任何内容
我需要搜索所有匹配的词
请大家帮忙解决
示例函数
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
调用函数
SELECT * from UDF_PredictItems_testfreetextdemo('Carrots baby')
您的查询是
INSERT INTO @tempfinal
SELECT
ItemDescription,
'1' as matchcontains
FROM @tempvalues tmp
WHERE tmp.ItemDescription like @searchstring**
问题在于您试图将 char 插入 int 字段。 所以试试这个:
INSERT INTO @tempfinal
SELECT
ItemDescription,
CAST('1' AS INT) as matchcontains
FROM @tempvalues tmp
WHERE tmp.ItemDescription like @searchstring**
或者
INSERT INTO @tempfinal
SELECT
ItemDescription,
1 as matchcontains
FROM @tempvalues tmp
WHERE tmp.ItemDescription like @searchstring**
我已经检查过 LIKE
您描述的运算符对我来说工作正常。
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
输出:
CARROTS
CARROTSI
CARROTSI
查看我的结果here
已更新
所以问题不在 LIKE
运算符中,正如@Antonios Katopodis 的回答,您应该插入 int
而不是 varchar
type
修复后更新
你的问题在这里
set @containExpression=(select '''%'+@containExpression+'%''')
你应该通过这种方式修复
set @containExpression=(select '%'+@containExpression+'%')