I have a NVARCHAR(max) column in a table and a stored procedure that would update this column as well as any other column in the table using CASE switching:
CREATE PROCEDURE updateTable
@columnName sysname,
@value nvarchar(max)
AS
UPDATE [dbo].[TestTable]
SET
BigNvarcharValue = CASE @columnName WHEN 'BigNvarcharValue' THEN @value ELSE BigNvarcharValue END,
TableName = CASE @columnName WHEN 'TableName' THEN @value ELSE TableName END
All is good if I execute this procedure from SQL Management Studio with
EXEC [dbo].[updateTable]
@columnName = 'BigNvarcharValue',
@value = N'SOME BIG 80Kb value'
I can also update TableName from C# code using the same stored procedure, but when it comes to updating this BigNvarcharValue from C#, it fails with SQLException that "String or binary data would be truncated". Now, I figured it has something to do with CASE in this stored procedure, because when I break it to a simpler stored procedure, everything works fine:
CREATE PROCEDURE updateTable
@columnName sysname,
@value nvarchar(max)
AS
UPDATE [dbo].[TestTable]
SET BigNvarcharValue=@value
I read a bunch of forum posts that describe this problem of trying to insert a bigger value into NVARCHAR column that would cause this exception, but it doesnt seem to apply.
I'm fairly new to T-SQL, so are there any limitations of CASE that I dont know of?
P.S. BigNvarcharValue is NVARCHAR(MAX) and TableName is NVARCHAR(50)