--创建函数 create function [dbo].[funcomparestring] ( @stra nvarchar(200), @strb nvarchar(200) ) returns int as begin declare @strbase nvarchar(200) declare @rank int select @rank=0 if len(@stra)>len(@strb) select @rank=count(*) from funsplitchar(@strb) where item in(select item from funsplitchar(@stra)) else select @rank=count(*) from funsplitchar(@stra) where item in(select item from funsplitchar(@strb)) return @rank end go --创建第二种函数 create function [dbo].[funcomparestring_new] ( @stra nvarchar(200), @strb nvarchar(200) ) returns int as begin declare @strbase nvarchar(200) declare @rank int select @rank=0 if len(@stra)>len(@strb) select @rank=count(*) from funsplitchar(@strb) where item in(select distinct item from funsplitchar(@stra)) else select @rank=count(*) from ( select distinct * from funsplitchar(@stra) where item in(select distinct item from funsplitchar(@strb)) ) bb return @rank end --以上两个函数有什么不同呢?下面我用个例子来给大家说明一下: --测试示例 select [dbo].[funCompareString]('中国Chinese之家','中国人是Chinese') --结果为:9 select [dbo].[funCompareString_new]('中国Chinese之家','中国人是Chinese') --结果为:8 --在这两个字符串中,'ese'与'ese'的重复在第一个函数算个字符重复, --而在第二个函数中算个字符重复。 --也就是说在第二个函数中,多次相同的重复不累积计算例如ese中的e。 特别说明: 如果数据量比较大,尽量避免使用自定义函数,以免严重影响性能。 本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/maco_wang/archive/2011/03/16/6254971.aspx