Transact-SQL: Count number of lines in all stored procedures in current database
08-08-2008 by Marek Śliwiński
Posted in Microsoft SQL SERVER, Transact-SQL | Tagged Transact-SQL, Microsoft SQL SERVER | 7 Comments
7 Responses
Leave a Reply
-
Authors
-
Recent Posts
- Blog move to new address http://code.mareoblo.pl
- How to find some text in any object (table, stored procedure, view,UDF, and more) of SQL Server 2005/2008?
- Thank God it’s Friday :)
- C#: SQL Server data types equivalents in .NET Framework
- Transact-SQL: Show real size of tables in database on your harddisk
Categories
- C# (14)
- C# Snippets (9)
- WinForms (6)
- Free 3rd Party Controls (1)
- Freeware – Best of the Best (2)
- Microsoft SQL SERVER (14)
- Transact-SQL (11)
- Regular Expressions (5)
- Thank God it's Friday :) (9)
- Uncategorized (2)
- Visual Studio (2)
- WSUS (1)
- C# (14)
Tags
-
Top Posts
- C#: WinForms - How to use DataTable as DataGridView datasource and DataTable column with HeaderText property
- Regular Expressions: Remove all special characters from a string, allowing only alphanumeric and chars: '.' and '-'
- Regular Expressions: Time hh:mm validation 24 hours format
- C#: SQL Server data types equivalents in .NET Framework
- C#: WinForms - How to check if form is already open and bring it to the front instead open new instance
01 Programmer Must Read
Favourite links
Personal links
-
Blog Stats
- 41,037 hits
KA-ME-HA-MEE-HAA

Master Kame Reincarnation



Very interesting SP.
Good Work!
Pinal Dave ( http://www.SQLAuthority.com )
Thank You! :)
Not sure what is the purpose / use of this SP (I have nothing against your code)
I personally think it would be much more valuable if you could guage the COMPLEXITY of the Stored Procedure based on the CODE which DEV Team can focus on rather them just making any decision based on LINE COUNT
Also, I think many a SP’s have ERROR HANDLERS which can make the LINE COUNT GROW which does not bring any value either
Again I say, I dont see any value add in this (please please dont take it personal)
Hi PP :)
Thx for the comment!
“Not sure what is the purpose / use of this SP
[...]
Again I say, I dont see any value add in this ”
There is only one purpose. The programmer curiosity :) Nothing more.
Have you never wondered about how many lines of SQL you wrote after 4-5 month development in some project? :)
I would like to know the answer because I’m curious man (maybe too much ;))
So there is nothing in code above what you can use to do some technical analysis or improvements in your SQL development (except maybe the thought “100.000 lines in last year? I have to go on holidays IMMEDIATELY!!! ;)) and that wasn’t my goal :)
This particular SP is nothing but for fun. Sorry for confusion if it was not clear enough. It is the same case as for example add-ins for Visual Studio to count number of lines in entire project or solution.
After your post I have started to think what “practical” use can has this SP:
1) maybe analysis which programmer did the most of work in SQL but I don’t think it is a good measure ;)
2) to show the boss that you are not lazy ;) ;)
3) to impress your girlfriend or other friends ;)
4) ? any idea yet? :)
Greetings!
Marek
I came to this site googling to find out how to count number of occurrences within a stored procedure without having a burden to write a SP to do this. I am still searching. But, you can use this query to do line counts without writing a SP. This is not fancy but gives you an idea of line count.
select o.name as sp_name,
sum((len(replace(c.text, char(10), ‘ ‘)) – len(c.text))) as lines_of_code
from sysobjects o
inner join syscomments c
on c.id = o.id
where o.type in (‘P’, ‘F’)
and o.name not like ’sp_%’
and o.name not like ’sys%’
and o.name not like ‘xp_%’
group by o.name
go
The trick here is to replace CHAR(10) with 2 blanks and count the diff between LEN of the SP code.
Several ways to do same thing. But this is simple and may not be sexy.
I think that I answered my own question by replying to you. If I think about it more. Same query will give count of a keyword in a Stored procedure.
If am trying to count occurrences of CREATE TABLE in a SP, the same query will give me results.
select o.name as sp_name,
sum((len(replace(lower(c.text), ‘create table’, ‘Xcreate table’ ‘)) – len(lower(c.text)))) as lines_of_code
from sysobjects o
inner join syscomments c
on c.id = o.id
where o.type in (’P’, ‘F’)
and o.name not like ’sp_%’
and o.name not like ’sys%’
and o.name not like ‘xp_%’
group by o.name
go
The trick is to replace create table with xcreate_table and count the diff in the count of text and that should give the total number of occurrences.
I try to use SQL as much as possible if I can avoid the SP code. Not that I am averse but we should use engine’s capability and that is where DB2 is way stronger than any other database. My biases – as I am a DB2 person. I am working to move large number of SQL Server / Sybase databases to DB2 and was interested to know complexities involved in SP code.
Hi Vikram :)
Thank you for your interesting comments :)
I didn’t know about syscomments table. It looks like a good and shorter replacment to use instead sp_helptext operations.
But when I did some test procedure like below (with 400 lines of getdate() ) and run your code from 1st post then it returns me only about a half number of lines (201 instead about 400 lines)
So there is something tricky in your SQL statement…
It looks like a problem with stored procedure size. I read that column text in syscomments can take only 4000 chars so maybe here is a point. I don’t have time to investigate this at this moment and will back to this later (maybe you will be faster to solve this problem in your SQL).
Thanks one again for additional idea :) Syscomments table looks better to handle than sp_helptext.
Greetings!
Marek
– TEST SP CODE HERE —
CREATE PROCEDURE [dbo].[TestCountLines]
AS
BEGIN
– SET NOCOUNT ON added to prevent extra result sets from
– interfering with SELECT statements.
SET NOCOUNT ON;
SET DATEFORMAT ymd
SET DATEFIRST 1
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
SELECT getdate()
END