What I have is a table with the following columns
All columns are nvarchar expect for the Date columns which are datetime
CreateDateA InvIDA StorageIDA CreateDateB InvIDB StorageIDB UniID
What I want to do is move the values on each row (based on UniID) into the represenative B columns where UniID is 1 less, so the ‘A’ columns where UniID = 2 should go to the B Columns where the UniID = 1.
This is the query i’m using but it’s not updating any columns.
update InvSubmission
set CreateDateB = CreateDateA,
InvIDB = InvIDA ,
StorageIDB = StorageIDA
where StorageIDB = StorageIDA and UniID = (select UniID-1 from InvSubmission)
There is multiple storageid’s in this table, however, each record has a match to at least one other row.
@PinnyM’s answer is close, but in SQL Server, you have to name the updated table in the from clause also:
Otherwise, this note becomes relevant:
And then you’re suffering the same problem that @Yuck pointed out – you’re trying to compare the
UniIDvalue within a single row to a value one less than itself.