CREATE TABLE ORDERMASTER
(
ORDERNO VARCHAR2 (30) NOT NULL,
MODELCD NUMBER NOT NULL,
ORDERQTY NUMBER NOT NULL,
ORDERDT DATE,
CUSTOMERID VARCHAR2 (30)
)
ALTER TABLE SCOTT.ORDERMASTER ADD(
CONSTRAINT PK_ORDERMASTER PRIMARY KEY (ORDERNO));
insert into OrderMaster (orderno,MODELCD,orderqty,orderdt) values('20200626K001',1,1,'20200626');
insert into OrderMaster (orderno,MODELCD,orderqty,orderdt) values('20200627K001',1,1,'20200627');
insert into OrderMaster (orderno,MODELCD,orderqty,orderdt) values('202006260001',1,1,'20200626');
insert into OrderMaster (orderno,MODELCD,orderqty,orderdt) values('202006260002',1,1,'20200626');
insert into OrderMaster (orderno,MODELCD,orderqty,orderdt) values('202006270001',1,1,'20200627');
SELECT *
FROM OrderMaster;
DELETE FROM ORDERMASTER;
--20200627K001
SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') || 'K' || LPAD(NVL(MAX(SUBSTR(ORDERNO, 10)),0)+1,3,'00')
FROM ORDERMASTER
WHERE SUBSTR(ORDERNO, 0,8) = TO_CHAR(SYSDATE, 'YYYYMMDD');
--20200626 0001 성공
SELECT TO_NUMBER(TO_CHAR(SYSDATE, 'YYYYMMDD') || NVL(TO_CHAR(MAX(SUBSTR(ORDERNO,9))), '0000'))+1
FROM ORDERMASTER
WHERE SUBSTR(ORDERNO, 0,8) = TO_CHAR(SYSDATE, 'YYYYMMDD');
--20200626 0001 성공
SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') || LPAD(NVL(MAX(SUBSTR(ORDERNO, 10)),0)+1,4,'000')
FROM ORDERMASTER
WHERE SUBSTR(ORDERNO, 0,8) = TO_CHAR(SYSDATE, 'YYYYMMDD');
'DB > Oracle' 카테고리의 다른 글
오라클 MERGE INTO 문으로 있으면 UPDATE 없으면 INSERT 한번에 수행하기 (0) | 2023.08.31 |
---|---|
[Oracle SQL] 오라클 외래 키(FK) 정의와 사용법 알아보기. (0) | 2023.08.27 |
강좌 테스트용 scott user 생성 스크립트 (0) | 2023.08.24 |
대용량 데이터베이스 스터디 (0) | 2023.07.16 |
오라클 전문가로 가는 지름길_이채남[SQL] (0) | 2023.07.06 |