ALTER Procedure [dbo].[cs_BannedAddress_CreateUpdateDelete]
@SettingsID INT
, @NetworkAddress NVARCHAR(50)
, @BannedDate DATETIME
, @ActionType INT --活动类型
AS
DECLARE @ID INT
DECLARE @ERROR INT
DECLARE @ROWCOUNT INT
-- CREATE
--判断不同的活动类型
IF @ActionType = 0
BEGIN
IF EXISTS( SELECT * FROM cs_BannedAddresses where SettingsID = @SettingsID and NetworkAddress = @NetworkAddress )
/*当活动类型为0的情况下,如果存在此地址于封闭表中,则执行活动类型为1的存储过程*/
exec cs_BannedAddress_CreateUpdateDelete @SettingsID, @NetworkAddress, @BannedDate, 1
else
BEGIN --如果不存在于表中,插入此数据记录
INSERT INTO cs_BannedAddresses VALUES( @SettingsID, @NetworkAddress, @BannedDate )
SELECT @ERROR = @@ERROR, @ID = @@IDENTITY, @ROWCOUNT = @@ROWCOUNT
IF( @ERROR <> 0 ) --如果错误则创建错误信息
BEGIN
RAISERROR( 'An error occurred while trying to create a new cs_BannedAddress record.', 16, 1 )
RETURN @ERROR
END
ELSE
BEGIN --成功返回0
RETURN 0
END
END
END
ELSE IF( @ActionType = 1 )
BEGIN
-- UPDATE
IF NOT EXISTS( SELECT * FROM cs_BannedAddresses WHERE SettingsID = @SettingsID and NetworkAddress = @NetworkAddress )
/*如果活动类型为1,但是表中不存在此记录则返回执行插入数据的上一段存储过程进行添加数据,然后再返回这里
此时就存在此纪录,转到ELSE块.更新*/
exec cs_BannedAddress_CreateUpdateDelete @SettingsID, @NetworkAddress, @BannedDate, 0
ELSE
BEGIN
UPDATE cs_BannedAddresses SET
SettingsID = @SettingsID,
NetworkAddress = @NetworkAddress,
BannedDate = @BannedDate
WHERE
SettingsID = @SettingsID
AND NetworkAddress = @NetworkAddress
/*更新数据*/
SELECT @ERROR = @@ERROR, @ID = @@IDENTITY, @ROWCOUNT = @@ROWCOUNT
IF( @ERROR <> 0 )
BEGIN --有错误则创建错误信息
RAISERROR('Could not update the cs_BannedAddress record for SettingsID %d and NetworkAddress of %d', 16, 1, @SettingsID, @NetworkAddress )
RETURN @ERROR
END
ELSE
BEGIN --成功返回0
RETURN 0
END
END
END
ELSE IF( @ActionType = 2 )
BEGIN
-- DELETE
/*如果活动类型为2(因为上面的活动类型在0和1之间,
所以可以判断在2的情况下记录肯定存在且被更新)*/
DELETE cs_BannedAddresses
WHERE SettingsID = @SettingsID
AND NetworkAddress = @NetworkAddress
SELECT @ERROR = @@ERROR, @ID = @@IDENTITY, @ROWCOUNT = @@ROWCOUNT
IF( @ROWCOUNT = 1 ) --如果操作成功返回0
RETURN 0
ELSE
RETURN 1
END
ELSE
BEGIN --如果活动类型不在0和2之间,则创建错误消息
RAISERROR( 'The ActionType value should be between 0 and 2. The value of %d was not within this range', 16, 1, @ActionType )
RETURN 1
END
GO
@SettingsID INT
, @NetworkAddress NVARCHAR(50)
, @BannedDate DATETIME
, @ActionType INT --活动类型
AS
DECLARE @ID INT
DECLARE @ERROR INT
DECLARE @ROWCOUNT INT
-- CREATE
--判断不同的活动类型
IF @ActionType = 0
BEGIN
IF EXISTS( SELECT * FROM cs_BannedAddresses where SettingsID = @SettingsID and NetworkAddress = @NetworkAddress )
/*当活动类型为0的情况下,如果存在此地址于封闭表中,则执行活动类型为1的存储过程*/
exec cs_BannedAddress_CreateUpdateDelete @SettingsID, @NetworkAddress, @BannedDate, 1
else
BEGIN --如果不存在于表中,插入此数据记录
INSERT INTO cs_BannedAddresses VALUES( @SettingsID, @NetworkAddress, @BannedDate )
SELECT @ERROR = @@ERROR, @ID = @@IDENTITY, @ROWCOUNT = @@ROWCOUNT
IF( @ERROR <> 0 ) --如果错误则创建错误信息
BEGIN
RAISERROR( 'An error occurred while trying to create a new cs_BannedAddress record.', 16, 1 )
RETURN @ERROR
END
ELSE
BEGIN --成功返回0
RETURN 0
END
END
END
ELSE IF( @ActionType = 1 )
BEGIN
-- UPDATE
IF NOT EXISTS( SELECT * FROM cs_BannedAddresses WHERE SettingsID = @SettingsID and NetworkAddress = @NetworkAddress )
/*如果活动类型为1,但是表中不存在此记录则返回执行插入数据的上一段存储过程进行添加数据,然后再返回这里
此时就存在此纪录,转到ELSE块.更新*/
exec cs_BannedAddress_CreateUpdateDelete @SettingsID, @NetworkAddress, @BannedDate, 0
ELSE
BEGIN
UPDATE cs_BannedAddresses SET
SettingsID = @SettingsID,
NetworkAddress = @NetworkAddress,
BannedDate = @BannedDate
WHERE
SettingsID = @SettingsID
AND NetworkAddress = @NetworkAddress
/*更新数据*/
SELECT @ERROR = @@ERROR, @ID = @@IDENTITY, @ROWCOUNT = @@ROWCOUNT
IF( @ERROR <> 0 )
BEGIN --有错误则创建错误信息
RAISERROR('Could not update the cs_BannedAddress record for SettingsID %d and NetworkAddress of %d', 16, 1, @SettingsID, @NetworkAddress )
RETURN @ERROR
END
ELSE
BEGIN --成功返回0
RETURN 0
END
END
END
ELSE IF( @ActionType = 2 )
BEGIN
-- DELETE
/*如果活动类型为2(因为上面的活动类型在0和1之间,
所以可以判断在2的情况下记录肯定存在且被更新)*/
DELETE cs_BannedAddresses
WHERE SettingsID = @SettingsID
AND NetworkAddress = @NetworkAddress
SELECT @ERROR = @@ERROR, @ID = @@IDENTITY, @ROWCOUNT = @@ROWCOUNT
IF( @ROWCOUNT = 1 ) --如果操作成功返回0
RETURN 0
ELSE
RETURN 1
END
ELSE
BEGIN --如果活动类型不在0和2之间,则创建错误消息
RAISERROR( 'The ActionType value should be between 0 and 2. The value of %d was not within this range', 16, 1, @ActionType )
RETURN 1
END
GO