簡體   English   中英

在當前批次中聲明變量后,為什么會收到錯誤消息“ Invalid Object Name”?

[英]Why am I getting an error message that says “Invalid Object Name” when I've declared my variable in the current batch?

declare @docs table(
    DocID int
)

insert into @docs (DocID)
select DocID
from tblDoc
where tblDoc.BatchID = #Batch_id#

select *
from tblPage
inner join [@docs]
on tblPage.DocID = [@docs].DocID

我試過將@docs放在方括號中,試過刪除where子句,試過insert命令的不同排列,但都沒有成功。

我不斷得到

必須聲明標量變量@docs”消息或“無效的對象名稱消息”。

我在這里錯過明顯的東西嗎?

使用Microsoft SQL Server 2008(RTM)-10.0.1600.22

別名表變量

select *
from tblPage
inner join @docs d
on tblPage.DocID = d.DocID

只是不要問我為什么:)

您不應該在JOIN中使用方括號。 以下似乎很好用:

create table tblDoc
(
    DocID int,
    BatchID INT
)

create table tblPage
(
    DocID int
)

declare @docs table(
    DocID int
)

insert into @docs (DocID)
select DocID
from tblDoc
where tblDoc.BatchID = 1

select *
from tblPage
inner join @docs
on tblPage.DocID = [@docs].DocID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM