1. 程式人生 > >詳解如何使用SQL中文分片語件查詢表記錄

詳解如何使用SQL中文分片語件查詢表記錄

CREATEPROCEDURE [dbo].[Search_Test]@Keywordnvarchar(50)=''ASBEGINSET NOCOUNT ONDECLARE@ConditionNVARCHAR(1000) SET@Condition='1 = 1'DECLARE@TablesVARCHAR(100) SET@Tables='[vw_tmp] a 'DECLARE@SqlNVARCHAR(4000) SET@Sql=''IF@Keyword<>''BEGINdeclare@Filterasvarchar(max) declare@Str
asvarchar(1000) ifright(@Keyword,1)<>'/'set@Str=@Keyword'/'elseset@Str=@Keywordset@Filter=''declare@Chasvarchar(100) while(@Str<>'') beginset@Ch=left(@Str,charindex('/',@Str,1)-1) set@Str=stuff(@Str,1,charindex('/',@Str,1),'')
set@Filter=@Filter' or [product_tag] LIKE ''%'@Ch'%'''endifcharindex('/',@Keyword,1)>0set@Keyword=replace(@Keyword,'/','') SET@Condition=' (a.[product_tag] LIKE ''%'@Keyword'%'''@Filter' )'ENDSET@Sql=@Sql' SELECT top 1 a.* FROM '@Tables' WHERE
'@Condition'order by CHARINDEX('''@Keyword''', product_tag, 1) desc'--排序根據最匹配的內容放在最前面print@sqlEXEC sp_executesql @Sql, N'@keyword nvarchar(500)=NULL', @KeywordEND