set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [Replay_InsertTrigger]
ON [dbo].[Sms_SendHistoryReplay]
INSTEAD OF INSERT
AS
BEGIN
DECLARE @LastReplayID INT
DECLARE @LastPhoneNum NVARCHAR(20)
DECLARE @FullContents NVARCHAR(200)
DECLARE @ReplayID INT
DECLARE @HistoryID INT
DECLARE @UserID INT
DECLARE @UserName NVARCHAR(50)
DECLARE @PhoneNum NVARCHAR(20)
DECLARE @Contents NVARCHAR(200)
DECLARE @SpNumber NVARCHAR(50)
DECLARE @CreateTime DATETIME
DECLARE @IsLong INT
SELECT @ReplayID=ReplayID,@HistoryID=HistoryID,@UserID=UserID,@UserName=UserName,
@PhoneNum=PhoneNum,@Contents=Contents,@SpNumber=SpNumber,@CreateTime=CreateTime,@IsLong=IsLong
FROM inserted
--前面包含3个问号“???”判断为长短信
IF(substring(@Contents,1,3)='???')
BEGIN
SET @Contents = substring(@Contents,4,len(@Contents))
SELECT TOP 1 @LastReplayID=ReplayID,@LastPhoneNum=PhoneNum FROM Sms_SendHistoryReplay ORDER BY ReplayID DESC
--与最新的一条号码相同,则组合在上一条短信中
--如果号码不相同,则重新插入。
IF(@LastPhoneNum=@PhoneNum)
BEGIN
IF(substring(@Contents,1,2)='信息' or substring(@Contents,1,2)='笔记')
BEGIN
UPDATE Sms_SendHistoryReplay SET Contents=@Contents+Contents WHERE ReplayID=@LastReplayID
END
ELSE
BEGIN
UPDATE Sms_SendHistoryReplay SET Contents=Contents+@Contents WHERE ReplayID=@LastReplayID
END
END
ELSE
BEGIN
INSERT INTO Sms_SendHistoryReplay(ReplayID,HistoryID,UserID,UserName,PhoneNum,Contents,SpNumber,CreateTime,PushStatus)
VALUES(@ReplayID,@HistoryID,@UserID,@UserName,@PhoneNum,@Contents,@SpNumber,GETDATE(),2)
END
END
ELSE
BEGIN
INSERT INTO Sms_SendHistoryReplay(ReplayID,HistoryID,UserID,UserName,PhoneNum,Contents,SpNumber,CreateTime,PushStatus)
VALUES(@ReplayID,@HistoryID,@UserID,@UserName,@PhoneNum,@Contents,@SpNumber,GETDATE(),0)
END
END
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [Replay_InsertTrigger]
ON [dbo].[Sms_SendHistoryReplay]
INSTEAD OF INSERT
AS
BEGIN
DECLARE @LastReplayID INT
DECLARE @LastPhoneNum NVARCHAR(20)
DECLARE @FullContents NVARCHAR(200)
DECLARE @ReplayID INT
DECLARE @HistoryID INT
DECLARE @UserID INT
DECLARE @UserName NVARCHAR(50)
DECLARE @PhoneNum NVARCHAR(20)
DECLARE @Contents NVARCHAR(200)
DECLARE @SpNumber NVARCHAR(50)
DECLARE @CreateTime DATETIME
DECLARE @IsLong INT
SELECT @ReplayID=ReplayID,@HistoryID=HistoryID,@UserID=UserID,@UserName=UserName,
@PhoneNum=PhoneNum,@Contents=Contents,@SpNumber=SpNumber,@CreateTime=CreateTime,@IsLong=IsLong
FROM inserted
--前面包含3个问号“???”判断为长短信
IF(substring(@Contents,1,3)='???')
BEGIN
SET @Contents = substring(@Contents,4,len(@Contents))
SELECT TOP 1 @LastReplayID=ReplayID,@LastPhoneNum=PhoneNum FROM Sms_SendHistoryReplay ORDER BY ReplayID DESC
--与最新的一条号码相同,则组合在上一条短信中
--如果号码不相同,则重新插入。
IF(@LastPhoneNum=@PhoneNum)
BEGIN
IF(substring(@Contents,1,2)='信息' or substring(@Contents,1,2)='笔记')
BEGIN
UPDATE Sms_SendHistoryReplay SET Contents=@Contents+Contents WHERE ReplayID=@LastReplayID
END
ELSE
BEGIN
UPDATE Sms_SendHistoryReplay SET Contents=Contents+@Contents WHERE ReplayID=@LastReplayID
END
END
ELSE
BEGIN
INSERT INTO Sms_SendHistoryReplay(ReplayID,HistoryID,UserID,UserName,PhoneNum,Contents,SpNumber,CreateTime,PushStatus)
VALUES(@ReplayID,@HistoryID,@UserID,@UserName,@PhoneNum,@Contents,@SpNumber,GETDATE(),2)
END
END
ELSE
BEGIN
INSERT INTO Sms_SendHistoryReplay(ReplayID,HistoryID,UserID,UserName,PhoneNum,Contents,SpNumber,CreateTime,PushStatus)
VALUES(@ReplayID,@HistoryID,@UserID,@UserName,@PhoneNum,@Contents,@SpNumber,GETDATE(),0)
END
END