22,210
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[code] varchar(1))
insert [tb]
select 1,'a' union all
select 1,'b' union all
select 1,'c' union all
select 2,'a' union all
select 2,'d' union all
select 3,'a' union all
select 3,'c' union all
select 3,'d'
---------------------------
--创建函数
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + cast([code] as varchar(10)) FROM tb WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
-- 调用函数
select * from tb
where id in
(SELECt id FROM tb where dbo.f_str(id) like'%a,b,c%' GROUP BY id )
/*
id code
----------- ----
1 a
1 b
1 c
(3 行受影响)
*/
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([A] int,[B] int)
insert [tb]
select 1,1 union all
select 1,2 union all
select 1,3 union all
select 1,4 union all
select 2,1 union all
select 2,2 union all
select 3,4
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + cast(b as varchar(10)) FROM tb WHERE a=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
-- 调用函数
select * from tb
where a in
(SELECt a FROM tb where dbo.f_str(a) like'%1,2,3%' GROUP BY a )
/*A B
----------- -----------
1 1
1 2
1 3
1 4
(4 行受影响)
*/
create table test (a int , b int)
insert into test
select 1,1 union all
select 1,2 union all
select 1,3 union all
select 1,4
go
select * from test
select case when
(select count(*) from test where b =1)>0
and (select count(*) from test where b =2)>0
and (select count(*) from test where b =3)>0
then '1' else '0' end
----
1
(所影响的行数为 1 行)