博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ORACLE常用脚本示例
阅读量:6499 次
发布时间:2019-06-24

本文共 2063 字,大约阅读时间需要 6 分钟。

create table DBO.INDEX_POLICY_TBL

(
ID NUMBER(10) NOT NULL PRIMARY KEY,
POLICY_ID NUMBER(10,0) default (-1) NOT NULL,
ALARM_COUNT NUMBER(10) default (0) NOT NULL
);

ALTER TABLE DBO.INDEX_POLICY_TBL ADD(C_NAME_1 VARCHAR2(64 CHAR) NOT NULL, C_NAME_2 VARCHAR2(64 CHAR) NOT NULL);

ALTER TABLE DBO.INDEX_POLICY_TBL RENAME COLUMN C_NAME_1 TO C_NAME_1_NEW;

ALTER TABLE DBO.INDEX_POLICY_TBL MODIFY C_NAME_2 VARCHAR2(128 CHAR);

create table DBO.INDEX_POLICY_TBL

(
ID NUMBER(10) NOT NULL PRIMARY KEY,
POLICY_ID NUMBER(10,0) default (-1) NOT NULL,
ALARM_COUNT NUMBER(10) default (0) NOT NULL
);

CREATE SEQUENCE DBO.INDEX_POLICY_TBL_SEQ INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCACHE NOCYCLE;

create or replace

TRIGGER DBO.INDEX_POLICY_TBL_ID_IDENTITY BEFORE INSERT ON DBO.INDEX_POLICY_TBL
FOR EACH ROW
DECLARE
v_newVal NUMBER(10) := 0;
v_incval NUMBER(10) := 0;
BEGIN
IF INSERTING AND :new.ID IS NULL THEN
SELECT DBO.INDEX_POLICY_TBL_SEQ.NEXTVAL INTO v_newVal FROM DUAL;
-- If this is the first time this table have been inserted into (sequence == 1)
IF v_newVal = 1 THEN
--get the max indentity value from the table
SELECT NVL(max(ID),0) INTO v_newVal FROM DBO.INDEX_POLICY_TBL;
v_newVal := v_newVal + 1;
--set the sequence to that value
LOOP
EXIT WHEN v_incval>=v_newVal;
SELECT DBO.INDEX_POLICY_TBL_SEQ.nextval INTO v_incval FROM dual;
END LOOP;
END IF;
-- save this to emulate @@identity
sqlserver_utilities.identity := v_newVal;
-- assign the value from the sequence to emulate the identity column
:new.ID := v_newVal;
END IF;
END;

INSERT INTO DBO.INDEX_POLICY_TBL(POLICY_ID, ALARM_COUNT, C_NAME_1_NEW, C_NAME_2)

VALUES(100, 1, 'A', 'B');
INSERT INTO DBO.INDEX_POLICY_TBL(POLICY_ID, ALARM_COUNT, C_NAME_1_NEW, C_NAME_2)
VALUES(101, 2, 'B', 'C');
INSERT INTO DBO.INDEX_POLICY_TBL(POLICY_ID, ALARM_COUNT, C_NAME_1_NEW, C_NAME_2)
VALUES(102, 3, 'C', 'D');
COMMIT;

SELECT * FROM DBO.INDEX_POLICY_TBL;

drop TRIGGER DBO.INDEX_POLICY_TBL_ID_IDENTITY;

drop SEQUENCE DBO.INDEX_POLICY_TBL_SEQ;
drop table DBO.INDEX_POLICY_TBL;

 

转载于:https://www.cnblogs.com/chriskwok/p/3974610.html

你可能感兴趣的文章
航电1012 u Calculate e
查看>>
netty支持SSL,OpenSSL
查看>>
Yii简单的基于角色的访问控制
查看>>
POJ-1860-Currency Exchange
查看>>
跨越企业的“中等收入陷阱”
查看>>
Android 开发者必知的开发资源
查看>>
jackson 常见问题
查看>>
软件工程技术基础-(软件复用技术)
查看>>
给django视图类添加装饰器
查看>>
对javscript中Object.defineProperty的理解
查看>>
.vimrc文件
查看>>
DVWA默认用户名密码
查看>>
简述 clearfix 的原理
查看>>
swift轮播图代码
查看>>
Entity Framework 约定
查看>>
构建现代化的命令行工具
查看>>
【Project Euler】530 GCD of Divisors 莫比乌斯反演
查看>>
js 使用 Lawnchair 存储 json 对象到本地
查看>>
还有这种操作?
查看>>
计算机基础第二课时
查看>>