在 Oracle 数据库上使用增量更新列的行值
Update row value for column with increment on Oracle Database
我正在尝试编写一个包含 Oracle 数据库更新的存储过程,我希望 'labelcount' 递增 1。
MEMBER_NUM字段不允许出现两次相同的值。
有什么建议吗?
labelcount:= 1
IF (major_count >=1 ) and (min_count >=1 ) then
UPDATE TBL_MEMBERS
SET MEMBER_NUM = Major || 'M' || labelcount
WHERE MEMBER_ID = Minor and MEMBER_NUM = 'XXX';
End If;
所以这个:
Member_ID | Member_NUM |
A | xxx |
A | 4444 |
A | xxx |
A | xxx |
变成
Member_ID | Member_NUM |
A | AM1 |
A | 4444 |
A | AM2 |
A | AM3 |
你不是很准确,所以回答也不一定准确
为避免在 MEMBER_NUM
列中出现重复,请在其上使用唯一索引。
因此,如果增加的数字已经存在于数据库中,则更新将失败。
最好对 minor
和 lablecount
使用显式列(不是字符串连接)
由于 Member_Num 列中的“4444”值,我不太确定我是否理解您的要求,但我会试一试:
DDL:
Create Table TBL_MEMBERS
(
Member_ID number
, Member_NUM varchar2(100)
);
create sequence labelcount
start with 1;
CREATE or replace PROCEDURE INC_LABEL_COUNT (P_major_count NUMBER, p_min_count NUMBER)
AS
L_STAGE VARCHAR2(100);
BEGIN
L_STAGE := 'Checking for Count Values';
IF (p_major_count >=1 ) and (p_min_count >=1 ) then
UPDATE TBL_MEMBERS
SET MEMBER_NUM = 'Major' || 'M' || labelcount.nextval
WHERE MEMBER_ID = 'Minor'
and MEMBER_NUM = 'XXX';
End If;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error in Stage: '||L_STAGE||' '||sqlerrm);
END INC_LABEL_COUNT;
我只能说这么多,因为这是我唯一的信息。
你能提供更多吗?
首先构建示例的一些行
-- Example Table
CREATE TABLE TBL_MEMBERS
(
MEMBER_ID CHAR(1),
MEMBER_NUM CHAR(5)
)
/
-- Example Data
delete from TBL_MEMBERS;
INSERT INTO TBL_MEMBERS (
SELECT 'A','xxx' FROM DUAL UNION ALL
SELECT 'A','xxx' FROM DUAL UNION ALL
SELECT 'A','xxx' FROM DUAL UNION ALL
SELECT 'A','1234' FROM DUAL UNION ALL
SELECT 'A','xxx' FROM DUAL UNION ALL
SELECT 'A','5678' FROM DUAL
)
/
数据:
MEMBER_ID|MEMBER_NUM
A |xxx
A |xxx
A |xxx
A |1234
A |xxx
A |5678
-- This is the way you should use
--DROP sequence AI_MEMBERS;
CREATE SEQUENCE AI_MEMBERS;
-- Here you can try it out, but attention
-- each select increases the number
SELECT AI_MEMBERS.NEXTVAL FROM DUAL;
-- Your update (without your minor/major ..., cause it is unimportant for explanation
UPDATE TBL_MEMBERS
SET MEMBER_NUM = 'A' || 'M' || AI_MEMBERS.NEXTVAL
WHERE MEMBER_ID = 'A' and MEMBER_NUM = 'XXX'
;
结果:
MEMBER_ID|MEMBER_NUM
A |AM1
A |AM2
A |AM3
A |1234
A |AM4
A |5678
我正在尝试编写一个包含 Oracle 数据库更新的存储过程,我希望 'labelcount' 递增 1。
MEMBER_NUM字段不允许出现两次相同的值。
有什么建议吗?
labelcount:= 1
IF (major_count >=1 ) and (min_count >=1 ) then
UPDATE TBL_MEMBERS
SET MEMBER_NUM = Major || 'M' || labelcount
WHERE MEMBER_ID = Minor and MEMBER_NUM = 'XXX';
End If;
所以这个:
Member_ID | Member_NUM |
A | xxx |
A | 4444 |
A | xxx |
A | xxx |
变成
Member_ID | Member_NUM |
A | AM1 |
A | 4444 |
A | AM2 |
A | AM3 |
你不是很准确,所以回答也不一定准确
为避免在 MEMBER_NUM
列中出现重复,请在其上使用唯一索引。
因此,如果增加的数字已经存在于数据库中,则更新将失败。
最好对 minor
和 lablecount
使用显式列(不是字符串连接)
由于 Member_Num 列中的“4444”值,我不太确定我是否理解您的要求,但我会试一试:
DDL:
Create Table TBL_MEMBERS
(
Member_ID number
, Member_NUM varchar2(100)
);
create sequence labelcount
start with 1;
CREATE or replace PROCEDURE INC_LABEL_COUNT (P_major_count NUMBER, p_min_count NUMBER)
AS
L_STAGE VARCHAR2(100);
BEGIN
L_STAGE := 'Checking for Count Values';
IF (p_major_count >=1 ) and (p_min_count >=1 ) then
UPDATE TBL_MEMBERS
SET MEMBER_NUM = 'Major' || 'M' || labelcount.nextval
WHERE MEMBER_ID = 'Minor'
and MEMBER_NUM = 'XXX';
End If;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error in Stage: '||L_STAGE||' '||sqlerrm);
END INC_LABEL_COUNT;
我只能说这么多,因为这是我唯一的信息。
你能提供更多吗?
首先构建示例的一些行
-- Example Table
CREATE TABLE TBL_MEMBERS
(
MEMBER_ID CHAR(1),
MEMBER_NUM CHAR(5)
)
/
-- Example Data
delete from TBL_MEMBERS;
INSERT INTO TBL_MEMBERS (
SELECT 'A','xxx' FROM DUAL UNION ALL
SELECT 'A','xxx' FROM DUAL UNION ALL
SELECT 'A','xxx' FROM DUAL UNION ALL
SELECT 'A','1234' FROM DUAL UNION ALL
SELECT 'A','xxx' FROM DUAL UNION ALL
SELECT 'A','5678' FROM DUAL
)
/
数据:
MEMBER_ID|MEMBER_NUM
A |xxx
A |xxx
A |xxx
A |1234
A |xxx
A |5678
-- This is the way you should use
--DROP sequence AI_MEMBERS;
CREATE SEQUENCE AI_MEMBERS;
-- Here you can try it out, but attention
-- each select increases the number
SELECT AI_MEMBERS.NEXTVAL FROM DUAL;
-- Your update (without your minor/major ..., cause it is unimportant for explanation
UPDATE TBL_MEMBERS
SET MEMBER_NUM = 'A' || 'M' || AI_MEMBERS.NEXTVAL
WHERE MEMBER_ID = 'A' and MEMBER_NUM = 'XXX'
;
结果:
MEMBER_ID|MEMBER_NUM
A |AM1
A |AM2
A |AM3
A |1234
A |AM4
A |5678