question

Nasrudin avatar image
Nasrudin asked

Update column with condition

I have to update a lot of columns with one query, but the fields which should be updated vary from parameters. i do not want update always all columns resp. fill them with the value existing because of some triggers. Is there a way to achieve, that like in this example instead of NULL for 'ldf' 1 and 3 the field is not touched:


DECLARE @t1 TABLE (lfd INT, txt NVARCHAR(50));

INSERT INTO @t1 ([lfd], [txt]) VALUES (1, 'abc');

INSERT INTO @t1 ([lfd], [txt]) VALUES (2, 'def');

INSERT INTO @t1 ([lfd], [txt]) VALUES (3, 'ghi');

SELECT * FROM @t1

UPDATE @t1 SET txt = (CASE WHEN lfd = 2 THEN 'xxx' END)

SELECT * FROM @t1

any help appreciated. thanks in advance

sql querysqlserver
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.