Hey all!
I have two similar SQL statements (bold represents a different param):
1)
SELECT REC_CI.CashItem, REC_CI.Position, REC_CI.DisplayName, REC_CI.Parent, REC_CI.OrderBy,
PivotData.* FROM (SELECT PivotCashItem, Month_Period, Amount / 1 AS Amount
FROM dbo.V35_FORCAST_DIFF_Get_PL_PG_Forcast_Actual_Difference (500, 400,
16, 41, 7, 'Aug 1 2013 12:00AM', 'Oct 31 2013 12:00AM', NULL, 'ILS', 14, 1)) Data
PIVOT(MAX(Amount) FOR Month_Period IN ([Forcast 8/2013],[Actual 8/2013],[Differences 8/2013],[Forcast 9/2013],[Actual 9/2013],[Differences 9/2013],[Forcast 10/2013],[Actual 10/2013],[Differences 10/2013])) PivotData
RIGHT OUTER JOIN dbo.V35_CASHITEMS_Get_SubCashItems(500, NULL) REC_CI ON REC_CI.CashItem = PivotData.PivotCashItem
2)
SELECT REC_CI.CashItem, REC_CI.Position, REC_CI.DisplayName, REC_CI.Parent, REC_CI.OrderBy,
PivotData.* FROM (SELECT PivotCashItem, Month_Period, Amount / 1 AS Amount
FROM dbo.V35_FORCAST_DIFF_Get_PL_PG_Forcast_Actual_Difference (500, 400, NULL, 41, 7, 'Aug 1 2013 12:00AM', 'Oct 31 2013 12:00AM', NULL, 'ILS', 14, 1)) Data
PIVOT(MAX(Amount) FOR Month_Period IN ([Forcast 8/2013],[Actual 8/2013],[Differences 8/2013],[Forcast 9/2013],[Actual 9/2013],[Differences 9/2013],[Forcast 10/2013],[Actual 10/2013],[Differences 10/2013])) PivotData
RIGHT OUTER JOIN dbo.V35_CASHITEMS_Get_SubCashItems(500, NULL) REC_CI ON REC_CI.CashItem = PivotData.PivotCashItem
Each statement runs as expected and returns 129 rows!
The problem begins when I try to insert these rows into a temp table:
IF OBJECT_ID('tempdb..##V3_TMP_ACTUAL_MATRIX_06D18928_AA8F_45AB_A518_FE88AE40B2B0') IS NOT NULL DROP TABLE ##V3_TMP_ACTUAL_MATRIX_06D18928_AA8F_45AB_A518_FE88AE40B2B0CREATE TABLE ##V3_TMP_ACTUAL_MATRIX_06D18928_AA8F_45AB_A518_FE88AE40B2B0(
[CashItem] [int] NOT NULL,
[Position] [int] NULL,
[DisplayName] [nvarchar](50) NULL,
[Parent] [int] NOT NULL,
[OrderBy] [int],
[PivotCashItem] [int],[Forcast 8/2013] [decimal](18, 2) NULL,[Actual 8/2013] [decimal](18, 2) NULL,[Differences 8/2013] [decimal](18, 2) NULL,[Forcast 9/2013] [decimal](18, 2) NULL,[Actual 9/2013] [decimal](18, 2) NULL,[Differences 9/2013] [decimal](18, 2) NULL,[Forcast 10/2013] [decimal](18, 2) NULL,[Actual 10/2013] [decimal](18, 2) NULL,[Differences 10/2013] [decimal](18, 2) NULL CONSTRAINT [PK_##V3_TMP_ACTUAL_MATRIX_06D18928_AA8F_45AB_A518_FE88AE40B2B0]
PRIMARY KEY CLUSTERED
([CashItem] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = OFF, ALLOW_PAGE_LOCKS = OFF) ON [PRIMARY]) ON [PRIMARY]
INSERT INTO ##V3_TMP_ACTUAL_MATRIX_06D18928_AA8F_45AB_A518_FE88AE40B2B0
... (1/2)
I get the following error for inserting statement number 1:
Msg 8623, Level 16, State 1, Line 9The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information.
Any ideas why it only happens for the insert statement?
Microsoft SQL Server 2008 (SP2) - 10.0.4000.0 (X64) Sep 16 2010 19:43:16 Copyright (c) 1988-2008 Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 5.2 <X64> (Build 3790: Service Pack 2) (VM)