简体   繁体   中英

Check if stored proc exists in DB?

i am trying to grant execute privs in stored proc in more than one database. The problem is this stored proc might not be in some of the databases. So how can i write a script which checks if stored proc exists in database and if does then provide execute privs for user?

many ways to do it:

1)

IF EXISTS (SELECT name 
       FROM   sysobjects 
       WHERE  name = N'proc1' 
       AND    type = 'P')

2)

IF EXISTS (SELECT * 
           FROM   information_schema.routines
           WHERE  routine_name = 'Proc1')

Try this:

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[your_procedure_name]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
  -- Set privileges here
END

Try this:

if exists (select 1
      from sysobjects
      where  id = object_id('YourProc')
      and type = 'P')

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

Check IF Exist for Trigger , Function also by clicking below link http://www.gurujipoint.com/2017/05/check-if-exist-for-trigger-function-and.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM