Fixed simply by removing a sneaky execute hiding after the procedure.
When I execute my stored procedure, it keeps on going in an infinite loop when I only want it to iterate once.
It successfully inserts the correct number of rows into my PartitionInfo
table, but when I use the cursor to iterate through, it will keep going until I get an error:
Msg 217, Level 16, State 1, Procedure ConnectorFile.GetPartitionInfo, Line 85 [Batch Start Line 84]
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)
This is my code:
CREATE OR ALTER PROCEDURE [ConnectorFile].[GetPartitionInfo]
@tableName NVARCHAR(128)
AS
BEGIN
SET NOCOUNT ON;
-- Declare a table variable to hold the partition information
DECLARE @PartitionInfo TABLE
(
table_name NVARCHAR(128),
partition_range NVARCHAR(128),
file_group_name NVARCHAR(128),
partition_number INT,
number_of_rows INT
);
-- Retrieve partition information and insert it into the table variable
INSERT INTO @PartitionInfo (table_name, partition_range, file_group_name, partition_number, number_of_rows)
SELECT DISTINCT
o.name AS table_name,
CONVERT(NVARCHAR(128), rv.value) AS partition_range,
fg.name AS file_group_name,
p.partition_number,
p.rows AS number_of_rows
FROM
sys.partitions p
INNER JOIN
sys.indexes i ON p.object_id = i.object_id
AND p.index_id = i.index_id
INNER JOIN
sys.objects o ON p.object_id = o.object_id
INNER JOIN
sys.system_internals_allocation_units au ON p.partition_id = au.container_id
INNER JOIN
sys.partition_schemes ps ON ps.data_space_id = i.data_space_id
INNER JOIN
sys.partition_functions f ON f.function_id = ps.function_id
INNER JOIN
sys.destination_data_spaces dds ON dds.partition_scheme_id = ps.data_space_id
AND dds.destination_id = p.partition_number
INNER JOIN
sys.filegroups fg ON dds.data_space_id = fg.data_space_id
LEFT OUTER JOIN
sys.partition_range_values rv ON f.function_id = rv.function_id
AND p.partition_number = rv.boundary_id
WHERE
o.object_id = OBJECT_ID(@tableName);
DECLARE @table_name NVARCHAR(128);
DECLARE @partition_range NVARCHAR(128);
DECLARE @file_group_name NVARCHAR(128);
DECLARE @partition_number INT;
DECLARE @number_of_rows INT;
-- Cursor to iterate through each row in the table variable
DECLARE cur CURSOR FOR
SELECT table_name, partition_range, file_group_name, partition_number, number_of_rows
FROM @PartitionInfo
ORDER BY partition_number;
OPEN cur;
FETCH NEXT FROM cur INTO @table_name, @partition_range, @file_group_name, @partition_number, @number_of_rows;
WHILE @@FETCH_STATUS = 0
BEGIN
IF @number_of_rows > 5000000
BEGIN
PRINT CONCAT('Splitting partition: ', @partition_number, ', Rows: ', @number_of_rows);
END
ELSE
BEGIN
PRINT CONCAT('NOT Splitting partition: ', @partition_number, ', Rows: ', @number_of_rows);
END
FETCH NEXT FROM cur INTO @table_name, @partition_range, @file_group_name, @partition_number, @number_of_rows;
END;
CLOSE cur;
DEALLOCATE cur;
END;
As said, I have checked if PartitionInfo
table, it contains the correct number of rows (in my case 8), and 8 exists, but when it has executed 8 it just starts over!
4