1- -- Compile Date: 03/24 /2025 14:11:31 UTC
1+ -- Compile Date: 03/25 /2025 20:35:56 UTC
22SET ANSI_NULLS ON;
33SET ANSI_PADDING ON;
44SET ANSI_WARNINGS ON;
@@ -10807,12 +10807,12 @@ BEGIN
1080710807 lock_mode nvarchar(10) NULL,
1080810808 resource_owner_type nvarchar(256) NULL,
1080910809 transaction_count int NULL,
10810- transaction_name nvarchar(512 ) NULL,
10810+ transaction_name nvarchar(1024 ) NULL,
1081110811 last_transaction_started datetime2(7) NULL,
1081210812 last_transaction_completed datetime2(7) NULL,
1081310813 client_option_1 varchar(261) NULL,
1081410814 client_option_2 varchar(307) NULL,
10815- wait_resource nvarchar(100 ) NULL,
10815+ wait_resource nvarchar(1024 ) NULL,
1081610816 priority int NULL,
1081710817 log_used bigint NULL,
1081810818 client_app nvarchar(256) NULL,
@@ -11281,7 +11281,7 @@ BEGIN
1128111281 wait_time = bd.value('(process/@waittime)[1]', 'bigint'),
1128211282 lastbatchstarted = bd.value('(process/@lastbatchstarted)[1]', 'datetime2'),
1128311283 lastbatchcompleted = bd.value('(process/@lastbatchcompleted)[1]', 'datetime2'),
11284- wait_resource = bd.value('(process/@waitresource)[1]', 'nvarchar(100 )'),
11284+ wait_resource = bd.value('(process/@waitresource)[1]', 'nvarchar(1024 )'),
1128511285 status = bd.value('(process/@status)[1]', 'nvarchar(10)'),
1128611286 priority = bd.value('(process/@priority)[1]', 'integer'),
1128711287 transaction_count = bd.value('(process/@trancount)[1]', 'integer'),
@@ -11338,7 +11338,7 @@ BEGIN
1133811338 wait_time = bg.value('(process/@waittime)[1]', 'bigint'),
1133911339 last_transaction_started = bg.value('(process/@lastbatchstarted)[1]', 'datetime2'),
1134011340 last_transaction_completed = bg.value('(process/@lastbatchcompleted)[1]', 'datetime2'),
11341- wait_resource = bg.value('(process/@waitresource)[1]', 'nvarchar(100 )'),
11341+ wait_resource = bg.value('(process/@waitresource)[1]', 'nvarchar(1024 )'),
1134211342 status = bg.value('(process/@status)[1]', 'nvarchar(10)'),
1134311343 priority = bg.value('(process/@priority)[1]', 'integer'),
1134411344 transaction_count = bg.value('(process/@trancount)[1]', 'integer'),
@@ -11890,10 +11890,10 @@ SELECT
1189011890 blocked_ecid = bd.value('(process/@ecid)[1]', 'integer'),
1189111891 query_text_pre = bd.value('(process/inputbuf/text())[1]', 'nvarchar(max)'),
1189211892 wait_time = bd.value('(process/@waittime)[1]', 'bigint'),
11893- transaction_name = bd.value('(process/@transactionname)[1]', 'nvarchar(512 )'),
11893+ transaction_name = bd.value('(process/@transactionname)[1]', 'nvarchar(1024 )'),
1189411894 last_transaction_started = bd.value('(process/@lasttranstarted)[1]', 'datetime2'),
1189511895 last_transaction_completed = CONVERT(datetime2, NULL),
11896- wait_resource = bd.value('(process/@waitresource)[1]', 'nvarchar(100 )'),
11896+ wait_resource = bd.value('(process/@waitresource)[1]', 'nvarchar(1024 )'),
1189711897 lock_mode = bd.value('(process/@lockMode)[1]', 'nvarchar(10)'),
1189811898 status = bd.value('(process/@status)[1]', 'nvarchar(10)'),
1189911899 priority = bd.value('(process/@priority)[1]', 'integer'),
@@ -12010,10 +12010,10 @@ SELECT
1201012010 blocked_ecid = bd.value('(process/@ecid)[1]', 'integer'),
1201112011 query_text_pre = bg.value('(process/inputbuf/text())[1]', 'nvarchar(max)'),
1201212012 wait_time = bg.value('(process/@waittime)[1]', 'bigint'),
12013- transaction_name = bg.value('(process/@transactionname)[1]', 'nvarchar(512 )'),
12013+ transaction_name = bg.value('(process/@transactionname)[1]', 'nvarchar(1024 )'),
1201412014 last_transaction_started = bg.value('(process/@lastbatchstarted)[1]', 'datetime2'),
1201512015 last_transaction_completed = bg.value('(process/@lastbatchcompleted)[1]', 'datetime2'),
12016- wait_resource = bg.value('(process/@waitresource)[1]', 'nvarchar(100 )'),
12016+ wait_resource = bg.value('(process/@waitresource)[1]', 'nvarchar(1024 )'),
1201712017 lock_mode = bg.value('(process/@lockMode)[1]', 'nvarchar(10)'),
1201812018 status = bg.value('(process/@status)[1]', 'nvarchar(10)'),
1201912019 priority = bg.value('(process/@priority)[1]', 'integer'),
@@ -28591,19 +28591,21 @@ BEGIN
2859128591 /* Choose appropriate string split function based on data type */
2859228592 IF @data_type = N'bigint'
2859328593 BEGIN
28594- SELECT @split_sql = @string_split_ints;
28594+ SELECT
28595+ @split_sql = @string_split_ints;
2859528596 END
2859628597 ELSE
2859728598 BEGIN
28598- SELECT @split_sql = @string_split_strings;
28599+ SELECT
28600+ @split_sql = @string_split_strings;
2859928601 END;
2860028602
2860128603 /* Execute the initial insert with troubleshooting if enabled */
2860228604 IF @troubleshoot_performance = 1
2860328605 BEGIN
2860428606 EXECUTE sys.sp_executesql
2860528607 @troubleshoot_insert,
28606- N'@current_table nvarchar(100)',
28608+ N'@current_table nvarchar(100)',
2860728609 @current_table;
2860828610
2860928611 SET STATISTICS XML ON;
@@ -28620,9 +28622,14 @@ BEGIN
2862028622 N')
2862128623 EXECUTE sys.sp_executesql
2862228624 @split_sql,
28623- N''@ids nvarchar(4000)'',
28625+ N''@ids nvarchar(4000)'',
2862428626 @param_value;';
2862528627
28628+ IF @debug = 1
28629+ BEGIN
28630+ PRINT @dynamic_sql;
28631+ END;
28632+
2862628633 EXEC sys.sp_executesql
2862728634 @dynamic_sql,
2862828635 N'@split_sql nvarchar(max),
@@ -28636,7 +28643,7 @@ BEGIN
2863628643
2863728644 EXECUTE sys.sp_executesql
2863828645 @troubleshoot_update,
28639- N'@current_table nvarchar(100)',
28646+ N'@current_table nvarchar(100)',
2864028647 @current_table;
2864128648
2864228649 EXECUTE sys.sp_executesql
@@ -28657,7 +28664,9 @@ BEGIN
2865728664 IF @param_name = 'include_query_ids'
2865828665 OR @param_name = 'ignore_query_ids'
2865928666 BEGIN
28660- SELECT @secondary_sql = N'
28667+ SET @secondary_sql = @isolation_level;
28668+
28669+ SELECT @secondary_sql += N'
2866128670 SELECT DISTINCT
2866228671 qsp.plan_id
2866328672 FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
@@ -28677,10 +28686,13 @@ BEGIN
2867728686 OPTION(RECOMPILE);' + @nc10;
2867828687 END;
2867928688 ELSE
28689+
2868028690 IF @param_name = 'include_query_hashes'
2868128691 OR @param_name = 'ignore_query_hashes'
2868228692 BEGIN
28683- SELECT @secondary_sql = N'
28693+ SET @secondary_sql = @isolation_level;
28694+
28695+ SELECT @secondary_sql += N'
2868428696 SELECT DISTINCT
2868528697 qsp.plan_id
2868628698 FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
@@ -28707,10 +28719,13 @@ BEGIN
2870728719 OPTION(RECOMPILE);' + @nc10;
2870828720 END;
2870928721 ELSE
28722+
2871028723 IF @param_name = 'include_plan_hashes'
2871128724 OR @param_name = 'ignore_plan_hashes'
2871228725 BEGIN
28713- SELECT @secondary_sql = N'
28726+ SET @secondary_sql = @isolation_level;
28727+
28728+ SELECT @secondary_sql += N'
2871428729 SELECT DISTINCT
2871528730 qsp.plan_id
2871628731 FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
@@ -28729,11 +28744,13 @@ BEGIN
2872928744 OPTION(RECOMPILE);' + @nc10;
2873028745 END;
2873128746 ELSE
28732- IF
28733- @param_name = 'include_sql_handles'
28747+
28748+ IF @param_name = 'include_sql_handles'
2873428749 OR @param_name = 'ignore_sql_handles'
2873528750 BEGIN
28736- SELECT @secondary_sql = N'
28751+ SET @secondary_sql = @isolation_level;
28752+
28753+ SELECT @secondary_sql += N'
2873728754 SELECT DISTINCT
2873828755 qsp.plan_id
2873928756 FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
@@ -28773,40 +28790,67 @@ BEGIN
2877328790 BEGIN
2877428791 EXECUTE sys.sp_executesql
2877528792 @troubleshoot_insert,
28776- N'@current_table nvarchar(100)',
28793+ N'@current_table nvarchar(100)',
2877728794 @current_table;
2877828795
2877928796 SET STATISTICS XML ON;
2878028797 END;
2878128798
28782- INSERT INTO
28783- #include_plan_ids
28784- WITH
28785- (TABLOCK)
28786- (
28787- plan_id
28788- )
28789- EXECUTE sys.sp_executesql
28790- @secondary_sql;
28799+ IF @debug = 1
28800+ BEGIN
28801+ PRINT @secondary_sql;
28802+ END;
28803+
28804+ /* Insert into the correct target table based on include/ignore */
28805+ IF @is_include = 1
28806+ BEGIN
28807+ INSERT INTO
28808+ #include_plan_ids
28809+ WITH
28810+ (TABLOCK)
28811+ (
28812+ plan_id
28813+ )
28814+ EXECUTE sys.sp_executesql
28815+ @secondary_sql;
28816+ END
28817+ ELSE
28818+ BEGIN
28819+ INSERT INTO
28820+ #ignore_plan_ids
28821+ WITH
28822+ (TABLOCK)
28823+ (
28824+ plan_id
28825+ )
28826+ EXECUTE sys.sp_executesql
28827+ @secondary_sql;
28828+ END;
2879128829
2879228830 IF @troubleshoot_performance = 1
2879328831 BEGIN
2879428832 SET STATISTICS XML OFF;
2879528833
2879628834 EXECUTE sys.sp_executesql
2879728835 @troubleshoot_update,
28798- N'@current_table nvarchar(100)',
28836+ N'@current_table nvarchar(100)',
2879928837 @current_table;
2880028838
2880128839 EXECUTE sys.sp_executesql
2880228840 @troubleshoot_info,
28803- N'@sql nvarchar(max), @current_table nvarchar(100)',
28841+ N'@sql nvarchar(max), @current_table nvarchar(100)',
2880428842 @secondary_sql,
2880528843 @current_table;
2880628844 END;
2880728845 END;
28846+ END;
2880828847
28809- /* Update where clause if needed */
28848+ /* Update where clause based on parameter type */
28849+ IF @param_name = 'include_plan_ids'
28850+ OR @param_name = 'ignore_plan_ids'
28851+ OR @requires_secondary_processing = 1
28852+ BEGIN
28853+ /* Choose the correct table and exists/not exists operator */
2881028854 SELECT
2881128855 @temp_target_table =
2881228856 CASE
@@ -28821,6 +28865,7 @@ BEGIN
2882128865 ELSE N'NOT EXISTS'
2882228866 END;
2882328867
28868+ /* Add the filter condition to the where clause */
2882428869 SELECT
2882528870 @where_clause +=
2882628871 N'AND ' +
@@ -28832,6 +28877,11 @@ BEGIN
2883228877 FROM ' + @temp_target_table + N' AS idi
2883328878 WHERE idi.plan_id = qsrs.plan_id
2883428879 )' + @nc10;
28880+
28881+ IF @debug = 1
28882+ BEGIN
28883+ PRINT @where_clause;
28884+ END;
2883528885 END;
2883628886
2883728887 FETCH NEXT
0 commit comments