我正在尝试根据行数更新sql中的列SortOrder。列名是SortOrder,ASP.net将显示列" reportdisplayname'基于排序顺序。 ' reportdisplayname'列全部分组。在下面,reportdisplayname有数据' FADMS - 测试' FADMS - 主题'。 sortorder的想法是一起显示相似的值。所以,' FADMS - 主题'将出现在FADMS - 测试'。
之后UPDATE managementreports
SET sortorder = rownum
where eventid = 'xxxxx';
UPDATE managementreports SET sortorder = 9
where eventid = 'xxx' and reportdisplayname = 'FADMS - Test';
UPDATE managementreports SET sortorder = 10
where eventid = 'xxx' and reportdisplayname = 'Results - xxx';
UPDATE managementreports
SET sortorder = sortorder + 1
where eventid = 'xxx' and sortorder > 10;
有没有更好的方法来做到这一点。目前,我为新字段设置了排序顺序' FADMS - 测试',然后将现有无关字段的排序顺序更新为1.
答案 0 :(得分:0)
是的,您可以使用CASE
表达式一次更新所有内容,如下所示:
UPDATE managementreports
SET sortorder = CASE
WHEN eventid = 'xxxxx' THEN rownum
WHEN eventid = 'xxx' AND reportdisplayname = 'FADMS - Test' THEN 9
WHEN eventid = 'xxx' AND reportdisplayname = 'Results - xxx' THEN 10
WHEN eventid = 'xxx' AND sortorder > 10 THEN (sortorder + 1)
ELSE sortorder
END