将 SQL 动态数据透视查询的结果集插入临时 Table
Inserting the results set of SQL Dynamic Pivot Query into Temporary Table
如何将列不固定的动态 t-sql 查询的结果插入到全局临时 table 中。
请参阅下面的 table 定义、值插入和用于数据透视查询的 t-sql。
/****** Object: Table [dbo].[ProdOrders] Script Date: 30/8/2017 7:24:35 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProdOrders](
[ItemCode] [nvarchar](50) NULL,
[ReleasedDate] [date] NULL,
[PlanQty] [float] NULL,
[ActualQty] [float] NULL
) ON [PRIMARY]
GO
INSERT INTO [dbo].[ProdOrders]
([ItemCode]
,[ReleasedDate]
,[PlanQty]
,[ActualQty])
VALUES
('0D203-22882-079','2017-08-18',12654,4218),
('0D203-22882-079','2017-08-23',15758,5253),
('0D203-22882-079','2017-08-27',26263,8754),
('0D203-22882-079','2017-09-02',7354,2451),
('0D301-05422-079','2017-08-18',31144,10381),
('0D301-05422-079','2017-08-18',20612,6871),
('0D301-05422-079','2017-08-23',25765,8588),
('0D301-05422-079','2017-08-29',19582,6527),
('0D301-05422-079','2017-09-04',15459,5153),
('0D203-22882-079','2017-09-22',5232,1744),
('0D203-22882-079','2017-09-28',13236,4412),
('0D203-22882-079','2017-10-03',7693,2564),
('0D301-05422-079','2017-09-23',24735,8245),
('0D301-05422-079','2017-09-27',19561,6520),
('0D301-05422-079','2017-09-06',23755,7918),
('0D301-05422-079','2017-09-14',23755,7918),
('0D301-05422-079','2017-09-17',29694,9898),
('0D203-22882-079','2017-11-01',2263,754),
('0D203-22882-079','2017-10-21',15693,5231),
('0D203-22882-079','2017-10-20',15968,5323),
('0D203-22882-079','2017-10-25',10521,3507),
('0D301-05422-079','2017-10-21',23755,7918),
('0D301-05422-079','2017-10-29',17816,5939),
('0D301-05422-079','2017-11-01',15612,5204),
('0D301-05422-079','2017-10-03',20816,6939),
('0D301-05422-079','2017-10-11',15612,5204),
('0D301-05422-079','2017-10-18',26020,8673)
Declare @SQL varchar(max) = '
Select *
From (
Select A.ItemCode
,B.*
From [dbo].[ProdOrders] A
Cross Apply ( values ( convert(varchar(6),ReleasedDate,112)+''-Plan'',PlanQty)
,( convert(varchar(6),ReleasedDate,112)+''-Actual'',ActualQty)
) B (Item,Value)
) S
Pivot (sum([Value]) For [Item] in (' + Stuff((Select Distinct ','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Plan')
+','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Actual')
From [dbo].[ProdOrders]
Order By 1
For XML Path('')),1,1,'') + ') ) p'
Exec(@SQL);
我需要将结果集插入全局临时 table 以便我可以使用它来引用另一个查询。
在您的 select 语句中添加 into 子句并尝试
Declare @SQL varchar(max) = '
Select *
into ##globaltemptable
From ( ...
下面的脚本动态创建一个全局临时文件 table。这个脚本没有检查 table 是否已经存在,你必须自己添加。
小心使用全局温度 tables。我认为有比您想要的更好的选择。
--Script for creating global temp table
Declare @CreateTempTable [nvarchar](max) = '
CREATE TABLE ##t ([ItemCode] nvarchar(50),' +
stuff((Select distinct ',' + QuoteName(convert(varchar(6), ReleasedDate,112)+'-Plan') + ' float,' + QuoteName(convert(varchar(6),ReleasedDate,112)+'-Actual') + ' float'
From [dbo].[ProdOrders]
Order By 1 For XML Path('')),1,1, '') + ')'
Exec sp_executesql @CreateTempTable
--Script for insert
Declare @InsertSql [nvarchar](max) = '
INSERT INTO ##t ([ItemCode], ' +
stuff((Select distinct ','+QuoteName(convert(varchar(6), ReleasedDate,112)+'-Plan') + ', ' + QuoteName(convert(varchar(6),ReleasedDate,112)+'-Actual') + ' '
From [dbo].[ProdOrders]
Order By 1 For XML Path('')),1,1, '') + ')'
Declare @SQL varchar(max) = @InsertSql + '
Select *
From (
Select A.ItemCode
,B.*
From [dbo].[ProdOrders] A
Cross Apply ( values ( convert(varchar(6),ReleasedDate,112)+''-Plan'',PlanQty)
,( convert(varchar(6),ReleasedDate,112)+''-Actual'',ActualQty)
) B (Item,Value)
) S
Pivot (sum([Value]) For [Item] in (' + Stuff((Select Distinct ','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Plan')
+','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Actual')
From [dbo].[ProdOrders]
Order By 1
For XML Path('')),1,1,'') + ') ) p'
Exec(@SQL);
Select * From ##t
如何将列不固定的动态 t-sql 查询的结果插入到全局临时 table 中。 请参阅下面的 table 定义、值插入和用于数据透视查询的 t-sql。
/****** Object: Table [dbo].[ProdOrders] Script Date: 30/8/2017 7:24:35 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProdOrders](
[ItemCode] [nvarchar](50) NULL,
[ReleasedDate] [date] NULL,
[PlanQty] [float] NULL,
[ActualQty] [float] NULL
) ON [PRIMARY]
GO
INSERT INTO [dbo].[ProdOrders]
([ItemCode]
,[ReleasedDate]
,[PlanQty]
,[ActualQty])
VALUES
('0D203-22882-079','2017-08-18',12654,4218),
('0D203-22882-079','2017-08-23',15758,5253),
('0D203-22882-079','2017-08-27',26263,8754),
('0D203-22882-079','2017-09-02',7354,2451),
('0D301-05422-079','2017-08-18',31144,10381),
('0D301-05422-079','2017-08-18',20612,6871),
('0D301-05422-079','2017-08-23',25765,8588),
('0D301-05422-079','2017-08-29',19582,6527),
('0D301-05422-079','2017-09-04',15459,5153),
('0D203-22882-079','2017-09-22',5232,1744),
('0D203-22882-079','2017-09-28',13236,4412),
('0D203-22882-079','2017-10-03',7693,2564),
('0D301-05422-079','2017-09-23',24735,8245),
('0D301-05422-079','2017-09-27',19561,6520),
('0D301-05422-079','2017-09-06',23755,7918),
('0D301-05422-079','2017-09-14',23755,7918),
('0D301-05422-079','2017-09-17',29694,9898),
('0D203-22882-079','2017-11-01',2263,754),
('0D203-22882-079','2017-10-21',15693,5231),
('0D203-22882-079','2017-10-20',15968,5323),
('0D203-22882-079','2017-10-25',10521,3507),
('0D301-05422-079','2017-10-21',23755,7918),
('0D301-05422-079','2017-10-29',17816,5939),
('0D301-05422-079','2017-11-01',15612,5204),
('0D301-05422-079','2017-10-03',20816,6939),
('0D301-05422-079','2017-10-11',15612,5204),
('0D301-05422-079','2017-10-18',26020,8673)
Declare @SQL varchar(max) = '
Select *
From (
Select A.ItemCode
,B.*
From [dbo].[ProdOrders] A
Cross Apply ( values ( convert(varchar(6),ReleasedDate,112)+''-Plan'',PlanQty)
,( convert(varchar(6),ReleasedDate,112)+''-Actual'',ActualQty)
) B (Item,Value)
) S
Pivot (sum([Value]) For [Item] in (' + Stuff((Select Distinct ','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Plan')
+','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Actual')
From [dbo].[ProdOrders]
Order By 1
For XML Path('')),1,1,'') + ') ) p'
Exec(@SQL);
我需要将结果集插入全局临时 table 以便我可以使用它来引用另一个查询。
在您的 select 语句中添加 into 子句并尝试
Declare @SQL varchar(max) = '
Select *
into ##globaltemptable
From ( ...
下面的脚本动态创建一个全局临时文件 table。这个脚本没有检查 table 是否已经存在,你必须自己添加。 小心使用全局温度 tables。我认为有比您想要的更好的选择。
--Script for creating global temp table
Declare @CreateTempTable [nvarchar](max) = '
CREATE TABLE ##t ([ItemCode] nvarchar(50),' +
stuff((Select distinct ',' + QuoteName(convert(varchar(6), ReleasedDate,112)+'-Plan') + ' float,' + QuoteName(convert(varchar(6),ReleasedDate,112)+'-Actual') + ' float'
From [dbo].[ProdOrders]
Order By 1 For XML Path('')),1,1, '') + ')'
Exec sp_executesql @CreateTempTable
--Script for insert
Declare @InsertSql [nvarchar](max) = '
INSERT INTO ##t ([ItemCode], ' +
stuff((Select distinct ','+QuoteName(convert(varchar(6), ReleasedDate,112)+'-Plan') + ', ' + QuoteName(convert(varchar(6),ReleasedDate,112)+'-Actual') + ' '
From [dbo].[ProdOrders]
Order By 1 For XML Path('')),1,1, '') + ')'
Declare @SQL varchar(max) = @InsertSql + '
Select *
From (
Select A.ItemCode
,B.*
From [dbo].[ProdOrders] A
Cross Apply ( values ( convert(varchar(6),ReleasedDate,112)+''-Plan'',PlanQty)
,( convert(varchar(6),ReleasedDate,112)+''-Actual'',ActualQty)
) B (Item,Value)
) S
Pivot (sum([Value]) For [Item] in (' + Stuff((Select Distinct ','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Plan')
+','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Actual')
From [dbo].[ProdOrders]
Order By 1
For XML Path('')),1,1,'') + ') ) p'
Exec(@SQL);
Select * From ##t