[英]Can't drop temp table SQL
Hi Im creating a Temp table and inserting data to the Table. 您好我创建一个临时表并将数据插入表。 Im going to use the Temp table to Join it to the specific User.
我将使用Temp表将其加入特定用户。
CREATE TABLE #MyTempTable
(
UsersId int,
ValautionCount int
)
SELECT
U.UserId,
COUNT(*) AS ValautionCount
INTO #MyTempTable
FROM
Users U
Right JOIN Valuation V ON V.ValuationUser = U.UserId
GROUP BY
U.UserId
DROP TABLE #MyTempTable
When I run this query I get this error : There is already an object named '#Temp' in the database. 当我运行此查询时,我收到此错误:数据库中已存在名为“#Temp”的对象。
But when I run this query DROP TABLE #MyTempTable
I get this error: Cannot drop the table '#Temp', because it does not exist or you do not have permission. 但是,当我运行此查询
DROP TABLE #MyTempTable
我收到此错误:无法删除表'#Temp',因为它不存在或您没有权限。 Im using SQL 2012 我正在使用SQL 2012
SELECT ... INTO ... statement itself create the #Temp table. SELECT ... INTO ...语句本身创建#Temp表。 Does not need CREATE TABLE statement here.
这里不需要CREATE TABLE语句。 Remove "CREATE TABLE" statement and try.
删除“CREATE TABLE”语句并尝试。
You already have an entity by name "Temp" in your database. 您的数据库中已有一个名为“Temp”的实体。 And you are not able to drop that entity because of access permissions.
而且由于访问权限,您无法删除该实体。
No need to drop the temp table since it visible at only till the session. 无需删除临时表,因为它仅在会话期间可见。
Create PROCEDURE proctemptable
BEGIN
IF object_id('tempdb..#Temp') is not null // Try this hope this will work
BEGIN
DROP TABLE #Temp
END
CREATE TABLE #Temp
(
UsersId int,
ValautionCount int
)
SELECT
U.UserId,
COUNT(*) AS ValautionCount
INTO #Temp
FROM
Users U
Right JOIN Valuation V ON V.ValuationUser = U.UserId
GROUP BY
U.UserId
//DROP TABLE #Temp
END
No need to drop the #Temp table, it will drop it automatically when the stored procedure execution completed 无需删除#Temp表,它将在存储过程执行完成时自动删除它
OR 要么
Please refer this link for more temp tables in sql server 请参阅此链接以获取sql server中的更多临时表
http://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/ http://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/
You may be creating the same table twice in your code. 您可能在代码中两次创建相同的表。
I experienced the same problem, I had copied a section of code I wanted to reuse (modified) into the same procedure, including a CREATE TABLE
statement - I was effectively creating the table twice - and even though the CREATE TABLE
statements were between separate BEGIN
and END
markers, and there was a DROP TABLE
statement dropping the 1st 'instance' before the 2nd CREATE
statement, I encountered this exact error. 我遇到了同样的问题,我已经将我希望重用(修改)的一段代码复制到同一个过程中,包括一个
CREATE TABLE
语句 - 我有效地创建了两次表 - 尽管CREATE TABLE
语句是在单独的BEGIN
之间和END
标记,并有一个DROP TABLE
语句在第二个CREATE
语句之前删除第一个'实例',我遇到了这个确切的错误。
Official answer from Microsoft page on how to drop temp table helped me. 微软关于如何删除临时表的官方回答对我有帮助。 In short I used this and it worked fine.
总之,我使用它,它工作得很好。
use tempdb
go
IF OBJECT_ID(N'tempdb..#yourtemptable', N'U') IS NOT NULL
DROP TABLE #yourtemptable;
GO
Using MS SQL 2017 使用MS SQL 2017
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.