在 sql 2000 中存储大于 4000 的 nvarchar
store nvarchar larger than 4000 in sql 2000
我正在尝试使用@SumSQL var type nvarchar 4000 执行sql,当我在我的存储过程中输入一个大于特定值(如“11”)的参数值时,程序return sql 错误...问题来了,当 sql 将 char 存储在 var @SumSQL 中时,它的大小变得大于 4000 然后它之后什么都不存储,在 sql 2008 if我将 nvarchar (4000) 的类型更改为 nvarchar (max) 它工作得很好,但这在 sql 2000 中不存在这里是 sql 存储的部分:
DECLARE @SQL AS [nvarchar](4000)
DECLARE @SumSQL AS [nvarchar](4000)
SET @SumSQL = ''
SET @SQL = ' ALTER TABLE [#t] ADD [TotalRemainingPrice] [FLOAT], [TotalRemainingQnt] [FLOAT], [MatUnitName] [nvarchar](256) COLLATE ARABIC_CI_AI, [AvgQty] [FLOAT], [AvgPrice] [FLOAT], [Price] [FLOAT]'
WHILE @PeriodCounter < @NumOfPeriods
BEGIN
SET @SQL = @SQL + ', [P' + CAST((@PeriodCounter+1) AS [nvarchar](10)) + '] [FLOAT]'
SET @SQL = @SQL + ', [r' + CAST((@PeriodCounter+1) AS [nvarchar](10)) + '] [FLOAT]'
SET @PeriodStart = @PeriodCounter * @PeriodLength
SET @PeriodEnd = @PeriodStart + @PeriodLength
IF @PeriodCounter = (@NumOfPeriods - 1)
SET @SumSQL = @SumSQL + ', ISNULL((SELECT SUM( [Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND [t_inner].[Age] >' + CAST(@PeriodStart AS [nvarchar](10)) + '), 0)'
+
', ISNULL((SELECT SUM( [Price] * [Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10)) + '), 0)'
ELSE
BEGIN
IF @PeriodCounter = 0
SET @SumSQL = @SumSQL + ', ISNULL((SELECT SUM([Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND ( [t_inner].[Age] = 0 OR ( [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10))
+ ' AND [t_inner].[Age] <= ' + CAST(@PeriodEnd AS [nvarchar](10)) + '))), 0)'
+
', ISNULL((SELECT SUM([Price] * [Remaining]) FROM [#In_Result] [t_inner] '
+ ' WHERE [t_inner].[MatID] = [t_outer].[MatID]'
+ ' AND ( [t_inner].[Age] = 0 OR ( [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10))
+ ' AND [t_inner].[Age] <= ' + CAST(@PeriodEnd AS [nvarchar](10)) + '))), 0)'
ELSE
SET @SumSQL = @SumSQL + ' , ISNULL((SELECT SUM([Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10)) + ' AND t_inner.Age <= ' + CAST(@PeriodEnd AS [nvarchar](10)) + '), 0)'
+ ' , ISNULL((SELECT SUM([Price] * [Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10)) + ' AND [t_inner].[Age] <= ' + CAST(@PeriodEnd AS [nvarchar](10)) + '), 0)'
END
SET @PeriodCounter = @PeriodCounter + 1
END
EXEC( @SQL)
DECLARE @SqlInsert AS [nvarchar](4000)
DECLARE @SqlInsert2 AS [nvarchar](4000)
SET @SqlInsert = ' INSERT INTO [#t] SELECT [MatID], SUM([Price] * [Remaining]), SUM([Remaining]), [MatUnitName], [AvgQty], [AvgPrice], [Price1] ' --+ @SumSQL +
SET @SqlInsert2 = ' FROM [#In_Result] [t_outer] GROUP BY [MatID], [MatUnitName],[AvgQty], [AvgPrice],[Price1]'
EXEC( @SqlInsert+@SumSQL+@SqlInsert2) <--- here is where to execute stored nvarchar !
那么在 sql 2000 中存储大于 4000 的 nvarchar 的最佳方法是什么?
如果你真的必须使用2000版本,你最好的选择是将数据类型从nvarchar(4000)
更改为nText
。
如果可能的话,你应该迫使你的老板/客户升级到更新版本的 sql 服务器。
我正在尝试使用@SumSQL var type nvarchar 4000 执行sql,当我在我的存储过程中输入一个大于特定值(如“11”)的参数值时,程序return sql 错误...问题来了,当 sql 将 char 存储在 var @SumSQL 中时,它的大小变得大于 4000 然后它之后什么都不存储,在 sql 2008 if我将 nvarchar (4000) 的类型更改为 nvarchar (max) 它工作得很好,但这在 sql 2000 中不存在这里是 sql 存储的部分:
DECLARE @SQL AS [nvarchar](4000)
DECLARE @SumSQL AS [nvarchar](4000)
SET @SumSQL = ''
SET @SQL = ' ALTER TABLE [#t] ADD [TotalRemainingPrice] [FLOAT], [TotalRemainingQnt] [FLOAT], [MatUnitName] [nvarchar](256) COLLATE ARABIC_CI_AI, [AvgQty] [FLOAT], [AvgPrice] [FLOAT], [Price] [FLOAT]'
WHILE @PeriodCounter < @NumOfPeriods
BEGIN
SET @SQL = @SQL + ', [P' + CAST((@PeriodCounter+1) AS [nvarchar](10)) + '] [FLOAT]'
SET @SQL = @SQL + ', [r' + CAST((@PeriodCounter+1) AS [nvarchar](10)) + '] [FLOAT]'
SET @PeriodStart = @PeriodCounter * @PeriodLength
SET @PeriodEnd = @PeriodStart + @PeriodLength
IF @PeriodCounter = (@NumOfPeriods - 1)
SET @SumSQL = @SumSQL + ', ISNULL((SELECT SUM( [Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND [t_inner].[Age] >' + CAST(@PeriodStart AS [nvarchar](10)) + '), 0)'
+
', ISNULL((SELECT SUM( [Price] * [Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10)) + '), 0)'
ELSE
BEGIN
IF @PeriodCounter = 0
SET @SumSQL = @SumSQL + ', ISNULL((SELECT SUM([Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND ( [t_inner].[Age] = 0 OR ( [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10))
+ ' AND [t_inner].[Age] <= ' + CAST(@PeriodEnd AS [nvarchar](10)) + '))), 0)'
+
', ISNULL((SELECT SUM([Price] * [Remaining]) FROM [#In_Result] [t_inner] '
+ ' WHERE [t_inner].[MatID] = [t_outer].[MatID]'
+ ' AND ( [t_inner].[Age] = 0 OR ( [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10))
+ ' AND [t_inner].[Age] <= ' + CAST(@PeriodEnd AS [nvarchar](10)) + '))), 0)'
ELSE
SET @SumSQL = @SumSQL + ' , ISNULL((SELECT SUM([Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10)) + ' AND t_inner.Age <= ' + CAST(@PeriodEnd AS [nvarchar](10)) + '), 0)'
+ ' , ISNULL((SELECT SUM([Price] * [Remaining]) FROM [#In_Result] [t_inner] WHERE [t_inner].[MatID] = [t_outer].[MatID] AND [t_inner].[Age] > ' + CAST(@PeriodStart AS [nvarchar](10)) + ' AND [t_inner].[Age] <= ' + CAST(@PeriodEnd AS [nvarchar](10)) + '), 0)'
END
SET @PeriodCounter = @PeriodCounter + 1
END
EXEC( @SQL)
DECLARE @SqlInsert AS [nvarchar](4000)
DECLARE @SqlInsert2 AS [nvarchar](4000)
SET @SqlInsert = ' INSERT INTO [#t] SELECT [MatID], SUM([Price] * [Remaining]), SUM([Remaining]), [MatUnitName], [AvgQty], [AvgPrice], [Price1] ' --+ @SumSQL +
SET @SqlInsert2 = ' FROM [#In_Result] [t_outer] GROUP BY [MatID], [MatUnitName],[AvgQty], [AvgPrice],[Price1]'
EXEC( @SqlInsert+@SumSQL+@SqlInsert2) <--- here is where to execute stored nvarchar !
那么在 sql 2000 中存储大于 4000 的 nvarchar 的最佳方法是什么?
如果你真的必须使用2000版本,你最好的选择是将数据类型从nvarchar(4000)
更改为nText
。
如果可能的话,你应该迫使你的老板/客户升级到更新版本的 sql 服务器。