SQL Server sp_get_query_template doesn't work with ODBC timestamp

dsf9zpds  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(126)

I have the following query:

  1. SELECT COUNT(*)
  2. FROM (
  3. SELECT DISTINCT USER_NAME
  4. FROM LICENSE
  5. WHERE RESTRICTED_USE = 'T'
  6. AND ALLOCATED = 'T'
  7. UNION ALL
  8. SELECT USER_NAME
  9. FROM LICENSE
  10. WHERE RESTRICTED_USE = 'T'
  11. AND ALLOCATED = 'F'
  12. AND "TIME" >= {ts '2023-10-02 08:09:12'}
  13. ) AS TEMP_ALIAS

When I try to create a plan guide with sp_get_query_template the plan guide doesn't work and I think that the cause is the ODBC timestamp value.

  1. DECLARE @stmt nvarchar(max);
  2. DECLARE @params nvarchar(max);
  3. EXEC sp_get_query_template
  4. N'SELECT COUNT(*) FROM (SELECT DISTINCT USER_NAME FROM LICENSE WHERE RESTRICTED_USE = ''T'' AND ALLOCATED = ''T'' UNION ALL SELECT USER_NAME FROM LICENSE WHERE RESTRICTED_USE = ''T'' AND ALLOCATED = ''F'' AND "TIME" >= {ts ''2023-10-02 08:09:12''})AS TEMP_ALIAS',
  5. @stmt OUTPUT,
  6. @params OUTPUT;
  7. select @stmt
  8. select @params
  9. EXEC sp_create_plan_guide
  10. N'UsersCountTable',
  11. @stmt,
  12. N'TEMPLATE',
  13. NULL,
  14. @params,
  15. N'OPTION(PARAMETERIZATION FORCED)';

I've tried also specifying timestamp parameter as: VARCHAR(), DATEATIME, SMALLDATETIME, DATETIME2 with no success.

wbrvyc0a

wbrvyc0a1#

I thought that i've found the solution (thanks to Dale K comment)

I changed the query by removing the double quotes thinking that this would work but after a while those execution plans reappeared.

相关问题