sql-cte

Printing tree with SQL CTE

社会主义新天地 提交于 2019-12-18 07:43:40
问题 The schema is as follows: CREATE TABLE [Structure]( [StructureId] [uniqueidentifier] NOT NULL, [SequenceNumber] [int] NOT NULL, -- order for siblings, unique per parent [ParentStructureId] [uniqueidentifier] NULL, CONSTRAINT [Structure_PK] PRIMARY KEY CLUSTERED ( [StructureId] ASC ) ) ON [PRIMARY] ALTER TABLE [Structure] WITH CHECK ADD CONSTRAINT [Structure_FK1] FOREIGN KEY([ParentStructureId]) REFERENCES [Structure] ([StructureId]) Currently, I can get all the logical data out with the

Maximum recursion has been exhausted

南楼画角 提交于 2019-12-11 11:56:13
问题 I need an efficient way to pass in a parameter [StartingNumber] and to count from [StartingNumber] sequentially until I find a number that is missing. I use the following sql to get the next number: DECLARE @StartOffset int SET @StartOffset = 23 ; With Missing as ( select @StartOffset as N where not exists( select * from [QUEUE] where QueueNum = @StartOffset AND ismelutash = 1) ), Sequence as ( select @StartOffset as N from [QUEUE] where QueueNum = @StartOffset union all select b.QueueNum

Deleting rows recursively in a self-referencing table using a CTE. How does the process take place?

拥有回忆 提交于 2019-12-06 00:51:38
问题 I'm working on a side project, and in order to delete a row and all its descendants in a self-referencing table, I'm using a recursive CTE like this inside a trigger: CREATE TRIGGER dbo.tr_Comment_Delete ON dbo.Comment INSTEAD OF DELETE AS ;WITH IDs AS ( SELECT id FROM DELETED UNION ALL SELECT c.id FROM Comment AS c INNER JOIN IDs AS i ON c.parent_comment_id = i.id ) DELETE FROM Comment WHERE id IN (SELECT id FROM IDs); GO This is the self-referencing table Although I have this code working

Deleting rows recursively in a self-referencing table using a CTE. How does the process take place?

时间秒杀一切 提交于 2019-12-04 06:18:45
I'm working on a side project, and in order to delete a row and all its descendants in a self-referencing table, I'm using a recursive CTE like this inside a trigger: CREATE TRIGGER dbo.tr_Comment_Delete ON dbo.Comment INSTEAD OF DELETE AS ;WITH IDs AS ( SELECT id FROM DELETED UNION ALL SELECT c.id FROM Comment AS c INNER JOIN IDs AS i ON c.parent_comment_id = i.id ) DELETE FROM Comment WHERE id IN (SELECT id FROM IDs); GO This is the self-referencing table Although I have this code working as expected, it is one of those cases in which you do something, but you're not quite sure how it works.

Printing tree with SQL CTE

泄露秘密 提交于 2019-11-29 13:35:46
The schema is as follows: CREATE TABLE [Structure]( [StructureId] [uniqueidentifier] NOT NULL, [SequenceNumber] [int] NOT NULL, -- order for siblings, unique per parent [ParentStructureId] [uniqueidentifier] NULL, CONSTRAINT [Structure_PK] PRIMARY KEY CLUSTERED ( [StructureId] ASC ) ) ON [PRIMARY] ALTER TABLE [Structure] WITH CHECK ADD CONSTRAINT [Structure_FK1] FOREIGN KEY([ParentStructureId]) REFERENCES [Structure] ([StructureId]) Currently, I can get all the logical data out with the follow CTE, but I would like to print it directly in a depth first fashion. WITH SCTE (StructureId, Level,