NewsId CategoryType CategoryID InfoContent
(自增ID) (int) (nvarchar) (ntext)
1 1 0 这条是大家都可以看的信息
2 2 1,11,111 这条是部门信息,且部门ID为1,11,111这三个部门可以查看
3 3 1 这条是用户ID为1私人信息,别人无法查看
4 1 0 这条是大家都可以看的信息
5 2 1,22,31 这条是部门信息,且部门ID为1,22,31这三个部门可以查看
6 3 5 这条是用户ID为5私人信息,别人无法查看
7 2 2,3 这条是部门信息,且部门ID为2,3这2个部门可以查看
---------------------------------------------------------------------------
这个是一名为News的表,表中有四个字段,字段名,字段类型如上。
CategoryType 字段是固定的3个类别数字,1表示公共信息,2表示部门信息,3表示私人
CategoryID 字段就是对应CategoryType类别的值,如CategoryType为1时候该值只有0值,CategoryType为2时候该值存储的是多个部门ID,CategoryType为3时候该值就存储一个用户ID号
InfoContent 就是信息内容字段当一个用户ID为1且他的部门ID是22的人,登陆系统后只能查看到NewsID为1,3,4,5这4条信息,请问这个SQL查询语句怎么样写,能查实现。
from news where cast(InfoContent as varchar(1000)) in('1','0')
and charindex(',',cast(InfoContent as varchar(1000))) = 0
union all
select *
from charindex(',',cast(InfoContent as varchar(1000))) > 0
and charindex(',22,',','+cast(InfoContent as varchar(1000))+',') > 0
-- Author: happy flystone
-- Version:V1.001
-- Date:2010-06-20
-------------------------------------- Test Data: news
If object_id('news') is not null
Drop table news
Go
Create table news(CategoryType int,CategoryID int,InfoContent ntext)
Go
Insert into news
select 1,1,'0' union all
select 2,2,'1,11,111' union all
select 3,3,'1' union all
select 4,1,'0' union all
select 5,2,'1,22,31' union all
select 6,3,'5' union all
select 7,2,'2,3'
Go
--Start
select *
from news
where cast(InfoContent as varchar(1000)) in('1','0')
and charindex(',',cast(InfoContent as varchar(1000))) = 0
union all
select *
from news
where charindex(',',cast(InfoContent as varchar(1000))) > 0
and charindex(',22,',','+cast(InfoContent as varchar(1000))+',') > 0
--Result:
/*CategoryType CategoryID InfoContent
------------ ----------- ----------
1 1 0
3 3 1
4 1 0
5 2 1,22,31(所影响的行数为 4 行)*/
--End
-- Author: happy flystone
-- Version:V1.001
-- Date:2010-06-20 22:35:20
-------------------------------------- Test Data: news
If object_id('news') is not null
Drop table news
Go
Create table news(NewsId int,CategoryType int,CategoryID nvarchar(8),InfoContent ntext)
Go
Insert into news
select 1,1,'0','这条是大家都可以看的信息' union all
select 2,2,'1,11,111','这条是部门信息,且部门ID为1,11,111这三个部门可以查看' union all
select 3,3,'1','这条是用户ID为1私人信息,别人无法查看' union all
select 4,1,'0','这条是大家都可以看的信息' union all
select 5,2,'1,22,31','这条是部门信息,且部门ID为1,22,31这三个部门可以查看' union all
select 6,3,'5','这条是用户ID为5私人信息,别人无法查看' union all
select 7,2,'2,3','这条是部门信息,且部门ID为2,3这2个部门可以查看'
Go
--Startselect *
from news
where CategoryID in('1','0')
and charindex(',',CategoryID) = 0
union all
select *
from news
where charindex(',',CategoryID) > 0
and charindex(',22,',','+CategoryID+',') > 0
--Result:
/*NewsId CategoryType CategoryID InfoContent
----------- ------------ ---------- -------------------------------
1 1 0 这条是大家都可以看的信息
3 3 1 这条是用户ID为1私人信息,别人无法查看
4 1 0 这条是大家都可以看的信息
5 2 1,22,31 这条是部门信息,且部门ID为1,22,31这三个部门可以查看(所影响的行数为 4 行)
*/
--End
但是你把SQL语句都固定了,我说如果是用户ID(用户ID字段是UserID)为1且他的部门ID(部门ID字段是GroupID)是22的人,登陆系统后只能查看到NewsID为1,3,4,5这4条信息你的语名怎么放到存储过程中,然后根据输入的UserID,GroupID来读取News表信息
IF OBJECT_ID('[news]') IS NOT NULL
DROP TABLE [news]
GO
CREATE TABLE [news] ([NewsId] [int],[CategoryType] [int],[CategoryID] [nvarchar](10),[InfoContent] [nvarchar](60))
INSERT INTO [news]
SELECT '1','1','0','这条是大家都可以看的信息' UNION ALL
SELECT '2','2','1,11,111','这条是部门信息,且部门ID为1,11,111这三个部门可以查看' UNION ALL
SELECT '3','3','1','这条是用户ID为1私人信息,别人无法查看' UNION ALL
SELECT '4','1','0','这条是大家都可以看的信息' UNION ALL
SELECT '5','2','1,22,31','这条是部门信息,且部门ID为1,22,31这三个部门可以查看' UNION ALL
SELECT '6','3','5','这条是用户ID为5私人信息,别人无法查看' UNION ALL
SELECT '7','2','2,3','这条是部门信息,且部门ID为2,3这2个部门可以查看'--SELECT * FROM [news]-->SQL查询如下:
IF OBJECT_ID('p_test')>0
DROP PROC p_test
GO
CREATE PROC p_test
@depid INT,
@perid INT
AS
SELECT *
FROM news
WHERE [CategoryType] = 1
OR ([CategoryType]=2 AND CHARINDEX(','+LTRIM(@depid)+',', ','+CategoryID+',')>0)
OR ([CategoryType]=3 AND CategoryID=LTRIM(@perid))
GOEXEC p_test 22,1
/*
NewsId CategoryType CategoryID InfoContent
----------- ------------ ---------- ------------------------------------------------------------
1 1 0 这条是大家都可以看的信息
3 3 1 这条是用户ID为1私人信息,别人无法查看
4 1 0 这条是大家都可以看的信息
5 2 1,22,31 这条是部门信息,且部门ID为1,22,31这三个部门可以查看(4 行受影响)
*/