Advertisement
JanuszKowalski123

10. hierarchyid 5

May 9th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.36 KB | None | 0 0
  1. CREATE TABLE Renewable_Energy_Devices (
  2.     device_id NVARCHAR(26) PRIMARY KEY,
  3.     device_status INT CHECK (device_status >= 0 AND device_status <= 6),
  4.     device_power DECIMAL(18,0),
  5.     company_id INT,
  6.     device_location GEOGRAPHY,
  7.     device_location_geom GEOMETRY,
  8.     device_hierarchy HIERARCHYID,
  9.     device_description XML
  10. );
  11.  
  12. DECLARE @latitude DECIMAL(9, 6), @longitude DECIMAL(9, 6);
  13. DECLARE @point GEOMETRY;
  14. DECLARE @hierarchy HIERARCHYID;
  15. DECLARE @device_description XML;
  16.  
  17. DECLARE @i INT = 1;
  18. WHILE @i <= 50
  19. BEGIN
  20.     SET @latitude = RAND() * 180 - 90;
  21.     SET @longitude = RAND() * 360 - 180;
  22.     SET @point = GEOMETRY::Point(@latitude, @longitude, 4326);
  23.     SET @hierarchy = HIERARCHYID::GetRoot();
  24.     SET @device_description = '<description>Device_' + CAST(@i AS NVARCHAR(5)) + '</description>'; -- Zmiana sposobu tworzenia wartości dla kolumny device_description
  25.  
  26.     INSERT INTO Renewable_Energy_Devices (device_id, device_status, device_power, company_id, device_location, device_location_geom, device_hierarchy, device_description)
  27.     VALUES (
  28.         'Device_' + CAST(@i AS NVARCHAR(5)),
  29.         CAST(RAND() * 6 AS INT),
  30.         CAST(RAND() * 1000 AS DECIMAL(18, 0)),
  31.         CAST(RAND() * 10 AS INT) + 1,
  32.         GEOGRAPHY::Point(@latitude, @longitude, 4326),
  33.         @point,
  34.         @hierarchy.GetDescendant(NULL, NULL),
  35.         @device_description
  36.     );
  37.  
  38.     SET @i = @i + 1;
  39. END;
  40.  
  41. WITH DeviceHierarchyCTE AS (
  42.     SELECT
  43.         device_id,
  44.         device_hierarchy,
  45.         CAST('/' + device_id + '/' AS NVARCHAR(MAX)) AS hierarchy_path,
  46.         CAST('/' + device_id + '/' AS NVARCHAR(MAX)) AS device_path,
  47.         1 AS hierarchy_level
  48.     FROM
  49.         Renewable_Energy_Devices
  50.     WHERE
  51.         device_hierarchy = HIERARCHYID::GetRoot()
  52.  
  53.     UNION ALL
  54.  
  55.     SELECT
  56.         red.device_id,
  57.         red.device_hierarchy,
  58.         dhc.hierarchy_path + red.device_id + '/' AS hierarchy_path,
  59.         dhc.device_path + red.device_id + '/' AS device_path,
  60.         dhc.hierarchy_level + 1
  61.     FROM
  62.         Renewable_Energy_Devices AS red
  63.     INNER JOIN
  64.         DeviceHierarchyCTE AS dhc ON dhc.device_hierarchy.IsDescendantOf(red.device_hierarchy) = 1
  65. )
  66. SELECT
  67.     device_id,
  68.     device_hierarchy.ToString() AS hierarchy_path,
  69.     device_path,
  70.     hierarchy_level
  71. FROM
  72.     DeviceHierarchyCTE
  73. ORDER BY
  74.     hierarchy_path;
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement