日期:2014-05-17 浏览次数:20583 次
create table tb(id int,value varchar(30))
insert into tb values(1,'KM,GD,XS,LD')
insert into tb values(2,'KM,GD,XS,CG,LD,JK,GX')
go
if not exists(
select 1 from
(
SELECT a.id, B.value
FROM(
SELECT id, [value] = CONVERT(xml,'<root><v>' + REPLACE([value], ',', '</v><v>') + '</v></root>') FROM tb
)A
OUTER APPLY(
SELECT value = N.v.value('.', 'varchar(100)') FROM A.[value].nodes('/root/v') N(v)
)B
) t
where id = 1 and
value not in
(
select value from
(
SELECT a.id, B.value
FROM(
SELECT id, [value] = CONVERT(xml,'<root><v>' + REPLACE([value], ',', '</v><v>') + '</v></root>') FROM tb
)A
OUTER APPLY(
SELECT value = N.v.value('.', 'varchar(100)') FROM A.[value].nodes('/root/v') N(v)
)B
) t where id = 2
)
)
begin
print '全部包含';
end
else
begin
print '没有全部包含';
end
CREATE FUNCTION JText(@a1 VARCHAR(100),@a2 VARCHAR(100))
RETURNS BIT
AS
BEGIN
DECLARE @Min VARCHAR(100),@Max VARCHAR(100),@Tmp VARCHAR(20),@Bit BIT
SET @Bit=1
IF LEN(@a1)<LEN(@a2)
SELECT @Min=@a1,@Max=@a2
ELSE
SELECT @Min=@a2,@Max=@a1
SELECT @Min=@Min+',',@Max=@Max+ ',', @Tmp=LEFT(@Min,CHARINDEX(',',@Min))
WHILE @Tmp<>''
BEGIN
IF CHARINDEX(@Tmp,@Max)=0
BEGIN
SET @Bit=0
BREAK
END
SELECT @Min=REPLACE(@Min,@Tmp,''), @Tmp=LEFT(@Min,CHARINDEX(',',@Min))
END
RETURN @Bit
END
GO
DECLARE @a1 VARCHAR(100) ,@a2 VARCHAR(100)
SET @a1='KM,GD,XS,LD'
SET @a2='KM,GD,XS,CG,LD,JK,GX'
SELECT CASE WHEN dbo.Jtext(@a1,@a2)=0 THEN '不包含' ELSE '包含' END