views:

586

answers:

3

Does anyone know a way to access the text of stored procedures using LINQ to SQL? I'd like to audit a set of stored procedures to check that they contain all contain some common code.

It feels like there should be a nice way of opening them through a LINQ to SQL data context but I don't know how. Any suggestions?

+3  A: 

You could try something along these lines:

string results = db.ExecuteQuery<string>
(@"EXEC sp_helptext '{0}'", procedure_name);

or use this query in you LINQ:

SELECT text
FROM syscomments
WHERE id = (SELECT id FROM sysobjects WHERE name = 'procedure_name')
ORDER BY colid
edosoft
As an aside, note that executequery can be parameterised: (@"EXEC sp_helptext {0}", procedureName);
Marc Gravell
thanks, didn't know that. Updated the code
edosoft
Perfect - many thanks indeed!
Jon Artus
Incidentally (in case anyone else needs to do this), the ExecuteQuery method seems to return an IEnumberable<string> so I've ended up converting it to a List<string> in this case.
Jon Artus
+2  A: 

I'm not sure that is a task ideally suited to LINQ-to-SQL - but you can presumably get the text from things like syscomments - however, this could be obfuscated. I would run the tests again my code repository instead of the database, personally.

Marc Gravell
+2  A: 

I guess you are using SQL Server.

To retrieve the text of an stored procedure you will need to execute the sp_helptext system stored procedure.

To know how to execute an stored procedure from Linq you can see this.

eKek0