我在表priority_list中有优先级列,如下所示。
Priority Header
1 A
2 B
3 C
4 D
5 E
如果我用新的优先级编号更改优先级,那么其他优先级应该重新安排
例如 如果我把4改成3那么记录将如下
Priority Header
5 A
1 B
2 C
3 D
4 E
我怎样才能做到这一点..请帮助!!!
我尝试了将输入参数作为 I_priority、I_Case_Type 的过程
并将优先级更新为
Update priority_list
set priority= I_Priority - 1
where priority<(select priority from priority_list where priority=I_Priority);
但没有得到想要的结果
您可以使用
MERGE
语句并更新旧优先级和新优先级之间的所有行。
例如,如果要将
priority
中的 1
更改为 5
,则:
MERGE INTO table_name dst
USING (
WITH changes (old_priority, new_priority) AS (
SELECT 1, 5 FROM DUAL
)
SELECT t.rowid AS rid,
c.*
FROM table_name t
INNER JOIN changes c
ON t.priority BETWEEN LEAST(c.old_priority, c.new_priority)
AND GREATEST(c.old_priority, c.new_priority)
) src
ON (dst.ROWID = src.rid)
WHEN MATCHED THEN
UPDATE
SET priority = CASE
WHEN priority > old_priority THEN priority - 1
WHEN priority < old_priority THEN priority + 1
ELSE new_priority
END;
对于样本数据:
CREATE TABLE table_name (Priority, Header) AS
SELECT 1, 'A' FROM DUAL UNION ALL
SELECT 2, 'B' FROM DUAL UNION ALL
SELECT 3, 'C' FROM DUAL UNION ALL
SELECT 4, 'D' FROM DUAL UNION ALL
SELECT 5, 'E' FROM DUAL;
然后,在
MERGE
之后,表格包含:
优先 | 标题 |
---|---|
5 | A |
1 | B |
2 | C |
3 | D |
4 | E |