简体   繁体   English

如何将大于NVARCHAR(MAX)的字符串保存到本地T-SQL变量中?

[英]How to save string greater than NVARCHAR(MAX) into a local T-SQL variable?

I have a very huge results that needs to be provided as an HTML. 我有一个非常巨大的结果,需要以HTML格式提供。

This is the query: 这是查询:

DECLARE @N_Data NVARCHAR(MAX) = ''
SET @N_Data = 
            (SELECT 
                '<td>' + N_code + '</td>'
                + '<td>' + N_Title + '</td>'
                + '<td>' + N_Description + '</td>'
            FROM NTable             
            ORDER BY N_code                 
            FOR XML PATH('tr'))
SELECT @N_Data

I know this is not the best way of getting the data. 我知道这不是获取数据的最佳方法。 Let's say that I am in certain circumstances that limit my options in just using this method. 假设我在某些情况下仅使用此方法会限制我的选择。

So, when I check the results, it doesn't display everything because the result string is greater than NVARCHAR(MAX) 因此,当我检查结果时,它不会显示所有内容,因为结果字符串大于NVARCHAR(MAX)

I know that using TEXT and NTEXT datatypes is not allowed with local variables. 我知道局部变量不允许使用TEXTNTEXT数据类型。

Any idea how to solve this? 任何想法如何解决这个问题?

nvarchar(max) aside, the string concatination caught my eye. 除了nvarchar(max)外,字符串混搭吸引了我的注意。

Perhaps another approach 也许是另一种方法

Example

Declare @YourTable table (ID int,N_Code varchar(25),N_Title varchar(50),N_Description varchar(50))
Insert Into @YourTable values
(1,'ABC','Title For ABC','Description for ABC and some additional text'),
(1,'123','Title For 123','Description for 123 and more text')

Declare @N_Data nvarchar(max) = (
Select  td=N_Code
       ,null
       ,td=N_Title
       ,null
       ,td=N_Description
 From @YourTable
 Order By N_Code
 For XML Path('tr')
)

Select @N_Data

Returns 退货

<tr>
  <td>123</td>
  <td>Title For 123</td>
  <td>Description for 123 and more text</td>
</tr>
<tr>
  <td>ABC</td>
  <td>Title For ABC</td>
  <td>Description for ABC and some additional text</td>
</tr>

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

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