I have two tables:
1) BlogPost:
COLUMN_NAME DATA_TYPE
Id int
Title varchar
Description varchar
ImageName varchar
FileName varchar
CreatedDate datetime
Tags varchar
ModifiedDate datetime
RateNumber int
CreatedBy int
ShortDescription varchar
2. BlogRating:
Id EmployeeId PostId Rate
4 1 12 3
5 1 13 2
6 1 11 2
I wrote a stored procedure to Save the details of BlogRating:
ALTER PROCEDURE [dbo].[BlogRatingSave]
@EmployeeId int
,@PostId int
,@Rate int
AS
BEGIN
DELETE FROM [HRM_BlogRating]
WHERE [HRM_BlogRating].[EmployeeId] = @EmployeeId
AND [HRM_BlogRating].[PostId] = @PostId
INSERT INTO [HRM_BlogRating]
([EmployeeId]
,[PostId]
,[Rate]
)
VALUES
(@EmployeeId
,@PostId
,@Rate
)
END
What I want is that If different employees give rate, while saving BlogRating I need to auto increment the field RateNumber in table1by 1. At the same time if same employee rates next time there should not an increment for RateNumber. Please help me to solve this.
Why are you deleting and then inserting. Instead you should try to insert and if it fails (due to a duplicate) then do an update.
In the process you can also adjust the value of the rating, but I assume you will need to pass in the EmployeeId of the person making the change since that is part of your rule. ie.
So it would all become: