0

这个语句有一些奇怪的东西 COALESCE(@param_ids + ',', '')

@param_ids 作为参数传入,它是 @param_ids VARCHAR(MAX)

知道为什么 SQL-Server 无法为以下场景生成缓存计划。此问题已上报给微软,他们仍在努力解决。

不工作的场景 - 没有生成缓存计划第 1 步

更改 SP

 IF (LTRIM(RTRIM(ISNULL(@param_ids,''))) = '')  
  BEGIN  
   SELECT @param_ids = COALESCE(@param_ids + ',', '') + CONVERT(VARCHAR(50),ID)  
   FROM [dbo].Content_Type WITH (NOLOCK)  
  END  

第 2 步 Exec Sp 带参数

第 3 步(无缓存计划)

SELECT *
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
WHERE [dbid] = DB_ID('databasename')
AND [objectid] = OBJECT_ID('databasename.dbo.us_spname')
GO

工作场景——生成缓存计划 Step 1

更改 SP

 IF (LTRIM(RTRIM(ISNULL(@param_ids,''))) = '')  
  BEGIN  
   --SELECT @param_ids = COALESCE(@param_ids + ',', '') + CONVERT(VARCHAR(50),ID)  
   --FROM [dbo].Content_Type WITH (NOLOCK)  

    SELECT @param_ids = COALESCE('aaaaaaa' + ',', '') + CONVERT(VARCHAR(50),ID)  
    FROM [dbo].Content_Type WITH (NOLOCK)   
  END  

第 2 步 Exec Sp 带参数

步骤 3(缓存计划退出)

SELECT *
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
WHERE [dbid] = DB_ID('databasename')
AND [objectid] = OBJECT_ID('databasename.dbo.us_spname')
GO

谢谢

4

1 回答 1

0

我添加了一个 WAITFOR 并且我一直得到一个计划。这应该与参数无关

CREATE PROC dbo.testplan @param_ids varchar(max) OUTPUT
AS
IF (LTRIM(RTRIM(ISNULL(@param_ids,''))) = '')  
  BEGIN  
   SELECT @param_ids = COALESCE(@param_ids + ',', '') + CONVERT(VARCHAR(50),ID)  
   FROM [dbo].Content_Type WITH (NOLOCK)  
  END  
GO
CREATE PROC dbo.testplan2 @param_ids varchar(max) OUTPUT
AS
IF (LTRIM(RTRIM(ISNULL(@param_ids,''))) = '')  
  BEGIN  
   SELECT @param_ids = COALESCE(@param_ids + ',', '') + CONVERT(VARCHAR(50),ID)  
   FROM [dbo].Content_Type WITH (NOLOCK)  
  END  
GO

EXEC dbo.testplan 'bar'
GO
WAITFOR DELAY '00:00:02'
SELECT OBJECT_NAME([objectid]), *
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
WHERE [dbid] = DB_ID()
AND [objectid] = OBJECT_ID('testplan')
GO

EXEC dbo.testplan2 ''
GO
WAITFOR DELAY '00:00:02'
SELECT OBJECT_NAME([objectid]), *
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
WHERE [dbid] = DB_ID()
AND [objectid] = OBJECT_ID('testplan2')
GO
于 2011-02-02T07:19:15.587 回答