I have a stored procedure in which i am inserting value in table t1 and after inserting values in table t1 updating a column c1 in table t1.
And am using two integer scalar variable @v1 and @v2 to get count of rows affected by using @@ROWCOUNT for both scalar variables.
It works fine for insert statement but returns 0 for update statement
DECLARE @VersionNumber VARCHAR(100)
DECLARE @ReleaseDate DATE
DECLARE @Created_By VARCHAR(MAX)
DECLARE @IsActive INT
DECLARE @ResponseText VARCHAR(MAX)
SET @VersionNumber = '1.0.2.0'
SET @ReleaseDate = '01-Aug-2013'
SET @Created_By = 'christopherf'
SET @IsActive = 1
SET @ResponseText = ''
BEGIN
SET NOCOUNT ON;
DECLARE @Update1 SMALLINT
DECLARE @Update2 SMALLINT
DECLARE @Ref_DeskTimeCurrentVersion_ID INT
IF(@VersionNumber <> '')
BEGIN
IF NOT EXISTS(SELECT * FROM DeskTime_CurrentVersion DCV WHERE LOWER(DCV.VersionNumber) = LOWER(@VersionNumber))
BEGIN
IF(@IsActive = 1)
BEGIN
IF((SELECT COUNT(*) FROM DeskTime_CurrentVersion) >= 1)
BEGIN
INSERT INTO DeskTime_CurrentVersion
(VersionNumber,ReleaseDate,Created_By,Created_DateTime,IsActive,Flag)
VALUES
(@VersionNumber,@ReleaseDate,@Created_By,GETDATE(),@IsActive,1)
IF(@@ROWCOUNT = 1)
BEGIN
SET @Update1 = 1
END
SELECT @Ref_DeskTimeCurrentVersion_ID = IDENT_CURRENT('DeskTime_CurrentVersion')
UPDATE DeskTime_CurrentVersion
SET IsActive = 0,
Flag = 0,
Last_Updated_By = @Created_By,
Last_Updated_DateTime = GETDATE()
WHERE Ref_DeskTimeCurrentVersion_ID <> @Ref_DeskTimeCurrentVersion_ID
IF(@@ROWCOUNT = 1)
BEGIN
SET @Update2 = 1
END
IF(@Update1 = 1 AND @Update2 = 1)
BEGIN
SET @ResponseText = 'ADDED'
END
END
ELSE
BEGIN
INSERT INTO DeskTime_CurrentVersion
(VersionNumber,ReleaseDate,Created_By,Created_DateTime,IsActive,Flag)
VALUES
(@VersionNumber,@ReleaseDate,@Created_By,GETDATE(),@IsActive,1)
IF(@@ROWCOUNT = 1)
BEGIN
SET @Update1 = 1
END
IF(@Update1 = 1)
BEGIN
SET @ResponseText = 'ADDED'
END
END
END
ELSE
BEGIN
INSERT INTO DeskTime_CurrentVersion
(VersionNumber,ReleaseDate,Created_By,Created_DateTime,IsActive,Flag)
VALUES
(@VersionNumber,@ReleaseDate,@Created_By,GETDATE(),@IsActive,1)
IF(@@ROWCOUNT = 1)
BEGIN
SET @ResponseText = 'ADDED'
END
END
END
ELSE
BEGIN
SET @ResponseText = 'Version number already exists'
END
END
ELSE
BEGIN
SET @ResponseText = 'Invalid version number provided.'
END
PRINT @ResponseText
END