您好我正在使用以下查询。
Create proc [dbo].[usp_getPropertyByWord]
(@Description nvarchar(200))
as
set @Description=replace(@Description,' ',' ')
set @Description=replace(@Description,' ',' ')
create table #temp(name varchar(100))
insert into #temp valueS(@Description)
insert into #temp
SELECT word FROM dbo.splitStringChars(@Description,' ',',')
delete #temp where name in('',' ',',')
create table #temp1(property_id int,property_number nvarchar(15),name nvarchar(500),short_desciption nvarchar(2000),description nvarchar(4000),address1 nvarchar(200),address2 nvarchar(200),city nvarchar(50),locality nvarchar(100),zip nvarchar(8))
insert into #temp1(property_id,property_number,name ,short_desciption,description,address1,address2,city,locality,zip)
SELECT distinct(p.property_id),p.property_number,p.name,p.short_description,p.description,ta.address1,ta.address2,
tc.city_name,cl.City_Locality_Name,ta.zip
FROM tbl_property p
INNER JOIN
tbl_PropertyAddress ta on ta.property_id=p.property_id
inner join tbl_City tc on ta.city=tc.city_id
inner join tbl_City_Locality cl on cl.City_id=tc.city_id
inner join
(SELECT distinct(name) FROM #temp) B
ON (p.name=@Description or p.name LIKE '%' + B.name + '%' or p.short_description like '%' + B.name + '%' or p.description like '%' + B.name + '%'
or ta.address1 LIKE '%' + B.name + '%' or ta.address2 LIKE '%' + B.name + '%' or tc.City_Name LIKE '%' + B.name + '%')
where NOT EXISTS(select t.* from #temp1 t where t.property_id=p.property_id and t.property_number=p.property_number and t.name=p.name and t.short_desciption=p.short_description and t.description=p.description and t.address1=ta.address1 and t.address2=ta.address2 and t.city=tc.City_Name and t.locality=cl.City_Locality_Name and t.zip=ta.zip)
delete #temp
select * from #temp1
GO
如果我为@Description提供“'hyderbad hyd,hgf,hyds'”,我会获得16000条记录,但我的表中只有130条记录。
我从splitStringChars函数中获取值
hyderbad
hyd
hgf
hyds
如何解决这个问题?
答案 0 :(得分:0)
你知道TSQL吗?
Create proc [dbo].[usp_getPropertyByWord]
(@Description nvarchar(200))
as
set @Description=replace(@Description,' ',' ')
set @Description=replace(@Description,' ',' ')
create table #temp(name varchar(100))
insert into #temp valueS(@Description)
insert into #temp
SELECT word FROM dbo.splitStringChars(@Description,' ',',')
delete #temp where name in('',' ',',')
SELECT p.property_id,p.property_number,p.name,p.short_description,p.description
-- ,ta.address1,ta.address2,tc.city_name,cl.City_Locality_Name,ta.zip
FROM tbl_property p
-- INNER JOIN
--tbl_PropertyAddress ta on ta.property_id=p.property_id
--inner join tbl_City tc on ta.city=tc.city_id
--inner join tbl_City_Locality cl on cl.City_id=tc.city_id
inner join
(SELECT distinct(name) FROM #temp) B
ON (p.name=@Description or p.name LIKE '%' + B.name + '%' or p.short_description like '%' + B.name + '%' or p.description like '%' + B.name + '%'
-- or ta.address1 LIKE '%' + B.name + '%' or ta.address2 LIKE '%' + B.name + '%'
-- or tc.City_Name LIKE '%' + B.name + '%'
)
where NOT EXISTS(select t.* from #temp1 t where t.property_id=p.property_id and t.property_number=p.property_number and t.name=p.name and t.short_desciption=p.short_description and t.description=p.description
-- and t.address1=ta.address1 and t.address2=ta.address2 and t.city=tc.City_Name and t.locality=cl.City_Locality_Name and t.zip=ta.zip
)