Search code examples
c#sql-serversql-updatesqlparameter

How to keep existing value (append) while using SqlParameter to update a record


I want to update a column in SQL Server by keeping its original value and appending new value separated by comma. It can be done like this:

string sql = "UPDATE Table SET Column = Column + ', Test' WHERE ID = 1";

How can I do the same using SqlParameter if I have something like:

string sql = "UPDATE Table SET Column = @ColumnParameter WHERE ID = 1";
command.Parameters.Add(new SqlParameter("@ColumnParameter", "Test"));

Solution

  • Something like.

    string sql = "UPDATE Table SET Column = concat(Column,  ', ', @ColumnParameter) WHERE ID = 1";