0

I have this table CustomerProfile that has the CustomerId, ProfileId,startDate and endDate. When the user add data to the profile it will insert a record with a startDate and endDate is null. If the user edit the profile I want to see if they make a change so I need to count the record in the table and if it not 0 then I do a check if that option has changed. In the BEGIN section do I need to select the ProfileId and see if it different? can I do that in the CountCustomerProfileId? Any help with this would be great.

Insert default values

insert into [dbo].[CustomerProfile] (CustomerId,ProfileId,startDate,endDate)
values (1,2,2022-09-07,null)

Counts that the users has insert data in the profile table before

DECLARE @CountCustomerProfileId INT
SET @CountCustomerProfileId = (SELECT Count([CustomerProfileId]) as CustomerProfileId
        FROM [dbo].[CustomerProfile]
        where [CustomerId] = @CustomerId)

BEGIN Code: ProfileId "2,3"

IF @CustomerProfileId > 0
    BEGIN
    
        UPDATE [dbo].[CustomerProfile] set endDate = GETDATE() 
        WHERE CustomerId = @CustomerId
    END     

http://sqlfiddle.com/#!18/26d992/5

2
  • "In the BEGIN section do I need to select the ProfileId and see if it different?" - What do you mean by this? What is your ultimate goal here?
    – J.D.
    Commented Sep 7, 2022 at 18:00
  • the ultimate goal is set a endDate on the old value if they are not the same id. If the id are the same I dont need to do anything.
    – Jefferson
    Commented Sep 7, 2022 at 19:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.