复制代码代码如下:DECLARE@cINTDECLARE@c2INTSELECT@c=COUNT(1)FROMdbo.SplitToTable('1|2|3|4','|')SELECT@c2=COUNT(1)FROMdbo
DECLARE @c INT
DECLARE @c2 INT
SELECT @c = COUNT(1)
FROM dbo.SplitToTable('1|2|3|4', '|')
SELECT @c2=COUNT(1)
FROM dbo.SplitToTable('1|2|3|4', '|') a
INNER JOIN dbo.SplitToTable('1|2|3|', '|') b ON a.value = b.value
IF @c = @c2
SELECT 'ok'
ELSE
SELECT 'no'
SplitToTable这个函数如下:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER FUNCTION [dbo].[SplitToTable]
(
@SplitString NVARCHAR(MAX) ,
@Separator NVARCHAR(10) = ' '
)
RETURNS @SplitStringsTable TABLE
(
[id] INT IDENTITY(1, 1) ,
[value] NVARCHAR(MAX)
)
AS
BEGIN
DECLARE @CurrentIndex INT ;
DECLARE @NextIndex INT ;
DECLARE @ReturnText NVARCHAR(MAX) ;
SELECT @CurrentIndex = 1 ;
WHILE ( @CurrentIndex <= LEN(@SplitString) )
BEGIN
SELECT @NextIndex = CHARINDEX(@Separator, @SplitString,
@CurrentIndex) ;
IF ( @NextIndex = 0
OR @NextIndex IS NULL
)
SELECT @NextIndex = LEN(@SplitString) + 1 ;
SELECT @ReturnText = SUBSTRING(@SplitString,
@CurrentIndex,
@NextIndex - @CurrentIndex) ;
INSERT INTO @SplitStringsTable
( [value] )
VALUES ( @ReturnText ) ;
SELECT @CurrentIndex = @NextIndex + 1 ;
END
RETURN ;
END
标签: 集合 SQL 比较 一个 是否 另一个 里存 在的 方法
声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!