oracle小记
-- PL/SQL程序结构 以块为单位
/* 声明部分,以declare开头,声明变量方式从右向左 给变量赋值用:=*/
declare
v_id integer;
v_name varchar(20);
cursor c_emp is select * from employee where emp_id=3;
/*游标的定义
declare cursor_name cursor
for selectStatement
[for{read only\ update[of column]}]
*/
/*执行部分以begin开头*/
begin
open c_emp; --打开游标
loop
fetch c_emp into v_id,v_name; --从游标开始取数据
exit when c_emp% notfound; --游标属性 sql%found ,sql%notfound , sql% rowcount
end loop;
close c_emp; --关闭游标
dbms_output.put_line(v_name);
/*异常处理部分,以exception开始*/
exception
when no_data_found then
dbms_output.put_line('没有数据');
end
/*控件结构 -- 条件结构*/
if condition then
statement
else
statement
end if
/*控件结构 -- 循环结构*/
loop ... end loop;
while condition loop ... end loop;
for variable in low_bound ... upper_bound loop ... end loop;
/*SQL语言分类*/
/*
数据定义语言DDL Create Drop Grant Revoke
数据操纵语言DML Update Insert Delete
数据控制语言DCL Commit Rollback Savapoint
其它 Alter System Connect Allocate
*/
/*过程与函数*/
create or replace procedure test_procedure as
v_f1 number := 1; --声明变量并赋值
v_f2 number := 2;
v_f3 varchar(20) := 'first';
v_f4 varchar(20) := 'second';
begin
insert into t1 values(v_f1,v_f2);
insert into t1 values(v_f3,v_f4);
end test_procedure; --test_procedure可以省略
/*游标*/
cursor cursor_name is Sql语句;
--使用游标必须有三步:打开游标,使用fetch语句将游标里面的数据取出,关闭游标
/*触发器是一种特殊的存储过程,当发生特定的事件才被调用,主要用于多表之间的消息通知书*/
/*select 用法*/
select [name], xtype,case when xtype='U' then '用户表' else case when xtype='S' then '系统表' end end as 类型 from sysobjects
/*union 与union all的区别*/
--若无ALL参数将过滤相同的记录
select A from table1 union select A from table2;
select A from table1 union all select A from table2;
/*与into联用*/
--将A表的指定数据存入B表中
select ... into b from a;
--创建新表
select * into new_tableName from a where 1<>1;
select top 0 * into new_tableName from a;
--将查询结果保存
select field1, field2 into result from table1 where id> 1000;
--创建新表并在新表中加入自动序号
select IDENTITY(int, 1,1) as autoid,* into new_tableName from table1;
--identity函数说明
--identity(datatype,seed,increment) 数据类型,开始数,增量
--identity可以用在创建表时,修改表时,
create table table1 (
autoid int identity, --或者AutoId int IDENTITY(1,1)
field1 nvarchar(20),
field2 nvarchar(20)
)
/*INNER JOIN 只显示两表一一对应的记录*/
select * from table1 inner join table2 on table1.id = table2.id order by table1.id;
/*LEFT JOIN 显示左表所有记录与右表对应左表的记录,当右表无记录,右表相应字段用NULL填充*/
select * from table1 left join table2 on table1.id = table2.id order by table1.id;
/*RIGHT JOIN 显示右表所有记录与左表对应右表的记录*/
select * from table1 right join table2 on table1.id = table2.id order by table1.id;
/*FULL JOIN 显示左右两表所有记录*/
select * from table1 full join table2 on table1.id = table2.id order by table1.id;
/*GROUP BY*/
select A,B,Sum(D) from table1 group by a,b order by a;
--同一查询中所有未进行聚合操作的字段都需要被GROUP BY
/*对聚合结果进行筛选用having*/
select A,b,Sum(d) from table1 group by a,b having count(*) >2;
/*变量定义
用户变量用@打头,系统变量用@@打头
declare @i int
set @i =0;
在SQL语句中对变量的赋值用SET 或者SELECT */
游标的定义,将查询结果返回为游标类型
declare cursor cursor_name
for select name from sysobjects
open cursor_name
fetch next from cursor_name into @i;
close cursor_name
删除游标 deallocate cursor_name;
/* 声明部分,以declare开头,声明变量方式从右向左 给变量赋值用:=*/
declare
v_id integer;
v_name varchar(20);
cursor c_emp is select * from employee where emp_id=3;
/*游标的定义
declare cursor_name cursor
for selectStatement
[for{read only\ update[of column]}]
*/
/*执行部分以begin开头*/
begin
open c_emp; --打开游标
loop
fetch c_emp into v_id,v_name; --从游标开始取数据
exit when c_emp% notfound; --游标属性 sql%found ,sql%notfound , sql% rowcount
end loop;
close c_emp; --关闭游标
dbms_output.put_line(v_name);
/*异常处理部分,以exception开始*/
exception
when no_data_found then
dbms_output.put_line('没有数据');
end
/*控件结构 -- 条件结构*/
if condition then
statement
else
statement
end if
/*控件结构 -- 循环结构*/
loop ... end loop;
while condition loop ... end loop;
for variable in low_bound ... upper_bound loop ... end loop;
/*SQL语言分类*/
/*
数据定义语言DDL Create Drop Grant Revoke
数据操纵语言DML Update Insert Delete
数据控制语言DCL Commit Rollback Savapoint
其它 Alter System Connect Allocate
*/
/*过程与函数*/
create or replace procedure test_procedure as
v_f1 number := 1; --声明变量并赋值
v_f2 number := 2;
v_f3 varchar(20) := 'first';
v_f4 varchar(20) := 'second';
begin
insert into t1 values(v_f1,v_f2);
insert into t1 values(v_f3,v_f4);
end test_procedure; --test_procedure可以省略
/*游标*/
cursor cursor_name is Sql语句;
--使用游标必须有三步:打开游标,使用fetch语句将游标里面的数据取出,关闭游标
/*触发器是一种特殊的存储过程,当发生特定的事件才被调用,主要用于多表之间的消息通知书*/
/*select 用法*/
select [name], xtype,case when xtype='U' then '用户表' else case when xtype='S' then '系统表' end end as 类型 from sysobjects
/*union 与union all的区别*/
--若无ALL参数将过滤相同的记录
select A from table1 union select A from table2;
select A from table1 union all select A from table2;
/*与into联用*/
--将A表的指定数据存入B表中
select ... into b from a;
--创建新表
select * into new_tableName from a where 1<>1;
select top 0 * into new_tableName from a;
--将查询结果保存
select field1, field2 into result from table1 where id> 1000;
--创建新表并在新表中加入自动序号
select IDENTITY(int, 1,1) as autoid,* into new_tableName from table1;
--identity函数说明
--identity(datatype,seed,increment) 数据类型,开始数,增量
--identity可以用在创建表时,修改表时,
create table table1 (
autoid int identity, --或者AutoId int IDENTITY(1,1)
field1 nvarchar(20),
field2 nvarchar(20)
)
/*INNER JOIN 只显示两表一一对应的记录*/
select * from table1 inner join table2 on table1.id = table2.id order by table1.id;
/*LEFT JOIN 显示左表所有记录与右表对应左表的记录,当右表无记录,右表相应字段用NULL填充*/
select * from table1 left join table2 on table1.id = table2.id order by table1.id;
/*RIGHT JOIN 显示右表所有记录与左表对应右表的记录*/
select * from table1 right join table2 on table1.id = table2.id order by table1.id;
/*FULL JOIN 显示左右两表所有记录*/
select * from table1 full join table2 on table1.id = table2.id order by table1.id;
/*GROUP BY*/
select A,B,Sum(D) from table1 group by a,b order by a;
--同一查询中所有未进行聚合操作的字段都需要被GROUP BY
/*对聚合结果进行筛选用having*/
select A,b,Sum(d) from table1 group by a,b having count(*) >2;
/*变量定义
用户变量用@打头,系统变量用@@打头
declare @i int
set @i =0;
在SQL语句中对变量的赋值用SET 或者SELECT */
游标的定义,将查询结果返回为游标类型
declare cursor cursor_name
for select name from sysobjects
open cursor_name
fetch next from cursor_name into @i;
close cursor_name
删除游标 deallocate cursor_name;