Friday, June 16, 2017

How to check if temporary / permanent objects exists and drop if it exists before creating them

There are many ways to perform drop the existing objects before create them. One of the common way use OBJECT_ID as shown below.


--for a permanent table you can use
IF OBJECT_ID('dbo.tabl1', 'U') IS NOT NULL
 DROP TABLE dbo.tabl1;
--for a temporary table you can use
IF OBJECT_ID('tempdb.dbo.#tbl1', 'U') IS NOT NULL
 DROP TABLE #tbl1;


In SQL Server 2016, new function DROP IF EXISTS introduced to perform this task. Currently, the following objects can DIE.
--from SQL Server 2016 and later
DROP PROCEDURE IF EXISTS
dbo.tabl1, dbo.tab2, dbo.tab3, dbo.tab4, dbo.tab5, #tbl1;
--to drop Stored Procedure
DROP PROCEDURE IF EXISTS
dbo.sp1, dbo.sp2;
Cheers!
Uma

17 comments:

  1. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Data science training in velachery
    Data science training in kalyan nagar
    Data Science training in OMR
    Data Science training in anna nagar
    Data Science training in chennai
    Data Science training in marathahalli
    Data Science training in BTM layout
    Data Science training in rajaji nagar

    ReplyDelete
  2. A universal message I suppose, not giving up is the formula for success I think. Some things take longer than others to accomplish, so people must understand that they should have their eyes on the goal, and that should keep them motivated to see it out til the end.

    java training in annanagar | java training in chennai

    java training in marathahalli | java training in btm layout

    java training in rajaji nagar | java training in jayanagar


    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete

  4. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs Training in online

    angularjs Training in marathahalli

    ReplyDelete
  5. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
    Python training in pune
    AWS Training in chennai
    Python course in chennai

    ReplyDelete
  6. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Best Devops Training institute in Chennai

    ReplyDelete
  7. Thanks for sharing this informative blog. I have read your blog and I gathered some valuable information from this blog. Keep posting.

    Article submission sites
    Education

    ReplyDelete
  8. Nice post I have been searching for a useful post like this on salesforce course details, it is highly helpful for me and I have a great experience with this, Salesforce Training India

    ReplyDelete