Im building a dynamic sql update statement. This statement will update an unknown number of columns. Im looking for a clever way to set the commas between the columns in the update-statement correct.
DECLARE @UpdateSQL nvarchar(max) = 'UPDATE MYTABLE SET '
WHILE (somecondition)
BEGIN
IF (someothercondition)
SET @UpdateSQL = @UpdateSQL + @ColNameVar + ' = ' @ColNameVar + ',' --How to set this comma only on columns that are not the last??
END
There is ofcourse more building to do with this updateSQL, but this is the only relevant part for my question.
One important note is that the last run of the while-loop is not necessarly putting in a new column in the update, so i could not use that as a check.
Any ideas?
Using SQL Server-2008
After your loop, strip the last character?
Change the position of the ‘,’ to the FRONT of the appended code, but don’t set it for the first one…