日期:2014-05-17 浏览次数:20450 次
USE tempdb;
/*
CREATE TABLE t1
(
id INT IDENTITY(1,1),
title NVARCHAR(10) NOT NULL,
visibletype INT NOT NULL,
name NVARCHAR(200) NULL
);
INSERT INTO t1(title,visibletype,name)
VALUES('第一篇',1,NULL),('第二篇',1,NULL),('第三篇',1,NULL),
('第四篇',0,'zhangsan;lisi;'),('第五篇',0,'zhangsan;lisi;wangwu');
*/
DECLARE @username AS NVARCHAR(50);
--SET @username = 'zhangsan';
SET @username = 'zhaoliu';
SELECT *
FROM t1
WHERE t1.visibletype = 1
OR t1.[name] LIKE '%'+ @username + ';%';
select Title from table1 where VisibleType=1
union
select Title from table1 where VisibleType=0 and charindex('zhangsan',Name)>0
if object_id('test') is not null
drop table test
go
create table test(id int identity,title varchar(20),visibletype bit,name varchar(40))
insert into test
select '第一课',1,null union all
select '第二课',1,null union all
select '第三课',1,null union all
select '第四课',0,'zhangsan;lishi' union all
select '第五课',0,'zhangsan;lishi' union all
select '第六课',1,null
select * from test t where exists(
select 1 from test where CHARINDEX('fengzi',isnull(name,''))>0 or visibletype=1 and id=t.id
)
/*
(6 row(s) affected)
id title visibletype name
----------- -------------------- ----------- ----------------------------------------
1 第一课 1 NULL
2 第二课 1 NULL
3 第三课 1 NULL
6 第六课 1 NULL