字符串处理(模糊顺序搜索)

-----------------------------------
--
Author: htl258(Tony)
--
Title : 字符串处理(模糊顺序搜索)
--
Date : 2009-09-13 20:26:00

-- 引用请保留此信息.
--
---------------------------------

IF NOT OBJECT_ID('TA') IS NULL
DROP TABLE TA
GO
CREATE TABLE TA (ID VARCHAR(3),Title VARCHAR(39))
INSERT INTO TA
SELECT '001','深入浅出SQLServer2005开发管理与应用实例' UNION ALL
SELECT '002','SQLServer2005性能调校' UNION ALL
SELECT '003','SQL完全手册' UNION ALL
SELECT '004','SQLServer2005技术内幕存储引擎' UNION ALL
SELECT '005','SQL宝典' UNION ALL
SELECT '006','SQLServer2000高级管理与开发' UNION ALL
SELECT '007','SQLServer2005数据库开发详解' UNION ALL
SELECT '008','SQLServer2000存储过程与XML编程' UNION ALL
SELECT '009','SQLServer2005系统管理实录' UNION ALL
SELECT '010','SQL技术手册'
GO

--创建自定义函数
IF NOT OBJECT_ID('FN_STR') IS NULL
DROP FUNCTION FN_STR
GO
CREATE FUNCTION FN_STR(@STR VARCHAR(1000))
RETURNS VARCHAR(5000)
AS
BEGIN
DECLARE @r VARCHAR(8000)
SET @r='%'
WHILE LEN(@str)>0
SELECT @r=@r+LEFT(@str,1)+'%',@str=STUFF(@str,1,1,'')
RETURN @r
END
GO
--声明变量并查询
DECLARE @s VARCHAR(200)
SET @s='SQL手册'

SELECT * FROM TA WHERE Title LIKE dbo.FN_STR(@s)
/*
ID Title
---- ---------------------------------------
003 SQL完全手册
010 SQL技术手册
*/

你可能感兴趣的:(sql,编程,xml,搜索引擎,Go)