Last post Feb 24, 2012 09:29 PM by gavilanch
Feb 24, 2012 05:30 PM|ashwinkotwaliwale|LINK
I have a text column in a table and there is some data in it such 'ABCD'. I want to update this column so i want 'ABCDPQRS'. Although the way i want to do this is without having to write a select statement and then appending to that result, in code.
Is there any way in code to only send 'PQRS' to the database and it will append it so that the end result will be 'ABCDPQRS'
Feb 24, 2012 05:35 PM|ryanbesko|LINK
UPDATE yourtablename SET yourcolumnname = yourcolumnname + 'PQRS'
Feb 24, 2012 09:29 PM|gavilanch|LINK
Because when you do that an update operation, SQL understands that you want to change the data in a cell, so he will "throw away" the actual data and subtitutes it with the new data. Imagine it like making an assigment to a variable:
a = 5; // Now 'a' is equal to 5 no matter what it was before.
a = 6; // Now 'a' is equal to 6 no matter what it was before.
a = a + 1; // Now 'a' is equal to 7 because it's using the previous value of 'a'
And something similar occurs with the a Update statement in SQL. If for example you want to add a counter to a column, you normally would do it like this:
update Users set [Counter]=Counter+1 where UserName=@id // Just an example
That way, similar to our example with a variable, you use the previous value of the column to send the correct one to the database.
I'm not sure why you don't want to use a Update statement though, but anyway, I'm not sure that's possible, because, that's
the way to comunicate with your database. You can use a data control you "just add" PQRS, but in the end is the same because you are sending "ABCPQRS" and not just PQRS...