I have a Microsoft SQL Server trigger that updates a remote database with new values when the local database is updated. Everything works fine, and I tested the script and it updates fine, unless there is a null value.
The code is below:
DECLARE @TSQL nvarchar(4000);
SELECT @TSQL =
'UPDATE
OPENQUERY(TEST,''SELECT * FROM test_db WHERE id = ' + convert(VARCHAR(MAX), @id) +''')
SET
parent_id = ' + convert(VARCHAR(MAX), @parent_id) + ', user_id = ' + convert(VARCHAR(MAX), @user_id) + ', item_id = ' + convert(VARCHAR(MAX), @item_id) + ''
EXEC (@TSQL)
Everything works well if all the fields have values, but if one column is null, then the query doesn't update the row at all, no errors thrown. I tried to use COALESCE()
to change the null variables to empty strings, and it will then update the row, but all the null columns become 0's and I want them to stay as NULL
values. All the columns in both database allow null values and default to null so I'm not sure why I cannot update the database.
Any help would be nice, thanks!