简体   繁体   English

SQL 存储过程中的 object 名称无效

[英]Invalid object name in SQL stored procedure

I have created as stored procedure, but when I try to get the data in ptask I get the following error我已经创建为存储过程,但是当我尝试在ptask中获取数据时,出现以下错误

Msg 208, Level 16, State 1, Procedure mySproc, Line 72 [Batch Start Line 0]
Invalid object name 'ptask'.

My code looks like this:我的代码如下所示:

CREATE procedure PSBASPLG.ipMirrorFromOffer
    @usr    nvarchar(255),
    @age    int,
    @id     int
as
set nocount on
declare @now datetime = getdate()
exec ipLgAppend @usr, 'PSBASPLG.ipMirrorFromOffer', @pint0 = @id

;with stask (tsID, tsTag, tsNode, tsTxt, cuID, pjRef, pjRsp)
as (select top 1
        ts.tsID, ts.tsTag, ts.tsNode, ts.tsTxt,
        pj.pjCust, pj.pjRef, pj.pjRsp
    from tbTasks as ts
        inner join tbProjs as pj
        on  (pj.pjID = ts.tsProj)
    where
        (ts.tsID = @id)
)
, sleaf (lfTag, lfTxt)
as (select  distinct 
        lf.lfTag, lf.lfTxt
    from tbLeafs as lf
        inner join stask as ts
        on  (ts.tsNode = lf.lfNode)
)
, sline (paTxt, paCnt, paSta, tyID, rsID, itID, acTyp, grID)
as (select  distinct 
        pa.paTxt, pa.paCnt, pa.paSta, pa.paTyp, pa.paRes,
        it.itID, ac.acTyp, pa.paGrp
    from tbParts as pa
        inner join tbActs as ac
        on  (pa.paAct = ac.acID)
        inner join stask as ts
        on  (ts.tsID = ac.acTask)
        inner join tbItems as it
        on  (it.itID = pa.paItem)
    where
        (pa.paStk >= 0) and
        (pa.paSta > -1) 
)
, ptask (tsID, tsProj, tsNode)
as (select  distinct
        ts.tsID, ts.tsProj, ts.tsNode
    from tbTasks as ts
        inner join tbLeafs as lf
        on  (lf.lfNode = ts.tsNode) and
            (lf.lfTag = PSBASPLG.fnConf('Leaf_offertag'))
        inner join stask as sl
        on  (sl.tsTag = lf.lfTag)
    where
        (ts.tsSta > -1)
)
, pline (paID, itID, acID)
as (select  distinct
        pa.paID, pa.paItem, ac.acID
    from tbParts as pa
        inner join tbActs as ac
        on  (pa.paAct = ac.acID)
        inner join ptask as ts
        on  (ts.tsID = ac.acTask)
    where
        (pa.paStk >= 0) and
        (pa.paSta > -1) 
)

select * from ptask -- Error happens here

Why is ptask an invalid object?为什么ptask是无效的 object?

Your code looks valid.您的代码看起来有效。 If I paste your query into my editor (which has is a standard vanilla install) I receive no errors, aside from the missing columns and entities.如果我将您的查询粘贴到我的编辑器中(这是标准的香草安装),除了缺少的列和实体之外,我不会收到任何错误。 Otherwise it is a valid query.否则它是一个有效的查询。

Sometimes intellisense for VS and SSMS become corrupted, especially if they've been open a long time.有时 VS 和 SSMS 的智能感知会损坏,特别是如果它们已经打开了很长时间。 I would recommend copy + pasting your query into notepad and restarting SSMS.我建议将您的查询复制 + 粘贴到记事本中并重新启动 SSMS。

Barring this, there must be an error in one of the CTE's .除此之外,其中一个 CTE 中一定有错误 Running each individually, where possible, should reveal the problem.在可能的情况下单独运行每个应该可以揭示问题。

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

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