存储过程中定义的临时表的无效对象名称错误

时间:2019-06-17 17:09:01

标签: sql-server

我已经编写了一个存储过程,该存储过程执行一个存储过程,并且获得的结果必须存储在本地临时表中。创建存储过程时不会给出任何错误。但是当我尝试执行存储过程时,它会返回错误消息,即临时表是无效的对象名。

CREATE PROCEDURE .dbo.CalulateETFWeights
-- Add the parameters for the stored procedure here
@CURR_DATE varchar(255),
@ETF_DATE datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

--Select max(ETF_DATE) into @ETF_DATE from .dbo.ETF_LIST_V --where ETF_LOAD_DATE = @CURR_DATE

-- Insert statements for procedure here
SELECT TqaSecCode, GlobalSecurity, Cusip
into #tempetftable
from .map.v_get_tqa_security where cusip in (select distinct ETF_CUSIP from .dbo.ETF_LIST_V where ETF_LOAD_DATE = 'Mon Jun 17 14:15:09 BST 2019')


Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd
    @sourceTable = '#tempetftable',
    @startDate = '20181219',
    @endDate = '20181219',
    @frequency = 'D'


Insert into .dbo.ETFComponentWeights

Select 
    C.ETF_CUSIP as W_CAL_CUSIP,
    C.STK_IDX as W_CAL_COMP,
    C.STK_QUANT as W_CAL_SHARES,
    CP.VALUE as W_CAL_PRICE,
    (C.STK_QUANT  * CP.VALUE_) as W_CAL_MVAL,
    (C.STK_QUANT * CP.VALUE_)/SUM(C.STK_QUANT * CP.VALUE) over (partition by C.ETF_CUSIP) as W_CAL_WEIGHT,
    @ETF_DATE as W_CAL_DATE

from .dbo.ETF_COMP_V C
    inner join (Select E.CUSIP, P.Value_ from #tempPriceTable P inner join #tempetftable E on P.TqaSecCode = E.TqaSecCode) CP
    on C.ETF_CUSIP = CP.CUSIP

所以我得到的错误是         无效的对象名称“ #tempPriceTable”。

我不明白为什么这不起作用?有人可以建议我在这里做错什么吗?以及为什么#tempetftable可以正常工作。但是这里的#tempPriceTable在这种情况下不起作用?

1 个答案:

答案 0 :(得分:1)

语法:

SELECT TqaSecCode, GlobalSecurity, Cusip
into #tempetftable

创建一个新的临时表,然后将数据插入该新表中。

语法:

Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd

是常规的“插入”语句,该语句将行添加到现有表中。 要使用此语法,您需要事先创建一个具有正确架构的空临时表。 因此,您需要执行以下操作:

CREATE TABLE #tempPriceTable (your schema)
Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd