Solved

SQL: Drop temp table if exists is returning error

Posted on 2014-10-08
1
652 Views
Last Modified: 2014-10-08
Error: There is already an object named '#tmpAnticipatedReleaseDate' in the database.

Before creating a temp table, I am checking if one already exists, but am receiving an error message, and don't know why.

IF OBJECT_ID('tempdb..#tmpAnticipatedRelease') IS NOT NULL                         
DROP TABLE #tmpAnticipatedReleaseDate 
GO  

-- Reference table containing anticipated release dates for all disbursements               
CREATE TABLE #tmpAnticipatedReleaseDate                
(                
DisbursementId INT NOT NULL,                
AnticipatedReleaseDate DATETIME               
)                
CREATE CLUSTERED INDEX cx ON #tmpAnticipatedReleaseDate(DisbursementId, AnticipatedReleaseDate)     


;WITH 
cteInstallmentHoldDueDate AS (
SELECT 
	InSc.InstallmentScheduleId, Insc.DueDate, 
	Di.DisbursementId, Di.Amount, Di.ReleaseDate, DiH.DisbursementHoldId, DiH.DisbursementHoldTypeId, DiH.HoldValue
FROM 
	Disbursement Di
	JOIN DisbursementHold DiH ON DiH.DisbursementId = Di.DisbursementId AND DiH.DisbursementHoldTypeId = 1
	JOIN InstallmentSchedule InSc ON InSc.CustomerAccountId = Di.PartyId AND InSc.InstallmentNumber = DiH.HoldValue
)

INSERT INTO #tmpAnticipatedReleaseDate (DisbursementId, AnticipatedReleaseDate)

SELECT Di.DisbursementId, Di.ReleaseDate
FROM 
	Disbursement Di

Open in new window

0
Comment
Question by:pzozulka
1 Comment
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40369361
Object name is slightly off in the IF:

IF OBJECT_ID('tempdb..#tmpAnticipatedReleaseDate') IS NOT NULL                        
    DROP TABLE #tmpAnticipatedReleaseDate
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now