简体   繁体   English

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

[英]Invalid object name error for temporary table defined in a stored procedure

I have written a stored procedure which executes a stored procedure and the result it gets has to be stored in a local temporary table.我编写了一个存储过程,它执行一个存储过程,它得到的结果必须存储在本地临时表中。 The Stored procedure gets created without giving any errors.创建存储过程时不会出现任何错误。 But when I try to execute stored procedure, it returns the error that the temporary table is invalid object name.但是当我尝试执行存储过程时,它返回临时表是无效对象名称的错误。

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

So the error I get is Invalid object name '#tempPriceTable'.所以我得到的错误是 Invalid object name '#tempPriceTable'。

I don't understand why is this not working?我不明白为什么这不起作用? Can anyone suggest what am I doing wrong here?谁能建议我在这里做错了什么? and why the #tempetftable works fine.以及为什么#tempetftable 工作正常。 But #tempPriceTable here is not working in this scenario?但是这里的#tempPriceTable 在这种情况下不起作用?

Syntax:句法:

SELECT TqaSecCode, GlobalSecurity, Cusip
into #tempetftable

creates a new temp table and then inserts data into this new table.创建一个新的临时表,然后将数据插入到这个新表中。

Syntax:句法:

Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd

is a regular "insert into" statement, which adds rows to existing table.是一个常规的“插入”语句,它将行添加到现有表中。 To use this syntax you need to create an empty temp table with correct schema beforehand.要使用此语法,您需要事先创建一个具有正确架构的空临时表。 So you need to do something like:因此,您需要执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM