我正在使用Microsoft SQL Server 2005
我创建了用于生成报告的存储过程。
ASP.NET from用于传递存储过程的参数。
问题是它没有将所有PartNumber选为参数。它仅获取225 PartNumber并仅为那些partNumber生成报告,但实际上我有超过700个PartNumber。
4000是nvarchar的最大大小(因为我试图扩展它并且它不接受超过4000)。 Xml变量没有限制。
PartNumber
314013
314039
314047
314054
314062
314070
314088
314096
314104
314344
314351
314377
314393
USE [Reportbox]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spCreateReport2]
(@prod nvarchar(4000))
AS
BEGIN
DECLARE @sqlQuery nvarchar(4000)
declare @intMonth nvarchar(2)
declare @intMonth1 nvarchar(2)
declare @intMonth2 nvarchar(2)
declare @intMonth3 nvarchar(2)
declare @intMonth4 nvarchar(2)
declare @intMonth5 nvarchar(2)
SELECT @intMonth = fiscalmonth FROM FiscalWeeks WHERE CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME) between weekstart and weekend
if @intmonth-1<=0 BEGIN set @intmonth1=@intmonth-1+12 END else set @intMonth1=@intMonth-1
if @intmonth-2<=0 BEGIN set @intmonth2=@intmonth-2+12 END else set @intmonth2=@intmonth-2
if @intmonth-3<=0 BEGIN set @intmonth3=@intmonth-3+12 END else set @intmonth3=@intmonth-3
if @intmonth-4<=0 BEGIN set @intmonth4=@intmonth-4+12 END else set @intmonth4=@intmonth-4
if @intmonth-5<=0 BEGIN set @intmonth5=@intmonth-5+12 END else set @intmonth5=@intmonth-5
Set @sqlQuery =
'select distinct d.PROD80, DESC80, SUM_AVGU80, AVGU80, SUM_ONHA80, d.ONHA80, SUM_SAFE80, d.SAFE80,
MAX_FOQT80, MAX_ABCC80, MAX_MINQ80,
SUM_WEEK_NO_PO, SUM_TRIG80, d.TRIG80, SUM_BQTY80, DQTY80, OQTY80,
CASE WHEN d.AVGU80<>0 THEN CAST(ROUND(d.ONHA80/d.AVGU80,0) AS INT) ELSE 0 END AS WKS30
FROM DI80ACT00 d INNER JOIN vwDataAtCompanyLevel v on (d.prod80=v.prod80)
LEFT OUTER JOIN DI80ACT01 t on (d.prod80=t.prod80) and t.ware80 <> ''30''
WHERE d.prod80 in (' + @prod + ') and d.prod80<>''HLW54B'' and d.ware80=''30''
GROUP BY d.PROD80, DESC80, SUM_AVGU80, AVGU80, SUM_ONHA80, d.ONHA80, SUM_SAFE80, d.SAFE80,
MAX_FOQT80, MAX_ABCC80, MAX_MINQ80,SUM_HSTA80,SUM_HSTB80,SUM_HSTC80,SUM_HSTD80,SUM_HSTE80,SUM_HSTF80,SUM_POOR80,POOR80,SUM_TWIN80,SUM_WEEK_No_PO,SUM_TRIG80,TRIG80,SUM_BQTY80,DQTY80,OQTY80'
exec(@sqlQuery)
END
任何帮助或详细信息都会非常有用 谢谢
答案 0 :(得分:0)
使用支持超过10亿个字符的NVARCHAR(MAX)
。但是,我真的不希望您对{700}值的IN()
查询表现得非常好。
如果他们选择ALL
,则将字符串'ALL'
传递给参数,并且当未选择{{1}时,仅包含IN()
列表}}。所以在SQL中:
ALL
没有理由使用大量Set @sqlQuery = 'select distinct
...
WHERE d.prod80<>''HLW54B'' and d.ware80=''30'''
+ CASE WHEN @prod = 'ALL' THEN '' ELSE
'AND d.prod80 in (' + @prod + ')' END
+ 'GROUP BY ...'
子句列出表中的每个值。