SQL存储过程中的对象名称无效

时间:2019-10-16 08:32:11

标签: sql-server tsql stored-procedures common-table-expression

我已创建为存储过程,但是当我尝试在ptask中获取数据时,出现以下错误

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

我的代码如下:

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

为什么ptask是无效的对象?

0 个答案:

没有答案