I have a requirement in which i need to see whether that key already exists. If it already exists update the data based on the key else just insert new data.
The approach which i took is
DECLARE @Tracking_Id INT
SELECT @Tracking_Id = Tracking_Id
FROM DOCUMENT_TRACKING
WHERE Secondary_Document_Id = @Secondary_Document_Id
AND primary_Document_Id = @Primary_Document_Id
IF ( @Tracking_Id = 0 )
BEGIN
INSERT INTO DOCUMENT_TRACKING
(
Primary_Document_Id,
Secondary_Document_Id,
Tracking_Result,
Comment,
Created_By,
Created_Dt,
Updated_By,
Updated_Dt
)
VALUES (
@Primary_Document_Id,
@Secondary_Document_Id,
@TrackingResult,
@Comments,
@User_ID,
GETDATE(),
@User_ID,
GETDATE()
)
END
ELSE
BEGIN
SELECT @Tracking_Id = Tracking_Id
FROM DOCUMENT_TRACKING
WHERE Secondary_Document_Id = @Secondary_Document_Id
AND primary_Document_Id = @Primary_Document_Id
UPDATE DOCUMENT_TRACKING
SET tracking_result = @TrackingResult,
Comment = @Comments,
Updated_By = @User_ID,
Updated_Dt = GETDATE()
WHERE Tracking_Id = @Tracking_Id
END
Now when there is no row corresponding to my condition in database,either @Tracking_Id has to be 0 or '' or null but it shows nothing when i compare it with any of these things.
I donot want to use count approach and then compare with 0 or greater than that. How can i deal with this scenerio.