将动态数据透视表结果获取到临时表SQL Server中

时间:2019-03-03 11:50:22

标签: sql-server sql-server-2008

我想将动态数据透视结果保存到temp表中,通过它可以使用选择查询将其发送回C#代码。

DECLARE @partymasterid bigint = 2;
DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query  AS NVARCHAR(MAX);

SELECT 
    @cols = STUFF((SELECT DISTINCT '[' + CAST(dbo.InventoryProductMaster.Name AS nvarchar(max)) + '],' 
                   FROM dbo.InventoryBiltyMaster 
                   INNER JOIN dbo.InventoryPartyProductPriceMaster ON dbo.InventoryBiltyMaster.InventoryPartyProductPriceMasterID = dbo.InventoryPartyProductPriceMaster.ID 
                   INNER JOIN dbo.InventoryProductMaster ON dbo.InventoryPartyProductPriceMaster.InventoryProductMasterID = dbo.InventoryProductMaster.ID 
                   WHERE dbo.InventoryBiltyMaster.PartyMasterID = @partymasterid 
                   FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 0, '');

SELECT @query = 
'
SELECT
*
FROM 
(SELECT
Count(dbo.InventoryBiltyMaster.ID) AS BiltyCount,
--dbo.InventoryBiltyMaster.InventoryProductMasterID,
--dbo.InventoryBiltyMaster.VehicleMasterID,
dbo.InventoryProductMaster.Name,
dbo.VehicleMaster.VehicleNumber

FROM
dbo.InventoryBiltyMaster
INNER JOIN dbo.InventoryPartyProductPriceMaster ON dbo.InventoryBiltyMaster.InventoryPartyProductPriceMasterID = dbo.InventoryPartyProductPriceMaster.ID
INNER JOIN dbo.InventoryProductMaster ON dbo.InventoryPartyProductPriceMaster.InventoryProductMasterID = dbo.InventoryProductMaster.ID
INNER JOIN dbo.VehicleMaster ON dbo.InventoryBiltyMaster.VehicleMasterID = dbo.VehicleMaster.ID
WHERE
dbo.InventoryBiltyMaster.PartyMasterID = ' + CAST(@partymasterid as nvarchar(50)) + '
GROUP BY
dbo.InventoryBiltyMaster.InventoryProductMasterID,
dbo.InventoryProductMaster.Name,
dbo.InventoryBiltyMaster.VehicleMasterID,
dbo.VehicleMaster.VehicleNumber
)
AS S
PIVOT
(
    MAX(BiltyCount)
    FOR [Name] IN (' + LEFT(@cols, LEN(@cols)-1) + ')
)AS pvt';

EXEC SP_EXECUTESQL @query

这里可能有3列或5列或10-15列。它取决于查询的结果。这个结果我想在临时表上使用更多此数据。 我想在存储过程中使用它,以将结果发送回带有实体框架的ASP.NET MVC 5

1 个答案:

答案 0 :(得分:1)

使用#时,如果不在父范围内定义sp_executesql(临时表),则无法存储在其中。在您的情况下,您有一个动态的pivot,但您不知道将要包含哪些列和多少列。

sp_executesql在不同的会话中运行(sp_executesql创建自己的会话),而临时表是特定于会话的。

对于您的方案,您可以使用##(全局临时表)。您可以按以下方式更改查询。

SELECT @query = 
'
SELECT
* into ##temp
FROM 
(SELECT
Count(dbo.InventoryBiltyMaster.ID) AS BiltyCount,
--dbo.InventoryBiltyMaster.InventoryProductMasterID,
--dbo.InventoryBiltyMaster.VehicleMasterID,
dbo.InventoryProductMaster.Name,
dbo.VehicleMaster.VehicleNumber

FROM
dbo.InventoryBiltyMaster
INNER JOIN dbo.InventoryPartyProductPriceMaster ON dbo.InventoryBiltyMaster.InventoryPartyProductPriceMasterID = dbo.InventoryPartyProductPriceMaster.ID
INNER JOIN dbo.InventoryProductMaster ON dbo.InventoryPartyProductPriceMaster.InventoryProductMasterID = dbo.InventoryProductMaster.ID
INNER JOIN dbo.VehicleMaster ON dbo.InventoryBiltyMaster.VehicleMasterID = dbo.VehicleMaster.ID
WHERE
dbo.InventoryBiltyMaster.PartyMasterID = ' + CAST(@partymasterid as nvarchar(50)) + '
GROUP BY
dbo.InventoryBiltyMaster.InventoryProductMasterID,
dbo.InventoryProductMaster.Name,
dbo.InventoryBiltyMaster.VehicleMasterID,
dbo.VehicleMaster.VehicleNumber
)
AS S
PIVOT
(
    MAX(BiltyCount)
    FOR [Name] IN (' + LEFT(@cols, LEN(@cols)-1) + ')
)AS pvt';

EXEC SP_EXECUTESQL @query
--now you can use ##temp

注意:如果从多个会话中进行更新,则使用全局临时表可能导致无法预测的行为,您可能会考虑为每个会话指定一个唯一的名称。