用来读取"最新消息"的存储过程
Receiver 字段存储的值的格式为: aaa|bbb|ccc|、aaa|
--select * from Messages
--drop table #Msg001
Create table #Msg001
(MSort varchar(50),
MTopic varchar(50),
MSender varchar(50),
MSendTime datetime,
MReceiver varchar(500),
Mcontent varchar(1000),
MState bit)
declare @i int,@start int,@Receiver varchar(500),@Acc varchar(50),@ChName varchar(50),@AllChName varchar(500)
declare @T1 varchar(50),@T2 varchar(50),@T3 varchar(50),@T4 datetime,@T5 varchar(500),@T6 varchar(1000),@T7 bit
--定义游标
declare Msg_cur scroll cursor for select Sort,Topic,Sender,SendTime,Receiver,Content,State from Messages
open Msg_cur
Fetch first from Msg_cur into @T1,@T2,@T3,@T4,@T5,@T6,@T7
--select @Reciver=Receiver from Messages where Receiver='aaa|bit|01.02|'
set @Receiver=@T5
while @@fetch_status=0
begin
set @AllChName=''
set @i=1
while (@i>0)
begin
if @i=1
set @start=@i
else
set @start=@i+1
set @i=charindex('|',@Receiver,@i+1)
if (@i>1)
begin
set @Acc=SUBSTRING(@Receiver,@start,@i-@start)
set @ChName=(select UserName from Accounts_Users where Account=@Acc)
--if (该IF语句用判断是否在最后加",")
if (@i=len(@Receiver))
select @AllChName=@AllChName+''+@ChName
else
select @AllChName=@AllChName+''+@ChName+','
--end if
--select @ChName,@AllChName,@i,@Receiver
end
end
--将接收者的姓名替换后插入到临时数据库里
insert into #Msg001(MSort,MTopic,MSender,MSendtime,MReceiver,MContent,MState)values(@T1,@T2,@T3,@T4,@AllChName,@T6,@T7)
Fetch next from Msg_cur into @T1,@T2,@T3,@T4,@T5,@T6,@T7
set @Receiver=@T5
end
--关闭游标
close Msg_cur
deallocate Msg_cur
select #Msg001.*,Messages_Sort.Name from #Msg001 INNER JOIN Messages_Sort ON #Msg001.MSort =Messages_Sort.ID
drop table #Msg001
--drop table #Msg001
Create table #Msg001
(MSort varchar(50),
MTopic varchar(50),
MSender varchar(50),
MSendTime datetime,
MReceiver varchar(500),
Mcontent varchar(1000),
MState bit)
declare @i int,@start int,@Receiver varchar(500),@Acc varchar(50),@ChName varchar(50),@AllChName varchar(500)
declare @T1 varchar(50),@T2 varchar(50),@T3 varchar(50),@T4 datetime,@T5 varchar(500),@T6 varchar(1000),@T7 bit
--定义游标
declare Msg_cur scroll cursor for select Sort,Topic,Sender,SendTime,Receiver,Content,State from Messages
open Msg_cur
Fetch first from Msg_cur into @T1,@T2,@T3,@T4,@T5,@T6,@T7
--select @Reciver=Receiver from Messages where Receiver='aaa|bit|01.02|'
set @Receiver=@T5
while @@fetch_status=0
begin
set @AllChName=''
set @i=1
while (@i>0)
begin
if @i=1
set @start=@i
else
set @start=@i+1
set @i=charindex('|',@Receiver,@i+1)
if (@i>1)
begin
set @Acc=SUBSTRING(@Receiver,@start,@i-@start)
set @ChName=(select UserName from Accounts_Users where Account=@Acc)
--if (该IF语句用判断是否在最后加",")
if (@i=len(@Receiver))
select @AllChName=@AllChName+''+@ChName
else
select @AllChName=@AllChName+''+@ChName+','
--end if
--select @ChName,@AllChName,@i,@Receiver
end
end
--将接收者的姓名替换后插入到临时数据库里
insert into #Msg001(MSort,MTopic,MSender,MSendtime,MReceiver,MContent,MState)values(@T1,@T2,@T3,@T4,@AllChName,@T6,@T7)
Fetch next from Msg_cur into @T1,@T2,@T3,@T4,@T5,@T6,@T7
set @Receiver=@T5
end
--关闭游标
close Msg_cur
deallocate Msg_cur
select #Msg001.*,Messages_Sort.Name from #Msg001 INNER JOIN Messages_Sort ON #Msg001.MSort =Messages_Sort.ID
drop table #Msg001