Oracle数据库SQL总结
1Oracle时间段的查询
1.1 场景:根据用户输入的时间段过滤出相应记录。
1.2 解决办法:
第一种写法:
Sql代码
SELECT * FROM T_XJXX_XJGL
WHERE CREATEDATE >= TO_DATE(2011-6-13, yyyy-MM-dd)
AND CREATEDATE <= TO_DATE(2011-6-17, yyyy-MM-dd);
SELECT * FROM T_XJXX_XJGL
WHERE CREATEDATE >= TO_DATE(2011-6-13, yyyy-MM-dd)
AND CREATEDATE <= TO_DATE(2011-6-17, yyyy-MM-dd);
第二种写法:
Sql代码
SELECT * FROM T_XJXX_XJGL
WHERE TO_CHAR(CREATEDATE, yyyy-MM-dd) >= 2011-6-13
AND TO_CHAR(CREATEDATE, yyyy-MM-dd) <= 2011-6-16;
SELECT * FROM T_XJXX_XJGL
WHERE TO_CHAR(CREATEDATE, yyyy-MM-dd) >= 2011-6-13
AND TO_CHAR(CREATEDATE, yyyy-MM-dd) <= 2011-6-16;
第三种写法:
Sql代码
SELECT * FROM T_XJXX_XJGL
WHERE CREATEDATE > TO_DATE(2011-6-15, yyyy-MM-dd) - 1
AND CREATEDATE <= TO_DATE(2011-6-16, yyyy-MM-dd) + 1;
SELECT * FROM T_XJXX_XJGL
WHERE CREATEDATE > TO_DATE(2011-6-15, yyyy-MM-dd) - 1
AND CREATEDATE <= TO_DATE(2011-6-16, yyyy-MM-dd) + 1;
取出当前时间在开始时间和结束时间范围内的记录:
Sql代码
SELECT * FROM T_XJXX_XJGL
WHERE 1 = 1
AND TO_CHAR(KSSJ, yyyy-mm-dd) <= TO_CHAR(SYSDATE, yyyy-mm-dd)
AND TO_CHAR(JSSJ, yyyy-mm-dd) >= TO_CHAR(SYSDATE, yyyy-mm-dd)
SELECT * FROM T_XJXX_XJGL
WHERE 1 = 1
AND TO_CHAR(KSSJ, yyyy-mm-dd) <= TO_CHAR(SYSDATE, yyyy-mm-dd)
AND TO_CHAR(JSSJ, yyyy-mm-dd) >= TO_CHAR(SYSDATE, yyyy-mm-dd)
2 Oracle创建触发器的例子
2.1场景:创建T_XJXX_XJGL中BH字段为自增长类型,start by 1 increment by 1
2.2解决方法:BH字段的类型设置为number,创建Sequence
Sql代码
CREATE SEQUENCE SEQ_XJXX_BH
MINVALUE 1 MAXVALUE 999999
INCREMENT BY 1 START WITH 11
CACHE 10 NOORDER NOCYCLE ;
CREATE SEQUENCE SEQ_XJXX_BH
MINVALUE 1 MAXVALUE 999999
INCREMENT BY 1 START WITH 11
CACHE 10 NOORDER NOCYCLE ; 创建触发器:
Sql代码
CREATE OR REPLACE TRIGGER TR_ADDID
BEFORE INSERT ON T_XJXX_XJGL
FOR EACH ROW
BEGIN
IF (:NEW.BH IS NULL) THEN
SELECT SEQ_XJXX_BH.NEXTVAL INTO :NEW.BH FROM DUAL;
END IF;
END;
CREATE OR REPLACE TRIGGER TR_ADDID
BEFORE INSERT ON T_XJXX_XJGL
FOR EACH ROW
BEGIN
IF (:NEW.BH IS NULL) THEN
SELECT SEQ_XJXX_BH.NEXTVAL INTO :NEW.BH FROM DUAL;
END IF;
相关新闻>>
- 发表评论
-
- 最新评论 更多>>