1、查看当前所有对象
SQL=========> select * from tab TNAME (名称) TABTYPE (table|view)
2、建一个和a表结构一样的空表
SQL > create table b as select * from a where 1 = 2 ; /*复制表结构但是不复制表中数据*/
create table B as select * from A where 1=1; /*复制表结构并copy数据*/
SQL > create table b(b1,b2,b3) as select a1,a2,a3 from a where 1 = 2 ;
3、显示当前连接用户
SQL> show user
4. 单独导表操作:
D:\>exp user/pwd@db file=datazy.dmp tables=(TBL_TRAIN_YEARCENSOR,TBL_TRAIN_EXAMMSG,TBL_TRAIN_ARCHIVES),
5.创建oracle触发器
ORACLE产生数据库触发器的语法为:
create [or replace] trigger 触发器名 触发时间 触发事件
on 表名
[for each row]
其中:
触发器名:触发器对象的名称。由于触发器是数据库自动执行的,因此该名称只是一个名称,没有实质的用途。
触发时间:指明触发器何时执行,该值可取:
before---表示在数据库动作之前触发器执行;
after---表示在数据库动作之后出发器执行。
触发事件:指明哪些数据库动作会触发此触发器:
insert:数据库插入会触发此触发器;
update:数据库修改会触发此触发器;
delete:数据库删除会触发此触发器。
表 名:数据库触发器所在的表。
for each row:对表的每一行触发器执行一次。如果没有这一选项,则只对整个表执行一次。
举例:下面的触发器在更新表auths之前触发,目的是不允许在周末修改表:
create trigger auth_secure
before insert or update or delete //对整表更新前触发
on auths
begin
if(to_char(sysdate,'DY')='SUN'
RAISE_APPLICATION_ERROR(-20600,'不能在周末修改表auths');
end if;
end
下面的列子是在数据库加入数据的时候自动增加ID序列
create or replace trigger EPP_INNER_AUDIT_TID
before insert
on EPP_INNER_AUDIT_TABLE for each row
declare
id_seq varchar2(20);
begin
select trim(to_char(EPP_SEQ_INNER_AUDIT_ID.nextval ,'0999999')) into id_seq from dual;
:new.audit_id := id_seq;
:new.operate_date := sysdate;
end;
6.创建oracle存储过程
oracle创建存储过程的语法为:
CREATE OR REPLACE PROCEDURE 存储过程名字
(
参数1 IN NUMBER,
参数2 IN NUMBER
) IS
变量1 INTEGER :=0;
变量2 DATE;
BEGIN
END 存储过程名字
SELECT INTO STATEMENT
将select查询的结果存入到变量中,可以同时将多个列存储多个变量中,必须有一条
记录,否则抛出异常(如果没有记录抛出NO_DATA_FOUND)
BEGIN
SELECT col1,col2 into 变量1,变量2 FROM typestruct where xxx;
EXCEPTION
WHEN NO_DATA_FOUND THEN
xxxx;
END;
IF 判断
IF V_TEST=1 THEN
BEGIN
do something
END;
END IF;
while 循环
WHILE V_TEST=1 LOOP
BEGIN
XXXX
END;
END LOOP;
变量赋值
V_TEST := 123;
举例:
添加新用户存储过程AddUser
CREATE Procedure AddUser
(
@UserName nvarchar(50),
@Password nvarchar(50),
@Name nvarchar(50),
@Email nvarchar(50),
@Sex nvarchar(50),
@IDCardNumber nvarchar(50),
@Telephone nvarchar(50),
@Address nvarchar(50),
@Zipcode nvarchar(50),
@Problem nvarchar(50),
@Answer nvarchar(50),
/**//**//**//* @Integral float(8), */
@UserID int OUTPUT
)
AS
/**//**//**//*检查是否存在相同的userName*/
if(not exists(select * from UserInfo where UserName=@UserName))
BEGIN
/**//**//**//*不存在则插入新记录,否则返回-1*/
INSERT INTO UserInfo(UserName,Password,Name,Email,Sex,IDCardNumber,
Telephone,Address,Zipcode,Problem,Answer)
VALUES(@UserName,@Password,@Name,@Email,
@Sex,@IDCardNumber,@Telephone,@Address,
@Zipcode,@Problem,@Answer)
SELECT @UserID=@@identity
END
ELSE BEGIN
SET @UserID=-1
END
GO
定期执行的存储过程
create or replace procedure checkpoint_pro
(
p_registrar_id in VARCHAR2, -- 注册商id
p_operator_id in VARCHAR2, -- 清算操作人员id
p_checkDate in varchar2, -- 要进行清算的数据的日期
p_result out varchar2 -- 清算返回结果
)
is
v_deal_id varchar2(7);
v_Money NUMBER := 0; -- 清算的费用
v_Count number := 0; -- 清算的记录数
v_last_expend_id number := 0;
pre_available_fee number := 0; -- 清算前余额
post_available_fee number := 0; -- 清算后余额
last_check_date date ; --最后一次清算日期
dealdate varchar2(20); --循环清算中某一次的清算日期
is_second varchar2(7); --是否是二次清算
begin
p_result := 'true';
is_second := 'false';
--查找注册商最后一次清算日期
select max(DEAL_DATE) into last_check_date
from rn_registry.rn_day_deal_table where registrar_id = p_registrar_id;
--如果最后一次清算日期等于当前要清算的日期,则标记为二次清算
if last_check_date is not null and to_char(last_check_date,'yyyy-mm-dd') = substr(p_checkDate,0,10) then
is_second := 'true';
end if;
--从最后一次清算日期开始按天循环清算,直到等于当前要清算的日期
while to_char(last_check_date,'yyyy-mm-dd') < substr(p_checkDate,0,10) or is_second = 'true' or last_check_date is null loop
--取得本次循环清算的清算日期
if is_second = 'true' or last_check_date is null then
dealdate := p_checkDate;
is_second := 'false';
else
dealdate := to_char(last_check_date+1,'yyyy-mm-dd')||' 00:00';
end if;
begin
savepoint DOMAIN_CHECK;
-- 给处理过的数据加标记,保证数据在下次计算时不会被第二次计算
select trim(to_char(rn_registry.RN_SEQ_DEAL_ID.nextval,'0999999')) into v_deal_id from dual;
update rn_registry.rn_renew_log_table
set check_flag = 'true',deal_id = v_deal_id
where registrar_id = p_registrar_id
and to_char(renew_date,'yyyy-mm-dd hh24:mi') <dealdate
and check_flag = 'false';
-- 计算要清算的费用总数
select count(*), nvl(sum(price * renew_year),0) into v_Count, v_Money
from rn_registry.rn_renew_log_table
where registrar_id = p_registrar_id
and deal_id = v_deal_id;
select avaliable_fee into pre_available_fee from rn_registry.rn_registrar_day_fee_table where registrar_id=p_registrar_id;
post_available_fee := pre_available_fee - v_Money;
--判断余额是否能够支付本次清算
--if post_available_fee < 0 then
-- p_result := 'false';
-- rollback to savepoint DOMAIN_CHECK;
-- goto end_one_check;
--end if;
-- 更新注册端余额
update rn_registry.rn_registrar_day_fee_table set avaliable_fee = avaliable_fee - v_Money where registrar_id=p_registrar_id;
-- 向注册端每日清算表中插入清算结果
insert into rn_registry.rn_day_deal_table (deal_id,deal_date,registrar_id,expendition,count)
values(v_deal_id, to_date(dealdate,'yyyy-mm-dd hh24:mi'), p_registrar_id, v_Money, v_Count);
-- 向费用端每日消费表中插入消费结果
insert into rn_registrar_expend_table (deal_id,operator_id,deal_date,amount,pre_available_fee,post_available_fee,expend_id,registrar_id)
values(v_deal_id, p_operator_id, to_date(dealdate,'yyyy-mm-dd hh24:mi'), v_Money, pre_available_fee, post_available_fee, null, p_registrar_id);
-- 更新费用端余额
select expend_id into v_last_expend_id from rn_registrar_expend_table where deal_id = v_deal_id and registrar_id = p_registrar_id;
update rn_registrar_fee_table set available_fee = available_fee - v_Money,last_expend_id = v_last_expend_id where registrar_id=p_registrar_id;
commit;
<<end_one_check>>
null;
exception
when others then
p_result := 'false';
rollback to savepoint DOMAIN_CHECK;
end;
exit when p_result = 'false';
--查找注册商最后一次清算日期
select max(DEAL_DATE) into last_check_date
from rn_registry.rn_day_deal_table where registrar_id = p_registrar_id;
end loop;
exception
when others then
p_result := 'false';
end checkpoint_pro;
7.查询当前用户权限
select * from session_privs;
|