在 Sql Server 2008 中处理较大的字符串
我们创建了一个存储过程,以便用户可以在其软件产品的管理中编写逗号分隔的搜索标签。因此,他可以添加逗号分隔的标签,如果他想编辑它们,我们会从表中读取所有标签,在存储过程中将它们重新创建为逗号分隔值 (CSV),并将其返回给调用代码。最近发生的事情,用户抱怨他看不到自己编写的新 CSV。我调查了一下,发现存储过程在从数据库读取值并创建 CSV 字符串时会截断字符串。该字符串的类型为 nvarchar,并且由于它超过了最大字符数 4000 个限制,因此值会被截断。关于如何解决该问题的任何想法。
在下面找到我的代码。
BEGIN
BEGIN
Declare @Synonyms Table
(
RowID int Identity(1,1),
SynonymID int,
[Synonym] nvarchar(4000)
);
SET NOCOUNT ON;
Insert @Synonyms(SynonymID, [Synonym])
Select distinct SynonymID, [Synonym] From RF_SearchSynonyms with(nolock) Where SearchTermID = @SearchTermID And ActiveInd = 1
If((Select COUNT(RowID) From @Synonyms) <> 0)
BEGIN
Declare @CurrentRow int = (Select MIN(RowID) From @Synonyms),
@TotalRows int = (Select MAX(RowID) From @Synonyms),
@Synonyms_CSV nvarchar(4000) = '';
WHILE @CurrentRow <= @TotalRows
BEGIN
Declare @TempSyn nvarchar(500);
Select @TempSyn = [Synonym] + ',' From @Synonyms Where RowID = @CurrentRow;
Set @Synonyms_CSV = @Synonyms_CSV + LTRIM(RTRIM(LOWER(@TempSyn)));
SET @CurrentRow = @CurrentRow + 1
END
END
Else
BEGIN
Set @Synonyms_CSV = '';
END
END
BEGIN
Declare @SKUs Table
(
RowID int Identity(1,1),
SkuID int,
SKU nvarchar(15)
);
SET NOCOUNT ON;
Insert @SKUs(SkuID, SKU)
Select distinct SkuID, SKU From RF_SearchSkus with(nolock) Where SearchTermID = @SearchTermID And ActiveInd = 1
If((Select COUNT(RowID) From @SKUs) <> 0)
BEGIN
Declare @CurrentRow1 int = (Select MIN(RowID) From @SKUs),
@TotalRows1 int = (Select MAX(RowID) From @SKUs),
@Skus_CSV nvarchar(4000) = '';
WHILE @CurrentRow1 <= @TotalRows1
BEGIN
Declare @TempSku nvarchar(15);
Select @TempSku = SKU + ',' From @SKUs Where RowID = @CurrentRow1;
Set @Skus_CSV = @Skus_CSV + LTRIM(RTRIM(@TempSku));
SET @CurrentRow1 = @CurrentRow1 + 1
END
END
Else
BEGIN
Set @Skus_CSV = '';
END
END
BEGIN
Declare @Combined varchar(8000),
@syn_len int = 0,
@sku_len int = 0;
Select @syn_len = LEN(@Synonyms_CSV);
Select @sku_len = LEN(@Skus_CSV);
Select @Combined = @Synonyms_CSV + '-_-' + @Skus_CSV;
Select @Synonyms_CSV + '-_-' + @Skus_CSV;
END
我无法
使用 text 和 ntext,因为它们不能很好地进行串联操作。
谢谢。
We have an stored procedure that we created so that user can write comma separated search tags in their software product's admin. So he can add comma-separated tags and in case if he wants to edit them, we read from the table all the tags, recreate them as comma-separated values (CSV) in stored procedure and returns that to the calling code. What happened recently, the user complained that he could not see the new CSVs he wrote. I looked into it and found out that the stored procedure is truncating the string when it reads values from database and creates CSV string. The string is of type nvarchar, and because its exceeding the max characters of 4000 limit, the values gets truncated. Any ideas on how to work out that problem.
Find my code underneath.
BEGIN
BEGIN
Declare @Synonyms Table
(
RowID int Identity(1,1),
SynonymID int,
[Synonym] nvarchar(4000)
);
SET NOCOUNT ON;
Insert @Synonyms(SynonymID, [Synonym])
Select distinct SynonymID, [Synonym] From RF_SearchSynonyms with(nolock) Where SearchTermID = @SearchTermID And ActiveInd = 1
If((Select COUNT(RowID) From @Synonyms) <> 0)
BEGIN
Declare @CurrentRow int = (Select MIN(RowID) From @Synonyms),
@TotalRows int = (Select MAX(RowID) From @Synonyms),
@Synonyms_CSV nvarchar(4000) = '';
WHILE @CurrentRow <= @TotalRows
BEGIN
Declare @TempSyn nvarchar(500);
Select @TempSyn = [Synonym] + ',' From @Synonyms Where RowID = @CurrentRow;
Set @Synonyms_CSV = @Synonyms_CSV + LTRIM(RTRIM(LOWER(@TempSyn)));
SET @CurrentRow = @CurrentRow + 1
END
END
Else
BEGIN
Set @Synonyms_CSV = '';
END
END
BEGIN
Declare @SKUs Table
(
RowID int Identity(1,1),
SkuID int,
SKU nvarchar(15)
);
SET NOCOUNT ON;
Insert @SKUs(SkuID, SKU)
Select distinct SkuID, SKU From RF_SearchSkus with(nolock) Where SearchTermID = @SearchTermID And ActiveInd = 1
If((Select COUNT(RowID) From @SKUs) <> 0)
BEGIN
Declare @CurrentRow1 int = (Select MIN(RowID) From @SKUs),
@TotalRows1 int = (Select MAX(RowID) From @SKUs),
@Skus_CSV nvarchar(4000) = '';
WHILE @CurrentRow1 <= @TotalRows1
BEGIN
Declare @TempSku nvarchar(15);
Select @TempSku = SKU + ',' From @SKUs Where RowID = @CurrentRow1;
Set @Skus_CSV = @Skus_CSV + LTRIM(RTRIM(@TempSku));
SET @CurrentRow1 = @CurrentRow1 + 1
END
END
Else
BEGIN
Set @Skus_CSV = '';
END
END
BEGIN
Declare @Combined varchar(8000),
@syn_len int = 0,
@sku_len int = 0;
Select @syn_len = LEN(@Synonyms_CSV);
Select @sku_len = LEN(@Skus_CSV);
Select @Combined = @Synonyms_CSV + '-_-' + @Skus_CSV;
Select @Synonyms_CSV + '-_-' + @Skus_CSV;
END
END
I can't use text and ntext as they do not play nice with concatenation operations.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您如何声明字符串参数?
最多支持 2^32-1 (2GB)
请参阅此链接。
How are your declaring the string parameter?
supports up to 2^32-1 (2GB)
See this link.