表tabbulletin 结构如下:
[ID] [int] IDENTITY
[ClassID] [int] NOT NULL
[ToName] [nvarchar] (100)
[ToID] [nvarchar] (100)
[ToALL] [int] NULL ,
[ToDepartment] [int] NULL ,
[Title] [nvarchar] (100)
[Body] [text] COLLATE
[Remark] [text] COLLATE
[Times] [datetime] NOT NULL
[isRead] [int] NOT NULL
[FromID] [int] NOT NULL
[FromName] [nvarchar] (50)
[KeyWords] [nvarchar] (50)
[HaveRead] [nvarchar] (200)
其中toall=1表示发送给所有人,todepartment表示发送给某个部门(即该部门所有员工或部分员工能接收到该公告),toid表示发送给某些人,以“,”间隔,如:12,5,39,48 表示分别发送给代号为12、5、39、48的员工。
三者的关系为:
发送给所有人:toall=1,todepartment=0,toid为空;
发送给某部分所有人:toall=0,todepartment=部门代号,toid为空
发送给某部门某些人:toall=0,todepartment=部门代号,toid为员工代号
发送给某些人:toall=0,todepartment=0,toid为员工代号
我要编写一个存储过程,把某员工收到的公告全部选出来,请问这个存储过程该怎么写:
存储过程开头如下:
create procedure t_searchbulletion myid varchar(5),mydeptid varchar(5) //myid 为员工代号 mydeptid为员工所在部门代号
as
…
请教这个存储过程该怎么写?解决问题立即给分揭贴!!
---------------------------------------------------------------
create procedure t_searchbulletion
@myid varchar(5),
@mydeptid varchar(10)
as
select * from abbulletin
where (toall=1 and todepartment=0 and toid is null) or
(toall=0 and todepartment=@myid and toid is null) or
(','+toid+',' like '%,'+ @mydeptid +',%' and toall=0 and todepartment=0 )
---------------------------------------------------------------
create procedure t_searchbulletion
@myid varchar(5),
@mydeptid varchar(10)
as
select * from abbulletin
where (toall=1 and todepartment=0 and toid is null) or
(toall=0 and (( todepartment=@myid and toid is null) or
(','+toid+',' like '%,'+ @mydeptid +',%')))
---------------------------------------------------------------
create procedure t_searchbulletion
@myid varchar(5),
@mydeptid varchar(10)
as
select * from abbulletin
where toall=1 or
toall=0 and todepartment=@myid and toid is null or
charindex(@myid + ',', toid+',') > 0