更新Oracle数据库上具有增量的列的行值

时间:2016-11-10 16:05:48

标签: sql oracle row auto-increment

我正在尝试编写一个带有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       |

3 个答案:

答案 0 :(得分:1)

你不是很精确,所以答案也不算精确

为避免MEMBER_NUM列中的重复,请在其上使用唯一索引。

如果数据库中已存在增加的数字,则更新将失败。

最好使用minorlablecount的explicite列(不是字符串连接)

答案 1 :(得分:1)

由于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;    

这是我可以去的,因为这是我唯一的信息。

你能提供更多吗?

答案 2 :(得分:1)

首先构建示例的一些行

// code
console.log(this.airport);
console.log(this.airport[0]);
console.log(this.airport[0][0]);
console.log(this.airport[0][0]['airportCode']);
console.log(typeof this.airport[0][0]);

// output
Array [ Array[51], Array[47] ]
Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, 41 more… ]
Object { airlineCode: "JT*", airlineLogoUrlPng: "http://d3brl4nqahsb3e.cloudfront.ne…", airlineLogoUrlSvg: "http://d3brl4nqahsb3e.cloudfront.ne…", airlineName: "Lion Air", airportCode: "BPN", airportName: "Sultan Aji Muhamad Sulaiman Airport", city: "Balikpapan", currentDate: "11/04/2016", currentGateDate: "11/04/2016", currentGateTime: "19:05", 31 more… }
BPN
Object

数据:

-- 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'
;