如何在SQL Server 2008中存储超过8000个字符并分配大型连接字符串?

时间:2017-11-27 11:08:21

标签: asp.net c#-4.0 sql-server-2008-r2

我正在尝试连接大量的id&d以及更新所有ID的状态。

例如:

aclid in (4604019,4604018,4604017,4604016,4604015,4604014,4604013,4604012,4604011,4604010,4604009,4604008,4604007,4604006,4604005,4604004,4604003,4604002,4604001,4604000,4603999,4603998,4603997,4603996,4603995,4603994,4603993,4603992,4603991,4603990,4603989,4603988)`

请检查我的存储过程:

ALTER PROCEDURE [dbo].[VT_ACLReportChangeStatus]
    (@ChangeStatus nvarchar(50) = null,
     @ACLId nvarchar(max))
AS
    /* Exec VT_ACLReportChangeStatus 'Complete','4599473,4599472,4599471,4599469,4599468' */
BEGIN 
    UPDATE VT_ACLReport 
    SET Status = @ChangeStatus 
    WHERE ACLId IN (SELECT * FROM SplitDelimiterString(@ACLId,','))
END

请检查我的代码:

ACLId = ACLId.ToString().Trim(',');

using (SqlConnection con = new SqlConnection(cs))
{
    cmd = new SqlCommand("VT_ACLReportChangeStatus", con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.CommandTimeout = 3600;

    cmd.Parameters.Add(new SqlParameter("@ACLId", SqlDbType.NVarChar,-1));
    cmd.Parameters.Add(new SqlParameter("@ChangeStatus", SqlDbType.NVarChar, 50));
    cmd.Parameters["@ACLId"].Value = ACLId;
    cmd.Parameters["@ChangeStatus"].Value = ddlChangeStatus.SelectedItem.Text.ToString();

    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}

AclId列数据类型为bigint identity

请帮助我连接大字符串并更新所有存在aclid的行。

2 个答案:

答案 0 :(得分:0)

我建议,在sql中创建一个用户定义的表类型,在sourecode中创建一个具有相应数据的类。您可以将此类的对象传递给sql,在SP中,您可以使用主表和此表(作为输入参数接收)之间的连接来更新数据。

答案 1 :(得分:0)

感谢大家的答案。实际上,这是一个愚蠢的错误,同时在存储过程中调用拆分功能。在功能上它是varchar(8000)。 :)解决了它。

ALTER FUNCTION [dbo].[SplitDelimiterString] (@StringWithDelimiter VARCHAR(max), @Delimiter VARCHAR(max))
RETURNS @ItemTable TABLE (Item VARCHAR(max))
AS
BEGIN
    DECLARE @StartingPosition INT;
    DECLARE @ItemInString VARCHAR(max);
    SELECT @StartingPosition = 1;
    --Return if string is null or empty
    IF LEN(@StringWithDelimiter) = 0 OR @StringWithDelimiter IS NULL RETURN; 
    WHILE @StartingPosition > 0
    BEGIN
        --Get starting index of delimiter .. If string
        --doesn't contain any delimiter than it will returl 0 
        SET @StartingPosition = CHARINDEX(@Delimiter,@StringWithDelimiter); 

        --Get item from string        
        IF @StartingPosition > 0                
            SET @ItemInString = SUBSTRING(@StringWithDelimiter,0,@StartingPosition)
        ELSE
            SET @ItemInString = @StringWithDelimiter;
        --If item isn't empty than add to return table    
        IF( LEN(@ItemInString) > 0)
            INSERT INTO @ItemTable(Item) VALUES (@ItemInString);            

        --Remove inserted item from string
        SET @StringWithDelimiter = SUBSTRING(@StringWithDelimiter,@StartingPosition + 
                     LEN(@Delimiter),LEN(@StringWithDelimiter) - @StartingPosition)

        --Break loop if string is empty
        IF LEN(@StringWithDelimiter) = 0 BREAK;
    END

    RETURN
END