Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Sunday, 21 May 2017

Check IF Exist For Trigger , Function And Stored Procedure

 check if exists for function, check if exists for stored procedure, check if exists for trigger, check if proc exist in sql, SQL     2 comments   


Introduction
In this article we will learn how to check if exist condition for Stored Procedure , Function and Trigger in sql server . Why do we need to check if exists. How to use If Exists in SQL.

Previous Updates
In previous articles we have learnt Const Readonly and Static difference in C#, SCOPE_IDENTITY(), @@IDENTITY and IDENT_CURRENT difference .What is Cursor in sql and use of cursor.  Best basic C# interview questions.


Whenever you are working with SQL server you always need to prepare Scripts of Stored Procedures , triggers , Functions , Indexes etc. If you are working only for yourself then it is OK, but if you work as an company or Company employee then you have to prepare Scripts for Client Machine or other's machine using IF Exists statement. Without this your generated script will fail.

Check If Exist For Stored Procedure

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = 
                    OBJECT_ID(N'[Schema].[Procedure_Name]') AND type IN (N'P', N'PC'))
BEGIN
DROP PROCEDURE [Schema].[Procedure_Name]
Print('Proceudre dropped => [Schema].[Procedure_Name]')
END
GO

You can also add Your Create Query in Else Block also. Just like 
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = 
OBJECT_ID(N'[Schema].[Procedure_Name]') AND type IN (N'P', N'PC'))
BEGIN
DROP PROCEDURE [Schema].[Procedure_Name]
Print('Proceudre dropped => [Schema].[Procedure_Name]')
END
ELSE
BEGIN
--  Your Create Procedure Query Here
Print('Proceudre Created => [Schema].[Procedure_Name]')
END
GO

Check If Exist For Trigger
--------------------------Drop Trigger----------------------------
IF EXISTS (SELECT * FROM sys.objects WHERE [name] =
            N'[Trigger_Name]' AND [type] = 'TR')
BEGIN
DROP TRIGGER [Trigger_Name]
Print('Trigger dropped => [Schema].[Trigger_Name]')
END
GO

Check If Exist For Function
--------------------------Drop Function---------------------------
IF  EXISTS (SELECT TOP 1 1 FROM sys.objects WHERE 
            object_id = OBJECT_ID(N'[Schema].[function_Name]')
             AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
 DROP FUNCTION [Schema].[function_Name]
 Print('function dropped => [Schema].[function_Name]')
END
GO

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

2 comments:

  1. 1GuyGadget26 November 2019 at 02:17

    asds

    ReplyDelete
    Replies
      Reply
  2. Kendra D26 June 2024 at 11:51

    Very thoughtfful blog

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write