why does updating a real table from a self-join temp table is not working but when using a real table it works?

点点圈 提交于 2019-12-02 18:08:18

问题


Here's the scenario:

Procedure 1 creates temp table #t.

Procedure 1 executes procedure 2.

Procedure 2 populates #t.

In procedure 1, I insert into a real table from #t just so I can view the data. The data is there.

Immediately after viewing this data, I do an update with a self-join. Like so:

  update b
  set b.column1 = a.column3
  from #t a
    inner join #t b on a.id = b.id;

The record that is supposed to be updated IS NOT UPDATING.

However, if I change #t to a real table "dbo.t" and do exactly the same thing, it works.

I'm so confused. Anyone has any idea why this could be? Thanks.


回答1:


Per the MS SQL Docs:

A local temporary table created in a stored procedure is dropped automatically when the stored procedure is finished. The table can be referenced by any nested stored procedures executed by the stored procedure that created the table. The table cannot be referenced by the process that called the stored procedure that created the table.

https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-2017



来源:https://stackoverflow.com/questions/51507785/why-does-updating-a-real-table-from-a-self-join-temp-table-is-not-working-but-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!