I have a stored procedure that receives two comma separated lists, parsing them and inserting into a temp table. Selecting from the temp table shows what I am working with:
What I need to do is update my "main_Page" table with the MenuOrder resquenced as per the ParentID. For example the bove would look like the following, which I could then use to update based on a join of PageID: (* denotes a change from the preceding result set)
Any ideas on how to accomplish this? I am stumped. I included what I have so far below.
Thanks,
Mike
My Proceedure call is sown below and the procedure so far follows:
Page_UpdateNavMenuOrder '1,6,1,1,4,4,4,17,17,17,4,4,1,2,2,2,2,1,5', '6,24,3,4,15,16,17,20,21,22,18,19,2,11,12,13,14,5,23'
Code:
ParentID PageID MenuOrder
----------- ----------- -----------
1 6 5
1 3 2
1 4 3
1 2 1
1 5 4
2 11 1
2 12 2
2 13 3
2 14 4
4 15 1
4 16 2
4 17 3
4 18 4
4 19 5
Code:
ParentID PageID MenuOrder
----------- ----------- -----------
1 6 1 *
1 3 2
1 4 3
1 2 4 *
1 5 5 *
2 11 1
2 12 2
2 13 3
2 14 4
4 15 1
4 16 2
4 17 3
4 18 4
4 19 5
Thanks,
Mike
My Proceedure call is sown below and the procedure so far follows:
Page_UpdateNavMenuOrder '1,6,1,1,4,4,4,17,17,17,4,4,1,2,2,2,2,1,5', '6,24,3,4,15,16,17,20,21,22,18,19,2,11,12,13,14,5,23'
Code:
ALTER PROCEDURE dbo.Page_UpdateNavMenuOrder
@ParentIDsList varchar(1000)
,@PageIDsList varchar(1000)
AS
SET NOCOUNT ON
---- Phase 1: parse the character delimeted strings
DECLARE
@ParentID int
,@PageID int
,@EndMarker int
,@Counter int
CREATE TABLE #tmpMenuItems (
ParentID int
,PageID int
,InsertOrder int
)
SET @Counter = 1
SET @EndMarker = CHARINDEX(',', @ParentIDsList)
WHILE (@EndMarker > 0)
BEGIN
SET @ParentID = SUBSTRING(@ParentIDsList, 1, @EndMarker - 1)
SET @ParentIDsList = SUBSTRING(@ParentIDsList, @EndMarker + 1, LEN(@ParentIDsList))
SET @EndMarker = CHARINDEX(',', @PageIDsList)
IF (@EndMarker > 0)
BEGIN
SET @PageID = SUBSTRING(@PageIDsList, 1, @EndMarker - 1)
SET @PageIDsList = SUBSTRING(@PageIDsList, @EndMarker + 1, LEN(@PageIDsList))
END
ELSE
BEGIN
SELECT 'FAIL' as [Status],
'Missing PageID for ParentID ' + STR(@ParentID) as [Reason]
RETURN
END
INSERT #tmpMenuItems (
ParentID
,PageID
,InsertOrder
)
VALUES (
@ParentID
,@PageID
,@Counter
)
SET @EndMarker = CHARINDEX(',', @ParentIDsList)
SET @Counter = @Counter + 1
END
IF CHARINDEX(',', @PageIDsList ) > 0
BEGIN
SELECT 'FAIL' as [Status],
'Unbalanced lists - PageID = ' + STR(@PageIDsList) + ' - ParentID = ' + STR(@ParentID) as [Reason]
RETURN
END
INSERT #tmpMenuItems (
ParentID
,PageID
,InsertOrder
)
VALUES (
@ParentIDsList
,@PageIDsList
,@Counter
)
SELECT
t.ParentID
,t.PageID
,MenuOrder
FROM
#tmpMenuItems t
JOIN main_Page p ON t.PageID = p.PageID
ORDER BY
t.ParentID
,t.InsertOrder
-- resequence and update here...
DROP TABLE #tmpMenuItems