I have a table which compares certain columns to columns in another table. If a column has changed, I want to ultimately change a "Notes" field to alert what has changed. More than one column could've changed.
In a nutshell, I declare the variable and I want to update that variable with whatever has changed and then Update a column with the variable.
In Access, I can do this quite easily, but I've spent two days trying to accomplish this in SQL to no avail.
Declare @NewUpdate nvarchar(max)
If B.RCustNmbr <> A.RSQ_Custnmbr Set @NewUpdate = 'CustNmbr changed from ' & A.RSQ_Custnmbr & ' to ' & B.RCustNmbr & ', '
If B.RCustName <> A.RSQ_CUSTNAME Set @NewUpdate = **@NewUpdate** & 'CustName changed from ' & A.RSQ_CUSTNAME & ' to ' & B.RCustName & ', '
Update A
Set A.[Notes] = @NewUpdate & " on today's date " & A.[Notes] *(to include notes that might've been there before)*
From [Table1] A Inner Join [Table2] B on A.ID = B.ID
Then I would want to Reset the @NewUpdate variable for the next ID (so Set @NewUpdate = Null)
Can you point me in the right direction to get this accomplished? I'm really unsure how I should reference my A & B tables in the If statements. I've tried a number of things. This is the closest:
Declare @NewUpdate nvarchar(max)
Set @NewUpdate = (Select 'CustName changed from ' + A.RSQ_CustName + ' to ' + B.RCustName + ', '
From [Customer Master List] A INNER JOIN [Customer Master List Changes TEMP] B ON A.ID = B.ID
Where **A.ID = 566** and B.RCustName <> A.RSQ_CustName)
(the above query formatted):
Declare @NewUpdate nvarchar(max)
Set @NewUpdate =
(Select 'CustName changed from ' + A.RSQ_CustName + ' to ' + B.RCustName + ', '
From [Customer Master List] A
INNER JOIN [Customer Master List Changes TEMP] B ON A.ID = B.ID
Where A.ID = 566 and B.RCustName <> A.RSQ_CustName)
But I have to limit it to 1 ID or I get the error Subquery returned more than 1 value. This is not permitted when the subquery follows =, etc.... and I need to do this for thousands of records.