SQL Server:无法从查询分析器中找到句柄为 10 的准备好的语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9830881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:57:53  来源:igfitidea点击:

SQL Server: Could not find prepared statement with handle 10 from query analizer

sqlsql-server

提问by sproketboy

I run the SQL profiler and I want to run some of the queries in the query analyser but I get an error "SQL Server: Could not find prepared statement with handle x".

我运行 SQL 探查器,我想在查询分析器中运行一些查询,但出现错误“SQL Server:找不到带有句柄 x 的准备好的语句”。

Any ideas?

有任何想法吗?

This is the SQL I have copied from the profiler:

这是我从探查器中复制的 SQL:

declare @p1 int
set @p1=10
exec sp_prepare @p1 output,N'@P0 int,@P1 nvarchar(4000),@P2 datetime,@P3 datetime,@P4 datetime,@P5 datetime,@P6 datetime,@P7 datetime',N'SELECT * FROM SCHEDULE WITH (NOLOCK) WHERE RoomNo= @P0  AND STATUS =  @P1   AND ( (EndTimeDT <=  @P2  AND EndTimeDT >  @P3 ) OR (StartTimeDT >=  @P4  AND StartTimeDT <  @P5 )  OR (StartTimeDT <=  @P6  AND EndTimeDT >  @P7 ) )',1
select @p1
go
exec sp_execute 10,19,N'A','2012-03-22 16:30:00','2012-03-22 16:00:00','2012-03-22 16:00:00','2012-03-22 16:30:00','2012-03-22 16:00:00','2012-03-22 16:30:00'
go

采纳答案by sproketboy

It seems you need to modify the profiler text if you want to run it in the query analizer.

如果您想在查询分析器中运行它,似乎您需要修改分析器文本。

declare @p1 int
exec sp_prepare @p1 output,N'@P0 int,@P1 nvarchar(4000),@P2 datetime,@P3 datetime,@P4 datetime,@P5 datetime,@P6 datetime,@P7 datetime',N'SELECT * FROM SCHEDULE WITH (NOLOCK) WHERE RoomNo= @P0  AND STATUS =  @P1   AND ( (EndTimeDT <=  @P2  AND EndTimeDT >  @P3 ) OR (StartTimeDT >=  @P4  AND StartTimeDT <  @P5 )  OR (StartTimeDT <=  @P6  AND EndTimeDT >  @P7 ) )',1
select @p1
exec sp_execute @p1,19,N'A','2012-03-22 16:30:00','2012-03-22 16:00:00','2012-03-22 16:00:00','2012-03-22 16:30:00','2012-03-22 16:00:00','2012-03-22 16:30:00'
go

回答by Smith

You should use RPC:Startingqueries not RPC:Completed

您应该使用RPC:Starting查询而不是RPC:Completed

In profiler you would normally see RPC:Startingand RPC:Completed. The statement shown in RPC:Staringis what you need to pick, RPC:Completedwould include output values that were not passed from the client.

在探查器中,您通常会看到RPC:StartingRPC:Completed。中显示的语句RPC:Staring是您需要选择的,RPC:Completed将包括未从客户端传递的输出值。

If you use RPC:Completedyou should remove SET set @p1=10the queries to work

如果您使用,RPC:Completed您应该删除 SETset @p1=10查询才能工作

回答by Diego

this is my second answer, I misunderstood the question when I wrote the first one.

这是我的第二个答案,我在写第一个答案时误解了这个问题。

Why are you doing set @p1=10? You are not supposed to set this variable. try removing it

你为什么要这样做set @p1=10?您不应该设置此变量。尝试删除它