現有兩張表:
表一名:Message
ID:編號;
SendID:寄件者編號;
RecID:接受者編號(如為0,則接受者為所有人);
MessageID:站內信編號;
Statue:站內信的查看狀態,
ReadTime:讀取時間;
表二名:MessageText
ID:編號;
Message:站內信的內容;
PDate:站內信發送時間;
表三名:ReadGlobalMessage
ID:群訊息編號
GlobalID:群訊息ID與Message中的messageid相對應;
USerID:使用者編號;
ReadTime:讀取時間;
Status:群訊息狀態(只是是否存在);
其中兩個編號為自動成長,Message中的MessageID為MessageText表的外鍵,對應於MessageText中的編號ID,
管理員群發站內信:
ALTER PROCEDURE dbo.newmsgsingle @sendid int, @recid int, @title nvarchar(50), @message nvarchar(50)ASinsert into messgetext values(@title,@message,getdate())insert into message values(@sendid,@recid,@@identity,'',0)RETURN
這裡的recid設為0,表示接受者為所有人;
下面用到的Msg為視圖:
SELECT dbo.Message.SendID, dbo.Message.RecID, dbo.Message.MessageID, dbo.Message.ReadTime, dbo.Message.Status, dbo.MessgeText.Title, dbo.MessgeText.Message, dbo.MessgeText.CreateTimeFROM dbo.Message INNER JOIN dbo.MessgeText ON dbo.Message.MessageID = dbo.MessgeText.ID
顯示未查看短訊息的數目:
ALTER PROCEDURE dbo.getmsgcount@userid intASSELECT count(MessageID)FROM MsgWHERE (MessageID NOT IN (SELECT GlobalID FROM ReadGlobalMessage WHERE (UserID =@userid))) AND (Status = 0) AND (RecID =@userid OR RecID = 0)RETURN
擷取未讀短訊息的資料:
ALTER PROCEDURE dbo.getunreadmsglist@userid int ASSELECT *FROM MsgWHERE (MessageID NOT IN (SELECT GlobalID FROM ReadGlobalMessage WHERE (UserID = @userid))) AND (Status = 0) AND (RecID =@userid OR RecID = 0)RETURN
查看所有短訊息(包括未讀的已讀的,只要沒有被使用者數刪除就擷取)
ALTER PROCEDURE dbo.getallmsg@userid int ASSELECT *FROM MsgWHERE (MessageID NOT IN (SELECT GlobalID FROM ReadGlobalMessage WHERE (UserID = @userid and status=0))) AND (RecID =@userid) OR (RecID = 0)RETURN
查看短訊息的詳細內容:
ALTER PROCEDURE dbo.getmsgdetails@msgid int,@userid intASdeclare @temprecid intselect @temprecid=recid from message where id=@msgidif @temprecid!=0begin select * from messgetext where id=@msgid update message set status=1,readtime=getdate() where messageid=@msgid and recid=@userid end else begin select * from messgetext where id=@msgid insert into readglobalmessage values(@msgid,@userid,getdate(),default) endRETURN
使用者查看短訊息內容:使用者進入短訊息介面之後,點擊其中一條短訊息,根據短訊息編號擷取具體的短訊息內容;如果該短訊息為群訊息則在ReadGlobalMessage中插入資料,如果為點對點的訊息則
更新Message表中的資料如Statue以及Readtime;
擷取
使用者之間發送短訊息和管理群發短訊息類似;
關鍵點:擷取剛剛添加的自動成長列的值:@@IDENTITY;
站內信執行個體下載
參考部落格1
參考部落格2