从 C# (T-SQL) 执行带有 CASE 切换的存储过程时出现异常

发布于 2024-09-29 05:08:49 字数 1105 浏览 5 评论 0原文

我的表中有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月竹挽风 2024-10-06 05:08:49

您正在处理的列的数据类型是什么?因为我通过尝试将 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.

少女的英雄梦 2024-10-06 05:08:49

该错误本身表示“字符串或二进制数据将被截断”。这意味着您插入的值似乎大于 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.

人海汹涌 2024-10-06 05:08:49

使用完全相同的存储过程和您描述的表,我运行了以下代码,

class Program
    {
    static void Main(string[] args)
    {

        using(SqlConnection cnn = new SqlConnection(@"Server=.;Database=test;Trusted_Connection=True;"))
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand("updateTable",cnn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@columnName",
                   System.Data.SqlDbType.NVarChar, 128));

            cmd.Parameters["@columnName"].Value = "BigNvarcharValue";

            cmd.Parameters.Add(new SqlParameter("@value",  
                     System.Data.SqlDbType.NVarChar, -1));
            cmd.Parameters["@value"].Value = new string('T', 80000);

            cmd.ExecuteNonQuery();



        }

    }
}

效果很好。我会检查命令文本和参数集合(名称和值)并验证每个是否如您所想。

With your exact same stored procedure and the table you described I ran the following code

class Program
    {
    static void Main(string[] args)
    {

        using(SqlConnection cnn = new SqlConnection(@"Server=.;Database=test;Trusted_Connection=True;"))
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand("updateTable",cnn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@columnName",
                   System.Data.SqlDbType.NVarChar, 128));

            cmd.Parameters["@columnName"].Value = "BigNvarcharValue";

            cmd.Parameters.Add(new SqlParameter("@value",  
                     System.Data.SqlDbType.NVarChar, -1));
            cmd.Parameters["@value"].Value = new string('T', 80000);

            cmd.ExecuteNonQuery();



        }

    }
}

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.

放肆 2024-10-06 05:08:49

感谢大家的回复。我最终将大列的更新分离为单独的过程,这解决了问题。我确信罪魁祸首是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文