@@ -1685,7 +1685,13 @@ DECLARE
16851685 @requires_secondary_processing bit ,
16861686 @split_sql nvarchar (max ),
16871687 @error_msg nvarchar (2000 ),
1688- @conflict_list nvarchar (max ) = N ' ' ;
1688+ @conflict_list nvarchar (max ) = N ' ' ,
1689+ @database_cursor CURSOR ,
1690+ @filter_cursor CURSOR ,
1691+ @dynamic_sql nvarchar (max ) = N ' ' ,
1692+ @secondary_sql nvarchar (max ) = N ' ' ,
1693+ @temp_target_table nvarchar (100 ),
1694+ @exist_or_not_exist nvarchar (20 );
16891695
16901696/*
16911697In cases where we are escaping @query_text_search and
@@ -2251,9 +2257,6 @@ BEGIN
22512257 END ;
22522258END ;
22532259
2254- DECLARE
2255- @database_cursor CURSOR ;
2256-
22572260SET
22582261 @database_cursor =
22592262 CURSOR
@@ -2530,7 +2533,8 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;',
25302533 ) AS ids
25312534 CROSS APPLY ids.nodes('' x'' ) AS x (x)
25322535 ) AS ids
2533- OPTION(RECOMPILE);' ,
2536+ OPTION(RECOMPILE);
2537+ ' ,
25342538 @string_split_strings = N'
25352539 SELECT DISTINCT
25362540 ids =
@@ -2574,7 +2578,8 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;',
25742578 ) AS ids
25752579 CROSS APPLY ids.nodes('' x'' ) AS x (x)
25762580 ) AS ids
2577- OPTION(RECOMPILE);' ,
2581+ OPTION(RECOMPILE);
2582+ ' ,
25782583 @troubleshoot_insert = N'
25792584 INSERT
25802585 #troubleshoot_performance
@@ -2589,15 +2594,17 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;',
25892594 @current_table,
25902595 GETDATE()
25912596 )
2592- OPTION(RECOMPILE);' ,
2597+ OPTION(RECOMPILE);
2598+ ' ,
25932599 @troubleshoot_update = N'
25942600 UPDATE
25952601 tp
25962602 SET
25972603 tp.end_time = GETDATE()
25982604 FROM #troubleshoot_performance AS tp
25992605 WHERE tp.current_table = @current_table
2600- OPTION(RECOMPILE);' ,
2606+ OPTION(RECOMPILE);
2607+ ' ,
26012608 @troubleshoot_info = N'
26022609 SELECT
26032610 (
@@ -2616,7 +2623,8 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;',
26162623 PATH(N'' '' ),
26172624 TYPE
26182625 ).query('' .[1]'' ) AS current_query
2619- OPTION(RECOMPILE);' ,
2626+ OPTION(RECOMPILE);
2627+ ' ,
26202628 @rc = 0 ,
26212629 @em = @expert_mode,
26222630 @fo = @format_output,
@@ -4041,9 +4049,6 @@ OR @ignore_plan_hashes IS NOT NULL
40414049OR @ignore_sql_handles IS NOT NULL
40424050)
40434051BEGIN
4044- DECLARE
4045- @filter_cursor CURSOR ;
4046-
40474052 SET @filter_cursor =
40484053 CURSOR
40494054 LOCAL
@@ -4116,9 +4121,9 @@ BEGIN
41164121 END ;
41174122
41184123 /* Execute the dynamic SQL to populate the temporary table */
4119- DECLARE @dynamic_sql nvarchar ( max ) = N'
4120- INSERT INTO
4121- ' + @temp_table + N'
4124+ SET @dynamic_sql = N'
4125+ INSERT INTO
4126+ ' + @temp_table + N'
41224127 WITH
41234128 (TABLOCK)
41244129 (
@@ -4159,11 +4164,8 @@ BEGIN
41594164 SELECT
41604165 @current_table = ' inserting #include_plan_ids for ' + @param_name;
41614166
4162- /* Build appropriate SQL based on parameter type */
4163- DECLARE
4164- @secondary_sql nvarchar (max ) = N ' ' ;
4165-
4166- IF @param_name = ' include_query_ids'
4167+ /* Build appropriate SQL based on parameter type */
4168+ IF @param_name = ' include_query_ids'
41674169 OR @param_name = ' ignore_query_ids'
41684170 BEGIN
41694171 SELECT @secondary_sql = N'
@@ -4183,7 +4185,7 @@ BEGIN
41834185 N' _query_ids AS iqi
41844186 WHERE iqi.query_id = qsp.query_id
41854187 )
4186- OPTION(RECOMPILE);' ;
4188+ OPTION(RECOMPILE);' + @nc10 ;
41874189 END ;
41884190 ELSE
41894191 IF @param_name = ' include_query_hashes'
@@ -4213,7 +4215,7 @@ BEGIN
42134215 WHERE iqh.query_hash = qsq.query_hash
42144216 )
42154217 )
4216- OPTION(RECOMPILE);' ;
4218+ OPTION(RECOMPILE);' + @nc10 ;
42174219 END ;
42184220 ELSE
42194221 IF @param_name = ' include_plan_hashes'
@@ -4235,7 +4237,7 @@ BEGIN
42354237 END + N' _plan_hashes AS iph
42364238 WHERE iph.plan_hash = qsp.query_plan_hash
42374239 )
4238- OPTION(RECOMPILE);' ;
4240+ OPTION(RECOMPILE);' + @nc10 ;
42394241 END ;
42404242 ELSE
42414243 IF
@@ -4272,7 +4274,7 @@ BEGIN
42724274 )
42734275 )
42744276 )
4275- OPTION(RECOMPILE);' ;
4277+ OPTION(RECOMPILE);' + @nc10 ;
42764278 END ;
42774279
42784280 /* Process secondary sql if defined */
@@ -4316,14 +4318,14 @@ BEGIN
43164318 END ;
43174319
43184320 /* Update where clause if needed */
4319- DECLARE
4320- @temp_target_table nvarchar ( 100 ) =
4321+ SELECT
4322+ @temp_target_table =
43214323 CASE
43224324 WHEN @is_include = 1
43234325 THEN N ' #include_plan_ids'
43244326 ELSE N ' #ignore_plan_ids'
43254327 END ,
4326- @exist_or_not_exist nvarchar ( 20 ) =
4328+ @exist_or_not_exist =
43274329 CASE
43284330 WHEN @is_include = 1
43294331 THEN N ' EXISTS'
@@ -7722,7 +7724,7 @@ WHERE EXISTS
77227724 AND qsp.database_id = qsq.database_id
77237725 WHERE qsq.context_settings_id = qcs.context_settings_id
77247726 )
7725- OPTION(RECOMPILE);' ;
7727+ OPTION(RECOMPILE);' + @nc10 ;
77267728
77277729INSERT
77287730 #query_context_settings
@@ -8667,8 +8669,7 @@ ORDER BY
86678669 END END
86688670 END
86698671 + N' DESC
8670- OPTION(RECOMPILE);'
8671- + @nc10
8672+ OPTION(RECOMPILE);' + @nc10
86728673 );
86738674
86748675 IF @debug = 1
@@ -8778,7 +8779,7 @@ BEGIN
87788779 FROM #query_store_plan_feedback AS qspf
87798780 ORDER BY
87808781 qspf.plan_id
8781- OPTION(RECOMPILE);' ;
8782+ OPTION(RECOMPILE);' + @nc10 ;
87828783
87838784 IF @debug = 1
87848785 BEGIN
@@ -8836,7 +8837,7 @@ BEGIN
88368837 FROM #query_store_query_hints AS qsqh
88378838 ORDER BY
88388839 qsqh.query_id
8839- OPTION(RECOMPILE);' ;
8840+ OPTION(RECOMPILE);' + @nc10 ;
88408841
88418842 IF @debug = 1
88428843 BEGIN
@@ -8884,7 +8885,7 @@ BEGIN
88848885 FROM #query_store_query_variant AS qsqv
88858886 ORDER BY
88868887 qsqv.parent_query_id
8887- OPTION(RECOMPILE);' ;
8888+ OPTION(RECOMPILE);' + @nc10 ;
88888889
88898890 IF @debug = 1
88908891 BEGIN
@@ -9175,7 +9176,7 @@ BEGIN
91759176 WHERE x.n = 1
91769177 ORDER BY
91779178 x.query_id
9178- OPTION(RECOMPILE);' ;
9179+ OPTION(RECOMPILE);' + @nc10 ;
91799180
91809181 IF @debug = 1
91819182 BEGIN
@@ -9391,7 +9392,7 @@ BEGIN
93919392 )
93929393 ORDER BY
93939394 qsq.query_id
9394- OPTION(RECOMPILE);'
9395+ OPTION(RECOMPILE);' + @nc10
93959396 );
93969397
93979398 IF @debug = 1
@@ -9436,7 +9437,7 @@ BEGIN
94369437 (
94379438 nvarchar (max ),
94389439 N'
9439- SELECT DISTINCT
9440+ SELECT
94409441 source =
94419442 '' query_store_wait_stats_by_query'' ,
94429443 database_name =
@@ -9550,7 +9551,7 @@ BEGIN
95509551 ORDER BY
95519552 qsws.plan_id,
95529553 qsws.total_query_wait_time_ms DESC
9553- OPTION(RECOMPILE);'
9554+ OPTION(RECOMPILE);' + @nc10
95549555 );
95559556
95569557 IF @debug = 1
@@ -9689,7 +9690,7 @@ BEGIN
96899690 qsws.database_id
96909691 ORDER BY
96919692 SUM(qsws.total_query_wait_time_ms) DESC
9692- OPTION(RECOMPILE);'
9693+ OPTION(RECOMPILE);' + @nc10
96939694 );
96949695
96959696 IF @debug = 1
@@ -9789,7 +9790,7 @@ BEGIN
97899790 N'
97909791 dqso.size_based_cleanup_mode_desc
97919792 FROM #database_query_store_options AS dqso
9792- OPTION(RECOMPILE);'
9793+ OPTION(RECOMPILE);' + @nc10
97939794 );
97949795
97959796 IF @debug = 1
0 commit comments