views:

52

answers:

1

When you log LINQ-to-SQL's query output via the "Log" property on the DataContext object, you get output similar to:

SELECT [t0].[fullaname], [t0].[Worker], [t0].[Office]
FROM [dbo].[Workers] AS [t0]
WHERE [t0].[OfficeID] = @p0
ORDER BY [t0].[Name]
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [412]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1

In this example, I'm just pulling back some information about every "Worker" in the Office with ID = 412. However, this output does not execute directly in a SQL Management Studio Query window, because of the "@p0" commented format that LINQ outputs.

Does anyone know if there is a stored procedure that takes this format so I can execute it? I looked at the paramaterized query procs, but maybe I'm just not seeing it. If there's no procedure, I'm about to write a parser that will turn this format into "normal" SQL...

Thanks!

Note:

I know I could just Define @p0 at the top of this, as shown in http://stackoverflow.com/questions/1705939/help-with-sql-linq-debugging, but - a lot of these queries I have take like 20 parameters, so it becomes a lot of work copying and pasting....

+1  A: 

There is no way to directly execute what you have above without manually (or programattically via some kind of parse) inserting the value of the parameters.

If you use sql profiler you will get an executable dynamic sql statement, this is a much better approach, but i realise that this isn't always possible.

Paul Creasey
Yeah - I guess I could always turn on profiler...though it's nice to be able to grab it right from your dev environment, using DebuggerWriter, for instance: http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11
Sam Schutte
I ended up writing a Regex parser that does search and replace on the necessary values. It's nice to be able to easily execute it now!
Sam Schutte