Home > Database > Mysql Tutorial > Sql Server 2005中查询用分隔符分割的内容中是否包含其中一个内

Sql Server 2005中查询用分隔符分割的内容中是否包含其中一个内

WBOY
Release: 2016-06-07 18:06:31
Original
1012 people have browsed it

Sql Server 2005中查询用分隔符分割的内容中是否包含其中一个内容,多个朋友给予的参考。

今天做项目遇到一个问题,

有产品分类A,B,C顶级分类,

期中A下面有a1,a2,a3子分类.

但是a1可能共同属于A和B,然后我的数据库是这样设计的

如果想要查询A的所有子类的话就要查询parent中包含1的,所以就萌生了这个办法.呵呵,

暂时没什么好办法,欢迎拍砖~~

另外,这个东西我想弄成bool返回值的,哪位兄弟会的希望帮忙解决一下....
代码如下:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
*/
ALTER Function [dbo].[HasKey](@DataStr varchar(100),@key varchar(8))
returns int
as
begin
declare @return_str varchar(50)
declare @isok bit
declare @posi int
set @isok = 0
--set @DataStr = 'a,aaaa,aaaddd,ffass'
--set @key = 'aaaa'
set @DataStr = @DataStr+','
while len(@DataStr) > 0 and @isok=0
begin
set @posi= charindex(',',@DataStr)
set @return_str = replace(substring(@DataStr,1,charindex(',',@DataStr)),',','')
set @DataStr = substring(@DataStr,@posi+1,len(@DataStr))
if(@return_str=@key)
begin
set @isok=1
break
end
end
return @isok
end
GO

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template