Click here to Skip to main content
16,016,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

SQL
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
Posted
Comments
CHill60 6-May-15 8:16am    
Do you actually have any data where Ref_DeskTimeCurrentVersion_ID <> @Ref_DeskTimeCurrentVersion_ID?
Christopher Fernandes 6-May-15 9:13am    
Thanks this was the issue I had no data where in the table at first entry and after entry the SP works fine

Actual solution confirmed by OP - there was no data matching where Ref_DeskTimeCurrentVersion_ID <> @Ref_DeskTimeCurrentVersion_ID

Once data was entered the problem was resolved.
 
Share this answer
 
Dear Chris,

your condition might be failed on this condition

Last_Updated_By = @Created_By, Last_Updated_DateTime = GETDATE()
 
Share this answer
 
Comments
CHill60 6-May-15 8:14am    
Why?
vivvicks 6-May-15 23:58pm    
Date format issue..might be
CHill60 7-May-15 4:05am    
the name "Last_Update_DateTime" implies that the column is of type DateTime and GetDate() returns a DateTime so format does not come into it. "Format" only becomes an issue when character based types (nvarchar, varchar, char, nchar) are used for dates instead of DateTime/Date
vivvicks 7-May-15 5:50am    
thanks u so much for your knowledge sharing
CHill60 7-May-15 6:00am    
My pleasure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900