码迷,mamicode.com
首页 > 数据库 > 详细

c#.net+SQL2005 实现简单搜索

时间:2019-10-06 09:16:54      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:search   sql   replace   sea   data   arc   arch   insert   har   

USE [DATABASE]<br />
GO<br />
SET ANSI_NULLS ON<br />
GO<br />
SET QUOTED_IDENTIFIER ON<br />
GO<br />
CREATE function [dbo].[search]<br />
(@Word nvarchar(max),<br />
@Phrase nvarchar(max))<br />
returns smallint<br />
as<br />
begin<br />
if @Word is null or @Phrase is null return 0<br />
DECLARE @BiggerWord VARCHAR(max)<br />
SELECT @BiggerWord = @Word + ‘x‘<br />
DECLARE @BiggerPhrase VARCHAR(max)<br />
SELECT @BiggerPhrase = REPLACE (@Phrase, @Word, @BiggerWord)<br />
RETURN LEN(@BiggerPhrase) - LEN(@Phrase)<br />
END

利用以上函数创建存储过程,提供3个参数查询条件

USE [DATABASE]<br />
GO</p>
<p>SET ANSI_NULLS ON<br />
GO<br />
SET QUOTED_IDENTIFIER ON<br />
GO<br />
CREATR PROCEDURE [dbo].[SearchDog]<br />
( </p>
<p> @Word1 nVARCHAR(max)=null,<br />
@Word2 nVARCHAR(max)=null,<br />
@Word3 nVARCHAR(max)=null)<br />
AS<br />
DECLARE @Dog TABLE<br />
(<br />
DogHead nvarchar(max),<br />
DogBody nVARCHAR(max),<br />
Rank INT)</p>
<p> INSERT INTO @Dog<br />
SELECT DogHead, DogBody,<br />
3 * dbo.search(@Word1, DogHead) + dbo.search(@Word1, DogBody) +<br />
3 * dbo.search(@Word2, DogHead) + dbo.search(@Word2, DogBody) +<br />
3 * dbo.search(@Word3, DogHead) + dbo.search(@Word3, DogBody)<br />
AS Rank<br />
FROM Dog ORDER BY Rank DESC</p>
<p>SELECT DogHead, DogBody, Rank FROM @Dog<br />
WHERE Rank > 0<br />
ORDER BY Rank DESC

c#.net+SQL2005 实现简单搜索

标签:search   sql   replace   sea   data   arc   arch   insert   har   

原文地址:https://www.cnblogs.com/yangfang777/p/11626436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!