Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    28

    Unanswered: Error in checking table.

    Hello experts,

    Im trying to check for a temporary table and if it exists then delete and re-create it. Ive this piece of code which check if table exist then delete it

    IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='#HoldDate')
    DROP TABLE #HoldDate

    However after this code when I try to create this table I end up having this error

    Msg 2714, Level 16, State 6, Line 4
    There is already an object named '#HoldDate' in the database.

    I believe something is wrong with my syntax not sure what is it. Can please somebody point out my mistake and tell me now to accomplish this task?

    Thanks a lot in advance.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    IF OBJECT_ID('tempDB..#mystupidtemptable') is not null
    DROP TABLE #mystupidtemptable
    If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry. Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Oct 2009
    Posts
    28
    Thanks Thrasymachus for your input. So where should my CREATE TABLE code will go
    Create table #HoldDate(
    DateStamp varchar(10))
    ? After Drop table? Outside BEGIN END or inside?
    Thrasymachus

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    I learn by trying.
    IF OBJECT_ID('tempDB..#mystupidtemptable') is not null
    BEGIN
    DROP TABLE #mystupidtemptable
    END

    CREATE TABLE #mystupidtemptable(surrogatekey int, naturalkey1 varchar(50))
    If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry. Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •