Count of lines of code

  • Is there a way to get a count of the lines of code for a given database, including Stored Procs, Triggers, Views and Functions?

  • This was removed by the editor as SPAM

  • I'm a little dumb-founded by the question but I'm sure you have your reasons for asking. The bottom line is that you need to have a complete scripting of the database in order to do this, that's easy enough.

    Since you're using Sql2005, right click on the database in question in the Sql Management Studio, select Tasks and then select Generate Scripts.

    You'll be presented with several screens that allow you to select what you want scripted and how you want it scripted. Select everything that you find applicable and then script the database.

    A file will be produced that is essentially the Sql for making the database from scratch.  You can get a count from this.

     

    Good Hunting!

  • I can easily understand the desire to have this metric. The number of lines of code-- for better or worse-- has long been used as a metric to gague the relative size of a programming project. As Microsoft continues to make the database environment more feature-rich, it's no wonder that developers will start to put more logic and programming into the database. To get a scope of the size of an application, then, it would make sense to include the SQL side of things as well.

    Another thing to keep in mind with respect to getting at SP contents is that they are also in the syscomments view, so theoretically you could count the number of linefeeds in there. Scripting to a file seems a lot easier, though.

  • Hi!

    Since SQL Server stores everything in tables, you can query the catalog to get this.

    Table syscomments (in the same database you want to count code lines) you'll find at least to columns:

    id, which is the id of the stored procedure, function, etc

    text, the code of the object.

    You can also work with sp_helptext

    Try begining with this:

    create

    table #temp (code varchar(500))

    declare

    @name varchar(100)

    declare

    @sql varchar(150)

    declare

    C cursor

    for Select object_name (id)

    from sysobjects

    where xtype = 'P'

    open

    C

    fetch

    C into @name

    while

    @@fetch_status = 0

    begin

    Select @sql = 'exec sp_helptext ' + @name

    insert into #temp

    exec (@sql)

    fetch C into @name

    end

    close

    C

    deallocate

    C

    Select

    count(*)

    from

    #temp

     

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply