简体   繁体   English

SQL Server中的VARCHAR(MAX)值以上

[英]More than VARCHAR(MAX) values in SQL Server

I have some tables with more than 700 columns/1000 columns. 我有一些表有超过700列/ 1000列。

Now I want to display all columns from this table to ISNULL(col1,0) format because when I use PIVOT / UNPIVOT and if there are some NULL values then they wont be converted to NULL and becomes empty strings. 现在我想将此表中的所有列显示为ISNULL(col1,0)格式,因为当我使用PIVOT / UNPIVOT并且如果有一些NULL值时,它们将不会转换为NULL并变为空字符串。 So I am trying to replace those NULL s with 0 . 所以我试图用0替换那些NULL

In this example I used sysobjects table so that you can try it in your ssms. 在这个例子中,我使用了sysobjects表,以便您可以在ssms中尝试它。 The result of this is incomplete as neither VARCHAR(MAX) nor NVARCHAR(MAX) is enough. 由于VARCHAR(MAX)NVARCHAR(MAX)都不够,因此结果不完整。 How do I get all rows rather than few rows here? 如何获取所有行而不是几行?

DECLARE @colsUnpivot VARCHAR(MAX)

SET @colsUnPivot = STUFF((
                       SELECT ',' +  'ISNULL(' + QUOTENAME(name) + ', 0) AS ' 
                                  + QUOTENAME(name) 
                       FROM  sysobjects t  
                       FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') 
PRINT @colsUnPivot

 set @query = 'SELECT id,code_name,lkp_value
 FROM  
 (
 SELECT unitid,institution,city,state,zip, '+ @colsUnpivot+'
  FROM sysobjects) AS cp
  UNPIVOT (lkp_value for code_name  IN ('+@colsUnPivot+')
  ) AS up' 
 --PRINT @Query
 --PRINT @Query1
   exec(@query)

I mean the code above does not make sense but I can not produce same thing that I have as i have to use sysobjects here. 我的意思是上面的代码没有意义,但我不能生成我所拥有的相同的东西,因为我必须在这里使用sysobjects。

But above code is throwing an error: 但上面的代码抛出一个错误:

Msg 102, Level 15, State 1, Line 6 Msg 102,Level 15,State 1,Line 6
Incorrect syntax near '('. '('附近的语法不正确。

And that's because there is so much data and it's being truncated. 那是因为有太多的数据而且它被截断了。

Here is what my PRINT says, 这是我的PRINT所说的,

    ,ISNULL([opd_

So I still think its truncating. 所以我仍然认为它是截断的。

Your problem is that the PRINT command will truncate the data for display in SSMS. 您的问题是PRINT命令将截断数据以便在SSMS中显示。

I would suggest leaving it as XML and doing a SELECT, if you just need to see the data in SSMS without truncating: 如果您只需要在不截断的情况下查看SSMS中的数据,我建议将其保留为XML并执行SELECT:

   DECLARE @colsUnpivot xml

   SET @colsUnPivot = (SELECT ',' +  'ISNULL(' + QUOTENAME(name) + ', 0) AS ' 
                                  + QUOTENAME(name) 
                       FROM  sysobjects t  
                       FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)')

   SELECT @colsUnPivot

SSMS treats XML output differently and has a higher threshold for truncating the data. SSMS以不同方式处理XML输出,并且具有更高的截断数据阈值。

Use SELECT instead of print in your SQL. 在SQL中使用SELECT而不是print。

SELECT @colsUnPivot

Also, make sure that these values are maxed out in Results to Grid: 此外,请确保在结果到网格中最大化这些值: 结果到网格选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM