I have a table which has the below columns.
Ticket_id (Primary key, Int)
Attachment1 (varchar)
Attachment2 (varchar)
Attachment3 (varchar)
Attachment4 (varchar)
Attachment5 (varchar)
I am writing a stored proc to update this table. there are 6 input parameters for the above 6 columns.
@Attached_File1 VARCHAR(MAX),
@Attached_File2 VARCHAR(MAX),
@Attached_File3 VARCHAR(MAX),
@Attached_File4 VARCHAR(MAX),
@Attached_File5 VARCHAR(MAX),
@Ticket_ID BIGINT
I want to write a sql query which will update the table with the values specified in the input parameters. BUT I must not overrite the attachment columns with null. I mean I need to use only those parameters which contains data.
For example, if the table ha a row [10, “aaa”, “bbb”, null, null, null] and the input parameters are (10, null, null, “ccc”, “ddd”, null) then after the update the row will become [10, “aaa”, “bbb”, “ccc”, “ddd”, null]
How to check for null/empty strings and generate the update query accordingly to achieve this?
You can use ISNULL
This solution does not overwrite an existing value with a new. If you want to do that, you should switch the values around: