简体   繁体   English

如何解决 SQL 服务器中的“对象已存在”错误

[英]How to work around “Object Already Exists” Error in SQL Server

I'm trying to run a query with a different range of data but for some reason when I run it, it gives back an error stating:我正在尝试使用不同范围的数据运行查询,但由于某种原因,当我运行它时,它会返回一个错误说明:

Msg 2714, Level 16, State 6, Line 54消息 2714,第 16 级,State 6,第 54 行
There is already an object named '##contacts' in the database.数据库中已经有一个名为“##contacts”的 object。

How would I get around this?我将如何解决这个问题? Is it a column or columns in the database that are created and that I need to drop?它是数据库中创建的并且我需要删除的一列或多列吗?

Code I'm using:我正在使用的代码:

use KBData
go

declare 
    @startdate datetime='2010-01-01',
    @enddate datetime = '2020-05-26';

/*from contacts*/
select
    lower(c.contactid) as contactid ,
    replace(replace(lower(c.emailaddress1),' ',''),',','') as emailaddress1,
    replace(replace(lower(c.emailaddress2),' ',''),',','') as emailaddress2,
    replace(replace(lower(c.emailaddress3),' ',''),',','') as emailaddress3
into ##contacts
from crm.Contact c
where (c.createdonutc >= @startdate and c.createdonutc < dateadd(dd,1,@enddate))
and (c.emailaddress1 is not null or c.emailaddress2 is not null or c.emailaddress3 is not null)

/*from buyers*/
select 
    lower(b.Email) as email
into #sales
from crm.SalesAgreement s  
left join dbo.BuyerContracts bc
    join dbo.buyers b
    on b.ProspectNo = bc.ProspectNo
    and b.Deleted is null  
    on s.kb_salesagreementnumber = bc.SalesAgreementNo
    and bc.Deleted is null
where (s.kb_saledate >= @startdate and s.kb_saledate < dateadd(dd,1,@enddate))
and s.Deleted is null   ;

select
    distinct replace(replace(lower(b.email),' ',''),',','') as email
into #buyers
from #sales b
where b.Email is not null ;

The issue you're running into stems from your use of a global temporary table ( ##tableName ) rather than a local temporary table ( #tableName ).您遇到的问题源于您使用全局临时表( ##tableName )而不是本地临时表( #tableName )。 The two behave differently.两者的行为不同。

A local temp table is created on the fly, is only accessible from the session where it is created, and is dropped when that session closes it's connection, or unless it is explicitly dropped before that.本地临时表是动态创建的,只能从创建它的 session 访问,并且在 session 关闭它的连接时被删除,或者除非在此之前明确删除它。 It's completely contained within that scope.它完全包含在 scope 中。

A global temp table is also created on the fly, but is then accessible via any connection to the server.全局临时表也是动态创建的,但随后可以通过与服务器的任何连接进行访问。 For instance (and I was just doing this earlier today), you can create a global temp table in one SSMS window, and then work with that table in multiple other SSMS windows.例如(我今天早些时候刚刚这样做),您可以在一个 SSMS window 中创建一个全局临时表,然后在多个其他 SSMS windows 中使用该表。 Or someone else from your team can work with it.或者您团队中的其他人可以使用它。 The table continues to exist until the last session accessing the table is disconnected, or, also, unless it's explicitly dropped before then.该表将继续存在,直到最后一个访问该表的 session 断开连接,或者,除非在此之前显式删除它。

So if you had two sessions open with this code running, the ##contacts from one session would already exist when the second session tries to create it.因此,如果您在运行此代码的情况下打开了两个会话,则当第二个 session 尝试创建它时,来自一个 session 的##contacts将已经存在。 There are a few situations when you need a global temp table, and you need to do existence checks and such in those sitations, but they're few and far between.在某些情况下,您需要一个全局临时表,并且您需要在这些情况下进行存在性检查等,但它们很少而且相差甚远。 It's generally easier to work with local temp tables.使用本地临时表通常更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server创建表给出错误,表明对象“”在不存在时已经存在 - SQL Server Create Table gives error that object '' already exists when it doesn't EXISTS子句如何在SQL Server中工作? - How does the EXISTS Clause work in SQL Server? 如何检查SQL Server中已经存在的数据库? 如果数据库已经存在,如何停止查询? - how to check database already exists in sql server? and how stop the query if database already exists? 一个名为...的 cursor 已经存在 - SQL 服务器 2008 - A cursor with the name … already exists - SQL Server 2008 SQL Server:如何解决WHERE IN限制的128个值 - SQL Server : how to work around WHERE IN limit of 128 values SQL Server 中的现有表导致“数据库中已经存在名为‘***’的对象”错误 - Existing table in SQL Server causing 'There is already an object named '***' in the database' error SQL Server 2014中的临时表导致“已经存在一个名为对象”错误 - Temporary table in SQL Server 2014 causing ' There is already an object named' error SQL Server 中的临时表导致“已经有一个名为的对象”错误 - Temporary table in SQL server causing ' There is already an object named' error 在SQL表中创建表时存在错误 - Error Creating table in SQL table already exists 如何解决“名称已存在的 cursor”错误 - How to resolve “A cursor with the name already exists” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM