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

问题描述 投票:0回答:1

我想将动态数据结果导入临时表,通过该表可以使用select查询将其发送回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列的结果。这取决于查询的结果。这个结果我想在临时表上使用更多这个数据。我想在存储过程中使用它将结果发送回带有Entity Framework的ASP.NET MVC 5

sql-server sql-server-2008
1个回答
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

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

© www.soinside.com 2019 - 2024. All rights reserved.