SQL 比较一个集合是否在另一个集合里存在的方法分享
党的建设必须,盘龙记,洪荒之人参果树
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某个日期是当年的第几周
/* *周一作为一周的开始 *当年的1月1号所在的周算作第一周 */ CREATE function GetWeekIndexFirstDate ( @...
[阅读全文]
-
-
数据库SQL---范式
1、数据冗余导致的问题:冗余存储、更新异常、插入异常、删除异常。 2、函数依赖:一种完整性约束。 在关系模式r(R)中,α属于R,β属于R。 1)α函数...
[阅读全文]
-
-
数据库SQL---查询
1、查询所有列 select *from emp;--*表示所有的,from emp表示从emp表中查询。 2、查询指定列 select empno,e...
[阅读全文]
-
-
-
-
网友评论