从 C# (T-SQL) 执行带有 CASE 切换的存储过程时出现异常
我的表中有一个 NVARCHAR(max) 列,还有一个存储过程,该存储过程将使用 CASE 切换更新此列以及表中的任何其他列:
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
如果我从 SQL Management Studio 执行此过程,那么一切都很好,
EXEC [dbo].[updateTable]
@columnName = 'BigNvarcharValue',
@value = N'SOME BIG 80Kb value'
我也可以更新使用相同的存储过程从 C# 代码中获取 TableName,但是当从 C# 更新此 BigNvarcharValue 时,它会失败并出现 SQLException“字符串或二进制数据将被截断”。现在,我认为它与此存储过程中的 CASE 有关,因为当我将其分解为更简单的存储过程时,一切正常:
CREATE PROCEDURE updateTable
@columnName sysname,
@value nvarchar(max)
AS
UPDATE [dbo].[TestTable]
SET BigNvarcharValue=@value
我读了一堆论坛帖子,这些帖子描述了尝试将更大的值插入到的问题NVARCHAR 列会导致此异常,但它似乎并不适用。
我对 T-SQL 相当陌生,那么 CASE 是否有我不知道的限制?
PS BigNvarcharValue 为 NVARCHAR(MAX),TableName 为 NVARCHAR(50)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在处理的列的数据类型是什么?因为我通过尝试将 NVARCHAR(max) 允许的值插入到 VARCHAR(50) 列中来重现该错误。
重申一下 - NVARCHAR(max) 允许您指定一个比指定数据类型长的值,这就是您收到有关截断的错误的原因。
What are the data types of the columns you're dealing with? Because I've reproduced the error by attempting to insert a value that is allowed by NVARCHAR(max) into a column that is VARCHAR(50).
To reiterate - NVARCHAR(max) is allowing you to specify a value that is longer than the stated data type, which is why you get the error about truncation.
该错误本身表示“字符串或二进制数据将被截断”。这意味着您插入的值似乎大于
nvarchar(max)
可以处理的值。SSMS 2008 有一些调试功能,允许设置断点等。
我想您可能也希望注意
System.String
最大容量。这只是某个地方的长度问题。The error says it by itself, "String or binary data would be truncated". This means that you seem to insert a larger value than what the
nvarchar(max)
can handle.SSMS 2008 has some debugging features allowing to set breakpoints, etc.
I think you might wish to take an eye out to the
System.String
maximum capacity either. This is only a matter of length, somewhere.使用完全相同的存储过程和您描述的表,我运行了以下代码,
效果很好。我会检查命令文本和参数集合(名称和值)并验证每个是否如您所想。
With your exact same stored procedure and the table you described I ran the following code
It worked fine. I would inspect the command text and the parameter collection (name and value) and verify every is as you think it is.
感谢大家的回复。我最终将大列的更新分离为单独的过程,这解决了问题。我确信罪魁祸首是 CASE 声明。
Thanks everyone for the responses. I ended up separating update of the big column to an individual procedure, which solved the problem. Im sure the culprit was with the CASE statement.