无法绑定 Temp table 多部分标识符。

The Temp table multi-part identifier could not be bound.

我在 C# 中创建了一个这样的查询:

create table #IdentityPerson (Id int)
insert into [IdentityPerson] (...)
output inserted.Id into #IdentityPerson
values (...)

create table #Identity (Id int)
insert into [Identity] (PersonId,...)
output inserted.Id into #Identity
values (#IdentityPerson.id,...)

create table #IdentityState (Id int)
create table #IdentityCity (Id int)
insert into [IdentityState] (...)
output inserted.Id into #IdentityState
values (...)
insert into [IdentityCity] (StateId,...)
output inserted.Id into #IdentityCity
values (#IdentityState.id,...)

insert into [IdentityAddress] (CityId,IdentityId,...)
values (#IdentityCity.id,#Identity.id,...)

drop table #IdentityCity
drop table #IdentityState
drop table #Identity
drop table #IdentityPerson

当我使用 Dapper.net (ExecuteAsync) 运行 这个查询时,我得到一些 SqlException 错误:

The multi-part identifier "#IdentityPerson.id" could not be bound. The multi-part identifier "#IdentityState.id" could not be bound. The multi-part identifier "#IdentityCity.id" could not be bound. The multi-part identifier "#Identity.id" could not be bound.

有人帮忙吗?

我想你在找 insert . . . select:

create table #Identity (Id int);

insert into [Identity] (PersonId,...)
    output inserted.Id into #Identity
    select ip.id, . . .
    from #IdentityPerson ip;