简体   繁体   中英

Best practice for concatenation in SQL Server?

My row has a NVARCHAR value in a certain column. I want to be able to concatenate onto this value eg, if the row stores "Hello," I want to be able to run a db.Execute command to keep the "Hello" and tag on " my name is Earl" so that "Hello, my name is Earl" is stored in the row.

What is the best practice for doing this?

db.Execute("UPDATE table SET description = description + '--@0 likes this' WHERE id = @1",user, id);

This doesnt accept the parameters and actually inserts @0and @1 into the database. Why is this ?

Just a guess, but try moving @0 out of the single quotes:

db.Execute(@"
    UPDATE table 
    SET description = description + @0 + ' likes this' 
    WHERE id = @1",user, id); 

If you have a table myTable with a message column you want to concat and the row id is 5 ...

UPDATE myTable
SET message = concat(message, N' my name is earl')
WHERE id = 5;  -- some where condition that identifies row(s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM