如何从临时表名中选择,传递给存储过程?
Posted
技术标签:
【中文标题】如何从临时表名中选择,传递给存储过程?【英文标题】:How to SELECT from a temp table name, passed to a stored procedure? 【发布时间】:2019-07-25 06:23:25 【问题描述】:我将临时表name?
作为存储过程的输出,并将创建的临时表的name
作为输入参数传递给另一个存储过程。
我想从 @CalculationGuidTempTableName VARCHAR
中选择 Guid,然后插入到 @CalcsGuids
。
这是我的存储过程:
CREATE PROCEDURE [dbo].[ret_Reports_ProxyVoucherAccounting]
(@CalculationGuidTempTableName VARCHAR(32))
AS
DECLARE @CalcsGuids TABLE ([Guid] UNIQUEIDENTIFIER PRIMARY KEY)
--INSERT INTO @CalcsGuids (SELECT * FROM + @CalculationGuidTempTableName)
--DROP TABLE @CalculationGuidTempTableName
WITH temp AS
(
SELECT
reference.DetailPureTitle,
reference.DetailCaption,
reference.Nature,
reference.Summarize
FROM
@CalcsGuids calcs
INNER JOIN
ret_vwPayrollCalculationBackPayForReport details ON details.CalculationGuid = calcs.[Guid]
LEFT JOIN
ret_PaymentAllocation payment ON payment.Guid = Details.ItemGuid
INNER JOIN
ret_vwFactorReferences reference ON reference.ItemGuid = Details.ItemGuid
INNER JOIN
ret_PayrollCalculationCommands command ON command.[Guid] = details.CalculationCommandGuid
WHERE
command.Approved = 1 AND command.Deleted = 0
GROUP BY
reference.DetailPureTitle,
reference.DetailCaption,
reference.Nature,
reference.Summarize
)
SELECT
*
FROM
temp
WHERE
PayAmount <> 0 OR
DeductionAmount <> 0 OR
EmployerAmount <> 0
ORDER BY
Nature
【问题讨论】:
使用动态 SQL。 @ZoharPeled 很好的提示,我试过了,它奏效了。 INSERT INTO @CalcsGuids EXEC ('SELECT * FROM ' + @CalculationGuidTempTableName); 很高兴为您提供帮助 :-) 请考虑发布问题的答案并在系统允许时接受它,或者删除问题。我敢肯定,如果我们搜索,我们可以在 SO 上找到重复项。 【参考方案1】:试试这个:
INSERT INTO @CalcsGuids EXEC ('SELECT * FROM ' + @CalculationGuidTempTableName);
【讨论】:
以上是关于如何从临时表名中选择,传递给存储过程?的主要内容,如果未能解决你的问题,请参考以下文章