简体   繁体   中英

How do I loop through a SQL result set and update the records?

I have a system that is used to reorder columns. I have a function that allows the user to disable a column. This sets its column order to null in the database and sets it to disabled but I need to then be able to update the ColumnOrder column to update the remaining enabled records with a new order.

Here is what my table looks like when I have disabled the 'Cust Types' as you can see I now have 4 enabled records remaining but I need to give them new ColumnOrder numbers 1,2,3,4 (don't need 5):

ID  ColumnID    UserID  Text                    ColumnOrder Enabled?
50  22          1       'id'                    1           1
51  1           1       'Cust Types'            NULL        0
52  2           1       'Description'           2           1
53  3           1       'Apply VAT'             NULL        0
54  4           1       'Produce Invoices?'     4           1
55  5           1       'Purchase Sale'         5           1
56  6           1       'Terms Days'            NULL        0
57  7           1       'Date Time Last Updated'    NULL    0

Here is the procedure that enables or disables records:

IF EXISTS ( SELECT [Enabled?]
                FROM dbo.CustomerLocalizationColumns CLC
                INNER JOIN Portal.dbo.Columns C
                    ON CLC.ColumnID = C.ID
                WHERE UserID = @UserID
                    AND ColumnID = @ColumnID
                    AND CLC.[Enabled?] = 0 )
    BEGIN
        UPDATE dbo.CustomerLocalizationColumns
            SET [Enabled?] = 1
                ,ColumnOrder = (
                                SELECT COUNT(*) + 1
                                    FROM dbo.CustomerLocalizationColumns
                                    WHERE [Enabled?] = 1
                                )
            WHERE (
                    SELECT [Enabled?]
                        FROM dbo.CustomerLocalizationColumns CLC
                        INNER JOIN Portal.dbo.Columns C
                            ON CLC.ColumnID = C.ID
                        WHERE UserID = @UserID
                            AND ColumnID = @ColumnID
                    ) = 0
                AND dbo.CustomerLocalizationColumns.UserID = @UserID
                AND dbo.CustomerLocalizationColumns.ColumnID = @ColumnID
    END
ELSE
    BEGIN
        UPDATE dbo.CustomerLocalizationColumns
            SET [Enabled?] = 0
                ,ColumnOrder = NULL
            WHERE (
                    SELECT [Enabled?]
                        FROM dbo.CustomerLocalizationColumns CLC
                        INNER JOIN Portal.dbo.Columns C
                            ON CLC.ColumnID = C.ID
                        WHERE UserID = @UserID
                            AND ColumnID = @ColumnID
                    ) = 1
                AND dbo.CustomerLocalizationColumns.UserID = @UserID
                AND dbo.CustomerLocalizationColumns.ColumnID = @ColumnID
    END
END 

Try this.. I could implement this using ROWNUMBER() to generate the new column order for your non-null columns and then update this into your table in the same pass

    WITH CTE
        AS (
            SELECT *
                , ROW_NUMBER() OVER (
                    ORDER BY columnorder
                    ) ROWNUM
            FROM CustomerLocalizationColumns
            WHERE columnorder IS NOT NULL
           --Could also use Enabled=1
            )
        UPDATE A
        SET columnorder = rownum
        FROM CustomerLocalizationColumns A
        INNER JOIN CTE
            ON A.Columnid = CTE.Columnid
            WHERE columnorder IS NOT NULL
            --Could also use Enabled=1;

End to end sample:

DECLARE @tble TABLE (
    Columnid VARCHAR(30)
    , columnorder INT
    )
insert into @tble
SELECT 'a', 1 UNION all
SELECT 'b', 2 UNION all
SELECT 'c', NULL UNION all
SELECT 'd', 6 UNION all
SELECT 'e', NULL UNION all
SELECT 'f', 8
--BEFORE UPDATE
SELECT *
FROM @tble;

--UPDATING
WITH CTE
AS (
    SELECT *
        , ROW_NUMBER() OVER (
            ORDER BY columnorder
            ) ROWNUM
    FROM @tble
    WHERE columnorder IS NOT NULL
    )
UPDATE A
SET columnorder = rownum
FROM @tble A
INNER JOIN CTE
    ON A.Columnid = CTE.Columnid;

--AFTER UPDATE
SELECT *
FROM @tble;
UPDATE CustomerLocalizationColumns
SET ColumnOrder = new_num
FROM CustomerLocalizationColumns
  LEFT OUTER JOIN 
    (SELECT ID, ROW_NUMBER() OVER (ORDER BY ID) as new_num
     FROM CustomerLocalizationColumns
     WHERE [Enabled?]=1
    ) as enabledrows
       ON CustomerLocalizationColumns.ID=enabledrows.ID

I used this to test :

CREATE TABLE #CustomerLocalizationColumns 
(ID INT, ColumnOrder INT, [Enabled?] INT);

With Data as
(SELECT 50 as ID, 1 as ColumnOrder, 1 as [Enabled?] UNION ALL
 SELECT 51,0,0 UNION ALL
 SELECT 52,2,1 UNION ALL
 SELECT 53,0,0 UNION ALL
 SELECT 54,4,1 UNION ALL
 SELECT 55,5,1 UNION ALL
 SELECT 56,0,0 UNION ALL
 SELECT 57,0,0
)
INSERT INTO #CustomerLocalizationColumns
SELECT * FROM Data;

-- because I used 0 instead of NULLs in a hurry (and it lines up nicely!) :)
UPDATE #CustomerLocalizationColumns
SET ColumnOrder=NULL
WHERE [Enabled?] = 0

UPDATE #CustomerLocalizationColumns
SET ColumnOrder = test
FROM #CustomerLocalizationColumns
  RIGHT OUTER JOIN (SELECT ID, ROW_NUMBER() OVER (ORDER BY ID) as test FROM #CustomerLocalizationColumns
   WHERE [Enabled?]=1) as enabledrows ON #CustomerLocalizationColumns.ID=enabledrows.ID

SELECT * FROM #CustomerLocalizationColumns;
DROP TABLE #CustomerLocalizationColumns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM