Solved

SQL: Drop temp table if exists is returning error

Posted on 2014-10-08
1
649 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

758 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

20 Experts available now in Live!

Get 1:1 Help Now