简体   繁体   中英

Stored Procedure: There is already an object named '#columntable' in the database

Please see the code below:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE Test
AS
BEGIN
    begin
    select * into #dbreviews from dbreviews
    end

    drop table #dbreviews

    begin
    select * into #dbreviews from dbreviews
    end

END
GO

The error I get is:

There is already an object named '#dbreviews' in the database.

Questions like this: There is already an object named '#columntable' in the database are telling me this should be possible.

As per SQL Server specification, it is not allowed. Please refer to the documentation .

If more than one temporary table is created inside a single stored procedure or batch, they must have different names.

You are creating two temporary tables, with the same name #dbreviews . This is not allowed.

At the end of your PROC, #dbreviews exists. When you run it again, it already exists. If you want to recreate every time you run the proc, use:

IF OBJECT_ID('tempdb..#dbreviews') IS NOT NULL
THEN
  drop table #dbreviews
END IF

select * into #dbreviews from dbreviews

Please make sure that you use the "alter" syntax:

ALTER PROCEDURE

if you paste code in, you may need to replace "create" with "alter"... this just happened to me and is frustrating, so I hope this helps someone else.

Note: This was my answer to a slightly different question, so I recycled it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM