Guest User

Untitled

a guest
Apr 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. CREATE TABLE [dbo].[Code] (
  2. [CodeId] INT IDENTITY (1, 1) NOT NULL,
  3. [Serial] VARCHAR(20) NOT NULL,
  4. [AggregationLevelId] TINYINT NOT NULL,
  5. [CommissioningFlag] TINYINT NOT NULL,
  6. [ ... ]
  7. CONSTRAINT [PK_CODE] PRIMARY KEY CLUSTERED ([CodeId] ASC),
  8. CONSTRAINT [UC_CODE_SERIAL] UNIQUE NONCLUSTERED ([Serial] ASC),
  9. CONSTRAINT [FK_Code_AggregationLevelConfiguration]
  10. FOREIGN KEY ([AggregationLevelId])
  11. REFERENCES [dbo].[AggregationLevelConfiguration] ([AggregationLevelConfigurationId])
  12. )
  13.  
  14. CREATE TABLE [dbo].[Aggregation] (
  15. [AggregationId] INT NOT NULL,
  16. CONSTRAINT [PK_AGGREGATIONS] PRIMARY KEY CLUSTERED ([AggregationId] ASC),
  17. CONSTRAINT [FK_Aggregation_Code]
  18. FOREIGN KEY ([AggregationId])
  19. REFERENCES [dbo].[Code] ([CodeId])
  20. )
  21.  
  22. CREATE TABLE [dbo].[AggregationChildren] (
  23. [AggregationChildrenId] INT NOT NULL,
  24. [AggregationId] INT NOT NULL,
  25. [Position] INT NOT NULL,
  26. CONSTRAINT [PK_AGGREGATION_CHILDS] PRIMARY KEY CLUSTERED ([AggregationChildrenId] ASC),
  27. CONSTRAINT [FK_AggregationChildren_Code]
  28. FOREIGN KEY ([AggregationChildrenId])
  29. REFERENCES [dbo].[Code] ([CodeId]),
  30. CONSTRAINT [FK_AggregationChildren_Aggregation]
  31. FOREIGN KEY ([AggregationId])
  32. REFERENCES [dbo].[Aggregation] ([AggregationId]) ON DELETE CASCADE
  33. )
  34.  
  35. Code1
  36. |___________________
  37. | |
  38. Code2 Code3
  39. |________ |________________
  40. | | | | |
  41. Code4 Code5 Code6 Code7 Code8
  42.  
  43. INSERT INTO dbo.Code VALUES
  44. (1, 'Code1', 1, 1),
  45. (2, 'Code2', 2, 1),
  46. (3, 'Code3', 2, 1),
  47. (4, 'Code4', 3, 1),
  48. (5, 'Code5', 3, 1),
  49. (6, 'Code6', 3, 1),
  50. (7, 'Code7', 3, 1),
  51. (8, 'Code8', 3, 1),
  52. (9, 'Code9', 1, 3),
  53. (10, 'Code10', 2, 3),
  54. (11, 'Code11', 2, 3);
  55. GO
  56.  
  57. INSERT INTO dbo.Aggregation VALUES (1),(2),(3),(9);
  58. GO
  59.  
  60. INSERT INTO dbo.AggregationChildren VALUES
  61. (2, 1, 1),
  62. (3, 1, 2),
  63. (4, 2, 1),
  64. (5, 2, 2),
  65. (6, 3, 1),
  66. (7, 3, 2),
  67. (8, 3, 3),
  68. (10, 9, 1),
  69. (11, 9, 2);
  70. GO
  71.  
  72. DECLARE @Serial VARCHAR(20) = 'Code1';
  73.  
  74. SELECT CodeId FROM dbo.Code WHERE Serial = @Serial;
  75.  
  76. SELECT AggregationId Id
  77. FROM dbo.Aggregation
  78. WHERE AggregationId = (SELECT CodeId FROM dbo.Code WHERE Serial = @Serial);
  79. GO
  80.  
  81. DECLARE @Serial VARCHAR(20) = 'Code1';
  82.  
  83. ;WITH rc AS
  84. (
  85. SELECT AggregationId Id
  86. FROM dbo.Aggregation
  87. WHERE AggregationId = (SELECT CodeId FROM dbo.Code WHERE Serial = @Serial)
  88. UNION ALL
  89. SELECT ac.AggregationChildrenId Id
  90. FROM dbo.AggregationChildren ac
  91. JOIN rc
  92. ON ac.AggregationId = rc.Id
  93. )
  94. UPDATE c
  95. SET CommissioningFlag = 5
  96. FROM dbo.Code c
  97. JOIN rc
  98. ON c.CodeId = rc.Id;
  99. GO
  100.  
  101. DECLARE @Serial VARCHAR(20) = 'Code1';
  102.  
  103. ;WITH rc AS
  104. (
  105. SELECT AggregationId Id
  106. FROM dbo.Aggregation
  107. WHERE AggregationId = (SELECT CodeId FROM dbo.Code WHERE Serial = @Serial)
  108. UNION ALL
  109. SELECT ac.AggregationChildrenId Id
  110. FROM dbo.AggregationChildren ac
  111. JOIN rc
  112. ON ac.AggregationId = rc.Id
  113. )
  114. UPDATE c
  115. SET CommissioningFlag = 5
  116. FROM dbo.Code c
  117. JOIN rc
  118. ON c.CodeId = rc.Id;
  119. GO
  120.  
  121. SELECT * FROM dbo.Code;
  122. GO
  123.  
  124. SELECT * FROM dbo.Code;
  125. GO
Add Comment
Please, Sign In to add comment