提问人:Sairam Avula 提问时间:3/10/2023 最后编辑:jarlhSairam Avula 更新时间:3/10/2023 访问量:39
动态 SQL 需要支持,其中尝试根据我遇到语法问题的当前月份命名列
Support needed on dynamic SQL where trying to name the columns based on the current month where I get a syntax issue
问:
我正在处理下面的脚本,我正在尝试使用存储过程以在月份内生成 purchaseforcast,但查询正在分组月份,而不是在 MONTH 和 YEAR 中。
目前,查询将 3 月全部分组在一起,而不考虑年份,但我需要按月和年的结果
存储过程:
CREATE PROCEDURE [dbo].[S4SP_MonthlyPfcstReport]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @forecast1 varchar(50);
DECLARE @forecast2 varchar(50);
DECLARE @forecast3 varchar(50);
DECLARE @forecast4 varchar(50);
DECLARE @forecast5 varchar(50);
DECLARE @forecast6 varchar(50);
DECLARE @forecast7 varchar(50);
DECLARE @forecast8 varchar(50);
DECLARE @forecast9 varchar(50);
DECLARE @forecast10 varchar(50);
DECLARE @forecast11 varchar(50);
DECLARE @forecast12 varchar(50);
SET @forecast1 = dbo.GetMonthandYear_Func(MONTH(GETDATE()),YEAR(GETDATE()));
SET @forecast2 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 1, GETDATE())),YEAR(DATEADD(month, 1, GETDATE())));
SET @forecast3 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 2, GETDATE())),YEAR(DATEADD(month, 2, GETDATE())));
SET @forecast4 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 3, GETDATE())),YEAR(DATEADD(month, 3, GETDATE())));
SET @forecast5 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 4, GETDATE())),YEAR(DATEADD(month, 4, GETDATE())));
SET @forecast6 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 5, GETDATE())),YEAR(DATEADD(month, 5, GETDATE())));
SET @forecast7 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 6, GETDATE())),YEAR(DATEADD(month, 6, GETDATE())));
SET @forecast8 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 7, GETDATE())),YEAR(DATEADD(month, 7, GETDATE())));
SET @forecast9 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 8, GETDATE())),YEAR(DATEADD(month, 8, GETDATE())));
SET @forecast10 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 9, GETDATE())),YEAR(DATEADD(month, 9, GETDATE())));
SET @forecast11 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 10, GETDATE())),YEAR(DATEADD(month, 10, GETDATE())));
SET @forecast12 = dbo.GetMonthandYear_Func(MONTH(DATEADD(month, 11, GETDATE())),YEAR(DATEADD(month, 11, GETDATE())));
DECLARE @query nvarchar(max) = '';
SET @query = 'DROP VIEW IF EXISTS [dbo].[CWE_MonthlyPfcstReport_Deliverydate];';
EXEC sp_executesql @query;
SET @query = 'CREATE VIEW [dbo].[CWE_MonthlyPfcstReport_Deliverydate] AS WITH OrderForecast AS (
SELECT *
FROM (
SELECT [articleCode] = pfCode
, [orderRequirements] = toOrder + purchaseForecast
, [orderSequence] = MONTH(orderDate)
, [orderYear] = YEAR(orderDate)
FROM PurchaseForecast
WHERE orderDate <> ''''
) TabSource
PIVOT (
SUM([orderRequirements]) FOR [orderSequence] IN (MONTH(GETDATE()), MONTH(DATEADD(month, 1, GETDATE())), MONTH(DATEADD(month, 2, GETDATE())), MONTH(DATEADD(month, 3, GETDATE())), MONTH(DATEADD(month, 4, GETDATE())), MONTH(DATEADD(month, 5, GETDATE())), MONTH(DATEADD(month, 6, GETDATE())), MONTH(DATEADD(month, 7, GETDATE())), MONTH(DATEADD(month, 8, GETDATE())), MONTH(DATEADD(month, 9, GETDATE())), MONTH(DATEADD(month, 10, GETDATE())), MONTH(DATEADD(month, 11, GETDATE())))
) PivSource
)
SELECT [Code] = article.articleCode
, [Article Description] = [description]
, [Slim4 ABC Class] = abcClass
, [Supplier] = primarySupplier
, [Category] = uD1
, [3] AS ''' + trim(@forecast1) + '''
, [4] AS ''' + trim(@forecast2) + '''
, [5] AS ''' + trim(@forecast3) + '''
, [6] AS ''' + trim(@forecast4) + '''
, [7] AS ''' + trim(@forecast5) + '''
, [8] AS ''' + trim(@forecast6) + '''
, [9] AS ''' + trim(@forecast7) + '''
, [10] AS ''' + trim(@forecast8) + '''
, [11] AS ''' + trim(@forecast9) + '''
, [12] AS ''' + trim(@forecast10) + '''
, [1] AS ''' + trim(@forecast11) + '''
, [2] AS ''' + trim(@forecast12) + '''
FROM Article
LEFT JOIN OrderForecast ON Article.articleCode = OrderForecast.articleCode
WHERE nonStocked = 0
AND OrderForecast.orderYear = YEAR(GETDATE())
AND OrderForecast.orderSequence IN (MONTH(GETDATE()), MONTH(DATEADD(month, 1, GETDATE())), MONTH(DATEADD(month, 2, GETDATE())), MONTH(DATEADD(month, 3, GETDATE())), MONTH(DATEADD(month, 4, GETDATE())), MONTH(DATEADD(month, 5, GETDATE())), MONTH(DATEADD(month, 6, GETDATE())), MONTH(DATEADD(month, 7, GETDATE())), MONTH(DATEADD(month, 8, GETDATE())), MONTH(DATEADD(month, 9, GETDATE())), MONTH(DATEADD(month, 10, GETDATE())), MONTH(DATEADD(month, 11, GETDATE())))
';
--execute dynamic query
exec sp_executesql @query
END
我收到此错误:
消息 102,级别 15,状态 1,过程 CWE_MonthlyPfcstReport_Deliverydate,第 12 行 [批处理开始第 22 行]
“(”附近的语法不正确。
期望的结果:我需要按月和年动态地对数据进行分组。
Code Article Description Slim4 ABC Class Supplier Category Mar 23 Apr 23 May 23 Jun 23 Jul 23 Aug 23 Sep 23 Oct 23 Nov 23 Dec 23 Jan 24 Feb 24
UKGK001966 LOCK Kryptolok STD/Flex Twinpack A S00746 9000 9720 9720 9000 9720 8280 15480 19080 5400 6480 8640 12960
-- =============================================
答: 暂无答案
评论