<p>I need to provide statistics on how many lines of code <code>(LOC)</code> associated with a system. The application part is easy but I need to also include any code residing within the SQL Server database. This would apply to stored procedures, functions, triggers, etc. </p>

<p>How can I easily get that info? Can it be done (accurately) with <strong>TSQL</strong> by querying the system <code>tables\sprocs</code>, etc?</p>

[{'answer_id': 75190, 'author': 'Mitchel Sellers', 'author_id': 13279, 'author_profile': 'https://Stackoverflow.com/users/13279', 'pm_score': 1, 'selected': False, 'text': '<p>Personally you might just script the objects to file using SQL Server Management tools, it will get a few extras in there for the checks to do the drop first incase the object exists.</p>\n'}, {'answer_id': 75412, 'author': 'Chris Bilson', 'author_id': 12934, 'author_profile': 'https://Stackoverflow.com/users/12934', 'pm_score': 2, 'selected': False, 'text': '<p>Just select all the text from syscomments and count how many lines you have. The text column is text, which you can\'t really see in Management studio, so I would write a program or power shell script like this:</p>\n\n<pre><code>$conn = new-object System.Data.SqlClient.SqlConnection("Server=server;Database=database;Integrated Security=SSPI")\n$cmd = new-object System.Data.SqlClient.SqlCommand("select text from syscomments", $conn)\n$conn.Open()\n$reader = $cmd.ExecuteReader()\n\n$reader.Read() | out-null\n$reader.GetString(0) | clip\n$reader.Close()\n$conn.Close()\n</code></pre>\n\n<p>Paste into an editor that has a line count, and you\'re done.</p>\n'}, {'answer_id': 83666, 'author': 'ninegrid', 'author_id': 13661, 'author_profile': 'https://Stackoverflow.com/users/13661', 'pm_score': 4, 'selected': True, 'text': '<p>In Management Studio, right click the database you want a line count for... select Tasks -> Generate Scripts,   you can select script options in the Scripts Wizard to include or exclude objects,   when you have it set the way you like it can generate to a new query window </p>\n'}]
['https://Stackoverflow.com/questions/75159', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4872/']