小编J.T*_*.T.的帖子

sp_prepexec (sp_execute) 与 sp_executeSQL

问题的核心:实际存储过程是实现临时表缓存的唯一机制还是系统存储过程(例如sp_executeSQL/sp_execute也利用它们)?

我不是 DBA,所以请少说几句。我们的应用程序发送准备好的语句,从分析器中,我看到运行所有 SQL,sp_prepexec它是运行sp_preparesp_execute. 我想要做的是弄清楚我是否从临时表缓存中受益。

我一直在使用带有 object_id() 的本指南来检查行为

https://sqlkiwi.blogspot.com/2012/08/temporary-tables-in-stored-procedures.html

然后这篇博文中的第 3 点表明 EXEC 不能使用临时表缓存,但忽略了 sp_executeSQL 是否可以:http : //blogs.msdn.com/b/turgays/archive/2013/09/18/exec-vs- sp-executesql.aspx

在我通过客户端发送的查询中,我创建了一个简单的临时表。

DECLARE @foo int; -- set by JDBC, unused but required to force a prepared statement

SELECT 1 AS id
    INTO #tmp

SELECT OBJECT_ID('tempdb..#tmp');
Run Code Online (Sandbox Code Playgroud)

在探查器中,我可以看到:

declare @p1 int
set @p1=NULL
exec sp_prepexec @p1 output,N'@P1 int',N'declare @foo INT = @P1 

SELECT 1 as id
    into #tmp

select Object_id(''tempdb..#tmp'');
DROP TABLE #tmp;',1 …
Run Code Online (Sandbox Code Playgroud)

sql-server stored-procedures dynamic-sql prepared-statement temporary-tables

8
推荐指数
1
解决办法
5799
查看次数