当前位置: 移动技术网 > IT编程>数据库>MSSQL > SQL 比较一个集合是否在另一个集合里存在的方法分享

SQL 比较一个集合是否在另一个集合里存在的方法分享

2017年12月12日  | 移动技术网IT编程  | 我要评论

党的建设必须,盘龙记,洪荒之人参果树

复制代码 代码如下:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网