24

How can I count all stored procedures which are written by me in my database?

mikhail
  • 5,019
  • 2
  • 34
  • 47
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166
  • possible duplicate of [Query to list SQL Server stored procedures along with lines of code for each procedure](http://stackoverflow.com/questions/291574/query-to-list-sql-server-stored-procedures-along-with-lines-of-code-for-each-proc) – Neil Knight Jan 07 '11 at 18:53

8 Answers8

50
select Count(*) from sys.procedures

And as Philip Kelley noted this is sql 2005 and up

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
14
-- Information about table -- 
SELECT * FROM sys.sysobjects WHERE xtype = 'U'

-- Information about Stored Procedure --
SELECT * FROM sys.sysobjects WHERE xtype = 'P'

-- Information about Functions --
SELECT * FROM sys.sysobjects WHERE xtype = 'FN'

-- Information about Views --
SELECT * FROM sys.sysobjects WHERE xtype = 'V'
Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
Iyyappan
  • 171
  • 1
  • 5
10

To get the Stored Procedure count:

SELECT COUNT(*) SPCOUNT 
  FROM INFORMATION_SCHEMA.ROUTINES
 WHERE ROUTINE_TYPE='PROCEDURE'

or:

SELECT COUNT(*)
  FROM sys.procedures

or:

SELECT COUNT(*) 
  FROM sys.sysobjects
 WHERE xtype = 'P'

Hope one of these help.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
7

As the OP pointed out in a comment, all of the earlier answers are wrong, because they include system procedures. He specifically asked for procedures that were "written by me" -- and later clarified in another comment "other than the system procedure, written by me or anybody working on that data base."

So to exclude system procedures, the only differentiating field I see in sys.procedures is the name. Therefore you need to add a WHERE clause to any of the other answers, like this:

select count(*) from sys.procedures
where name not like 'sp_%'
phantomflash
  • 114
  • 1
  • 4
3
select count(name)
from sys.objects
where type = 'P'
Jason
  • 1,325
  • 6
  • 14
2
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = 'PROCEDURE'
Lamak
  • 69,480
  • 12
  • 108
  • 116
1
select count(*)
from sysobjects
where xtype='P'
Doliveras
  • 1,794
  • 2
  • 14
  • 30
0

This will give you the count of tables and stored procedures.

SELECT 
    CASE TYPE 
        WHEN 'U' 
            THEN 'User Defined Tables' 
        WHEN 'S'
            THEN 'System Tables'
        WHEN 'IT'
            THEN 'Internal Tables'
        WHEN 'P'
            THEN 'Stored Procedures'
        WHEN 'PC'
            THEN 'CLR Stored Procedures'
        WHEN 'X'
            THEN 'Extended Stored Procedures'
    END, 
    COUNT(*)     
FROM SYS.OBJECTS
WHERE TYPE IN ('U', 'P', 'PC', 'S', 'IT', 'X')
GROUP BY TYPE

You can find in sys.objects all types of objects in the database. You will have to run this query on each of your databases to see the count of objects.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Prince
  • 1
  • 1