Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR Incorporated.

Katrin and Jim

Month List

Running NHibernate queries in management studio

by Jim Mar 17, 2011 5:33 PM
If you watch your NHibernate-generated queries go by in SQL Profiler, you’ll see a whole lot of SQL that looks like this:
declare @p1 int
set @p1=11
exec sp_prepexec @p1 output,N'@p0 varchar(8000),@p1 ...
SELECT TOP (@p1) this_.ProjectId as ProjectId78_0_, ...
',@p0='jimrogers',@p1=10,@p2='jimrogers',@p4=10
select @p1
But if you try to paste that into SQL Management Studio and run it, you get this error:
Msg 8179, Level 16, State 2, Procedure sp_prepexec, Line 1
Could not find prepared statement with handle 11.
Getting the query to run is simple enough – just comment out or delete the second line:
declare @p1 int
--set @p1=11
exec sp_prepexec @p1 output,N'@p0 varchar(8000),@p1 ...
SELECT TOP (@p1) this_.ProjectId as ProjectId78_0_, ...
',@p0='jimrogers',@p1=10,@p2='jimrogers',@p4=10
select @p1

Comments (3) -

6/27/2011 10:45:48 PM #

happytony

May i know why need to delete the second line. which exist in the profiler.
can share the idea? and email to me. Thanks

happytony United States

3/26/2013 3:45:12 PM #

John MacIntyre

First off thanks, and if you know the answer to @happytony's question, I'd love to know as well.  Thanks again.

John MacIntyre Canada

5/29/2013 2:13:05 PM #

Dean-O

In your example...  not sure I understand what the select @p1 does.   It is an int, but what does it contain?  and what is it being sent to?  The calling application?

exec sp_prepexec @p1 output,N'@p0 varchar(8000),@p1 ...
SELECT TOP (@p1) this_.ProjectId as ProjectId78_0_, ...
',@p0='jimrogers',@p1=10,@p2='jimrogers',@p4=10
select @p1

Dean-O United States

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading