程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> Oracle常用語句

Oracle常用語句

編輯:Oracle教程

--如何用grade表的資料去更新usertable表的資料(有關聯的字段userid)
update usertable u set u.grade =
(select g.grade from grade g where g.userid = u.userid);

--如何使查詢結果字段生成序號
select rownum, t.* from sm_t_pad_new t

--如何快速做一個和原表一樣的備份表
create table new_table as (select * from user);

--如何查看數據文件的存放路徑
select tablespace_name, file_id, bytes/1024/1024, file_name
from dba_data_files order by file_id;

--查詢姓名相同的員工的信息
select u1.userid, u1.username from user u1,
(select username, count(*) from user group by username having count(username) > 1) u2
where u1.username = u2.username;

--根據時間查詢

select * from user
where create_time >= to_date('2010-4-16 00:00:00','YYYY-MM-DD HH24:mi:ss')
and create_time <= to_date('2010-4-16 12:00:00','YYYY-MM-DD HH24:mi:ss')

-- 批量刪除方法一
declare
v_temp number;
begin
loop
begin
select 1 into v_temp from user
where create_time <= to_date('2010-4-16 17:35:22','YYYY-MM-DD HH24:mi:ss') and rownum < = 1;

delete from user
where create_time <= to_date('2010-4-16 17:35:22','YYYY-MM-DD HH24:mi:ss') and rownum < = 2;

commit;
exception when no_data_found then exit;
end;
end loop;
end;

-- 批量刪除方法二
declare
v_log_num number; -- 數據庫中擁有的日志文件數
v_archive number; -- 需要歸檔的日志文件數
begin
select count(1) into v_log_num from v$log;
loop
loop
select count(1) into v_archive from v$archive;
if v_archive < v_log_num - 1 then exit;
else dbms_lock.sleep(60);
end if;
end loop;
delete from user
where create_time <= to_date('2010-4-16 17:39:44','YYYY-MM-DD HH24:mi:ss') and rownum < = 2;
if sql%rowcount = 0 then exit;
end if;
commit;
end loop;
end;

--批量刪除方法三
declare
v_ids varchar2(4000);
v_id varchar(20);
v_char char;
begin
v_ids := '2121,2141';
v_char := ',';
while(length(v_ids) > 0)
loop
begin
if(instr(v_ids, v_char) > 0)
then v_id := substr(v_ids, 0, instr(v_ids, v_char) - 1);
v_ids := substr(v_ids, instr(v_ids, v_char) + 1);
else
v_id := v_ids;
v_ids := '';
end if;
delete from user where userId = v_id;
--if sql%rowcount = 0 then exit;
--end if;
dbms_output.put_line('刪除一條數據。');
end;
commit;
dbms_output.put_line(v_id);
end loop;
end;

-- 批量刪除的存儲過程
create or replace procedure batchInsert(ids in varchar2, v_char in varchar2)
as
v_ids varchar2(4000);
v_id varchar2(20);
begin
v_ids := ids;
while(length(v_ids) > 0)
loop
begin
if(instr(v_ids, v_char) > 0) -- 在ids中搜索',', 返回發現','的位置,若不存在則返回0;
then v_id := substr(v_ids, 1, instr(v_ids, v_char) - 1);
v_ids := substr(v_ids, instr(v_ids, v_char) + 1);
else
v_id := v_ids;
v_ids := '';
end if;
delete from user where userId = v_id;
--if sql%rowcount = 0 then exit;
--end if;
dbms_output.put_line('刪除一條數據。');
end;
commit;
dbms_output.put_line(v_id);
end loop;
end batchInsert;

-- 執行存儲過程
declare
v_ids varchar2(4000);
v_char varchar2(20);
begin
v_ids := '2062,2081,2101';
v_char := ',';
batchInsert(v_ids, v_char);
end;

===============================================================

CREATE OR REPLACE PROCEDURE sm_p_sendNotice(
v_noticeId in varchar2, --公告編號, 對應SM_T_NOTICE表的NOTICE_ID
v_unitId in varchar2, --機構ID
v_title in varchar2, --公告標題, 對應SM_T_NOTICE表的TITLE
v_model_id in VARCHAR2 --公告類別編號, 對應SM_T_NOTICE表的MODEL_ID
)
IS
v_notice_model_name NVARCHAR2(100);
BEGIN
SELECT NOTICE_NAME INTO v_notice_model_name FROM sm_t_notice_model WHERE NOTICE_MODEL_ID = v_model_id;
insert into sm_t_notice_newest(NOITCE_STAFF_ID, NOTICE_ID, STAFF_ID, TITLE, out_time, notice_model_name)
(select SM_S_NOITCE_NEWEST_ID.Nextval, v_noticeId, t.staff_id, v_title, sysdate, v_notice_model_name from SM_t_STAFF t
WHERE t.unit_id IN(SELECT unit_id FROM sm_t_unit START WITH unit_id = v_unitId CONNECT BY super_unit_ID = PRIOR unit_id));
END sm_p_sendNotice;
--
DECLARE
v_noticeId varchar2(4000);
v_unitId varchar2(20);
v_title VARCHAR2(500);
v_model_id NVARCHAR2(100);
BEGIN
v_noticeId := '1003';
v_unitId := '01';
v_title := '929555993應答處理口徑';
v_model_id := '28';
sm_p_sendNotice(v_noticeId, v_unitId, v_title, v_model_id);
END;
--

--任務隊列管理器

begin
sys.dbms_job.submit(job => :job,
what => 'sm_p_insertMessage;',
next_date => to_date('07-09-2010 02:30:00', 'dd-mm-yyyy hh24:mi:ss'),
interval => 'TRUNC(SYSDATE + 1) + (2*60+30)/(24*60)');
commit;
end;

--

oracle 創建表時判斷表是否存在語句

declare cnt number;

begin
---查詢要創建的表是否存在
select count(*)into cnt from user_tables where table_name='ENTRY_MODIFYSTATUS';
---如果存在則刪除該表
if cnt>0 then
dbms_output.put_line('表存在不創建');
else
dbms_output.put_line('表不存在');
execute immediate 'create table ENTRY_MODIFYSTATUS (
ENTRY_ID VARCHAR2(18) not null,
APPLY_TIME DATE not null,
STATUS NUMBER(2),
constraint PK_ENTRY_MODIFYSTATUS primary key (ENTRY_ID, APPLY_TIME)
)';
end if;

cnt:=0;
end;

==============================================

CREATE OR REPLACE PROCEDURE AP_T_RPT_PRO_LOGINCOUNT
AS
login_count NVARCHAR2(100);
week_login_count NVARCHAR2(100);
--date_time DATE;
now_time DATE;
flag CHAR(1);
BEGIN
SELECT TRUNC(SYSDATE) INTO now_time FROM dual;
--date_time := to_date('2010-6-7', 'YYYY-MM-DD');
--WHILE(date_time < now_time-6)
--date_time := date_time + 7;
--SELECT d.ANALYSIS_FLAG INTO flag FROM ap_t_rpt_login_dict d
FOR A IN (SELECT start_time FROM ap_t_rpt_login_dict WHERE start_time < now_time-6)
LOOP
BEGIN
SELECT d.ANALYSIS_FLAG INTO flag FROM ap_t_rpt_login_dict d WHERE d.start_time = A.START_TIME;
IF(flag = 'N')
THEN
FOR C IN (SELECT u1.unit_id FROM sm_t_unit u1)
LOOP
SELECT COUNT(DISTINCT t.staff_id) INTO login_count FROM SM_t_login T
WHERE t.unit_id = C.UNIT_ID
AND t.login_time > add_months(A.START_TIME + 7,-2)
AND t.login_time < A.START_TIME + 7;

SELECT COUNT(DISTINCT t.staff_id) INTO week_login_count FROM SM_t_login T
WHERE t.unit_id = C.UNIT_ID
AND t.login_time > A.START_TIME
AND t.login_time < A.START_TIME + 7;

INSERT INTO ap_t_rpt_login (rpt_login_id, unit_id, start_time, user_count, week_login_user)
VALUES(ap_s_rpt_login.nextval, C.UNIT_ID, A.START_TIME, login_count, week_login_count);

END LOOP;
UPDATE ap_t_rpt_login_dict SET ANALYSIS_FLAG = 'Y' WHERE start_time = A.START_TIME;

END IF;
END;
END LOOP;

END;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved