Determine whether MS SQLSERVER temporary table exists

drop table #tempcitys
 select * into #tempcitys from hy_citys The

above statement is definitely wrong the first time it is run, but not the second time.
Because select * into #tempcitys from hy_citys automatically creates a temporary table #tempcitys, the first time the temporary table does not exist, the drop table will naturally go wrong.
I didn't react at first, select * into will automatically create a temporary table.

So it is more reliable to first judge whether the temporary table exists, and then drop table
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempcitys') and type='U')
   drop table #tempcitys

Note that there are two behind tempdb. Not one

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324890571&siteId=291194637