Cannot drop the SQL table error message

Hi Experts,

When trying to run the below SQL query in the SQL management studio 2008 I get the following message:

Msg 3701, Level 11, State 5, Line 1
Cannot drop the table '#TT1', because it does not exist or you do not have permission.
Any ideas how to stop this message appearing ?


DROP TABLE #TT1

SELECT
 'A'                         AS 'LineSeq'      ,
 T0.ItemCode                              ,
 T0.ItemName                              ,
 T0.CardCode                        AS 'Cust'      ,
 999999999                   AS 'DocNum'      ,
 GETDATE()                  AS 'DueDate'      ,
 T0.OnHand                      AS 'OpenQty'       
INTO #TT1
FROM OITM T0


INSERT INTO #TT1

SELECT
 'B'                         AS 'LineSeq'      ,
 T0.ItemCode                              ,
 T2.CardName                                                               ,
 T2.CardCode                       AS 'Cust'      ,
 T2.DocNum                   AS 'DocNum'      ,
 T2.DocDueDate                   AS 'DueDate'      ,
- T1.OpenQty                   AS 'OpenQty'      

FROM #TT1 T0

INNER JOIN RDR1 T1 ON T1.ItemCode = T0.ItemCode and T1.OpenQty > 0
INNER JOIN ORDR T2 ON T2.DocEntry = T1.DocEntry

INSERT INTO #TT1
SELECT
 'C'                         AS 'LineSeq'      ,
 T0.ItemCode                              ,
 T3.U_MAX_SHIP                                                               ,
 T4.CardCode                     AS 'Cust'      ,
 T4.DocNum                   AS 'DocNum'      ,
 T3.ShipDate                   AS 'DueDate'      ,
 T3.OpenQty                   AS 'OpenQty'      

FROM #TT1 T0

INNER JOIN POR1 T3 ON T3.ItemCode = T0.ItemCode and T3.OpenQty > 0
INNER JOIN OPOR T4 ON T4.DocEntry = T3.DocEntry
where T0.LineSeq = 'A' and T3.Shipdate < DATEADD(d,84,GETDATE())
INSERT INTO #TT1



SELECT
 'E'                         AS 'LineSeq'      ,
 T0.ItemCode                              ,
 NULL                                    ,
 NULL                         AS 'Cust'      ,
 0                         AS 'DocNum'      ,
 DATEADD(D,365,GETDATE())                   AS 'DueDate'      ,
 0                         AS 'OpenQty'       

FROM #TT1 T0

WHERE T0.LineSeq = 'A'

SELECT  
 CASE T0.LineSeq
   WHEN 'A' THEN 'Stock'
   WHEN 'B' THEN 'Sales'
   WHEN 'C' THEN 'Purchase'
 
   ELSE NULL             END       AS 'Info'      ,
 CASE T0.LineSeq
 WHEN 'E' THEN NULL
   ELSE T0.ItemCode       END      AS 'Item No'      ,
 T0.ItemName                  AS 'Item Cust'      ,
 T0.Cust                  AS 'Cust'      ,
 CASE T0.LineSeq  
   WHEN 'A' THEN NULL
   WHEN 'E' THEN NULL
   ELSE T0.DocNum       END      AS 'SO/PO Number'      ,
 CASE T0.LineSeq
   WHEN 'E' THEN NULL
   ELSE T0.DueDate       END      AS 'SO/PO Due'      ,
 CASE T0.LineSeq
   WHEN 'E' THEN NULL
   ELSE T0.OpenQty       END       AS 'SO/PO Qty'      
   
FROM #TT1 T0
ORDER BY T0.ItemCode, T0.DueDate, T0.LineSeq
kevin1983Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
IF OBJECT_ID('tempdb..#TT1') IS NOT NULL
   DROP TABLE #TT1
GO

-- Then do your stuff
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kevin1983Author Commented:
ok so if i remove the very first line drop table statement. Then use code as you suggested and then start the rest from the Select line?
0
kevin1983Author Commented:
what does you suggested code do, does it check to see if the temp table exists first?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
First comment:  correct
Second comment:  Yes, and deletes it only if it exists.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
So ... how's it going?
0
kevin1983Author Commented:
jimhorn - sorry I thought I had accepted your solution yesterday. that has done the trick just fine thanks, run the SQL multiple time without any errors.

Kevin
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your stuff. -Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.