定义的存储过程变量nvarchar(max)被截断为4000个字符

时间:2015-01-09 07:46:04

标签: sql-server tsql sqldatatypes nvarchar

当我打印@v_sql参数时,我得到4000个字符。

我如何解决这个问题?

来源:

BEGIN
    SET NOCOUNT ON;
    DECLARE @v_sql nvarchar(max)
    SELECT @v_sql = N'';
    IF UPPER(@action)='DISABLE'
    BEGIN
        SELECT @v_sql = @v_sql + 'ALTER INDEX '+ QUOTENAME(name,'[')+' ON ' + quotename( OBJECT_SCHEMA_NAME(object_id),'[') +'.'+ quotename( OBJECT_NAME(object_id),'[') + ' DISABLE;'+char(10)
        FROM sys.indexes
        WHERE OBJECTPROPERTY([object_id], 'IsMsShipped') = 0 AND type =2;   
    END
    IF UPPER(@action)='REBUILD'
    BEGIN
        SELECT @v_sql = @v_sql + 'ALTER INDEX '+QUOTENAME(name,'[')+' ON ' +  quotename( OBJECT_SCHEMA_NAME(object_id),'[') +'.'+ quotename( OBJECT_NAME(object_id),'[') + ' REBUILD WITH(SORT_IN_TEMPDB=ON);'+char(10)
        FROM sys.indexes
        WHERE OBJECTPROPERTY([object_id], 'IsMsShipped') = 0 AND type =2;
    END
    IF @v_sql <> ''
    BEGIN
        print @v_sql
        --EXEC sp_executesql @v_sql;
    END
END

3 个答案:

答案 0 :(得分:1)

它被print截断 - “varchar(max)和nvarchar(max)数据类型被截断为不大于varchar(8000)和nvarchar(4000)的数据类型。”

答案 1 :(得分:1)

你是对的,当我们获取PRINT中的数据时它会被截断,但在执行实际数据时它不会被截断,唯一的就是我们必须将长度定义为MAX。

Sqlserver也支持8000字符。唯一的,你看不到,但它将由sqlserver执行。

意味着EXEC sp_executesql @v_sql;将执行sqlserver追加多个长字符串。并执行。

我认为你得到一个错误,因为我们需要打印实际的查询。是的,如果是,那么我有一个解决方案,你可以打破你独立执行或工作的代码。

您可以使用结果集中的核心值,检查是否会产生问题,然后删除/添加一些过滤更多数据的条件。

最后,你得到了产生问题的数据。

仅运行此部分,然后运行第二部分。

DECLARE @v_sql nvarchar(max)
    SELECT @v_sql = N'';
    IF UPPER(@action)='DISABLE'
    BEGIN
        SELECT @v_sql = @v_sql + 'ALTER INDEX '+ QUOTENAME(name,'[')+' ON ' + quotename( OBJECT_SCHEMA_NAME(object_id),'[') +'.'+ quotename( OBJECT_NAME(object_id),'[') + ' DISABLE;'+char(10) + char(13)
        FROM sys.indexes
        WHERE OBJECTPROPERTY([object_id], 'IsMsShipped') = 0 AND type =2;   
    END
    IF @v_sql <> ''
       BEGIN
         select @v_sql
         --EXEC sp_executesql @v_sql;
       END

nvarchar(max) still being truncated

答案 2 :(得分:0)

在我的情况下,我希望能够复制粘贴sp的结果,所以解决方案是:

1)用select替换打印 2)在char(10)之后添加char(13)

BEGIN
    SET NOCOUNT ON;
    DECLARE @v_sql nvarchar(max)
    SELECT @v_sql = N'';
    IF UPPER(@action)='DISABLE'
    BEGIN
        SELECT @v_sql = @v_sql + 'ALTER INDEX '+ QUOTENAME(name,'[')+' ON ' + quotename( OBJECT_SCHEMA_NAME(object_id),'[') +'.'+ quotename( OBJECT_NAME(object_id),'[') + ' DISABLE;'+char(10) + char(13)
        FROM sys.indexes
        WHERE OBJECTPROPERTY([object_id], 'IsMsShipped') = 0 AND type =2;   
    END
    IF UPPER(@action)='REBUILD'
    BEGIN
        SELECT @v_sql = @v_sql + 'ALTER INDEX '+QUOTENAME(name,'[')+' ON ' +  quotename( OBJECT_SCHEMA_NAME(object_id),'[') +'.'+ quotename( OBJECT_NAME(object_id),'[') + ' REBUILD WITH(SORT_IN_TEMPDB=ON);'+char(10) + char(13)
        FROM sys.indexes
        WHERE OBJECTPROPERTY([object_id], 'IsMsShipped') = 0 AND type =2;
    END
    IF @v_sql <> ''
    BEGIN
        select @v_sql
        --EXEC sp_executesql @v_sql;
    END
END