关于插入的存储过程和函数

存储过程
create or replace procedure p_4s(
p_type in varchar2,
p_num in number,
p_da in date,
p_id in number,
p_price in number
)
is
begin
insert into t_4s(type,num,da,id,price) values (p_type,p_num,p_da,p_id,p_price);
end p_4s;
执行过程
begin
-- Call the procedure
p_4s('',1,sysdate,3,1);
end;
select * from t_4s;
函数
create or replace function f_4s(
p_type in varchar2,
p_num in number,
p_da in date,
p_id in number,
p_price in number
)
return number
is
n number;
begin
insert into t_4s(type,num,da,id,price) values (p_type,p_num,p_da,p_id,p_price);
commit;
select count(*) into n from t_4s;
return n;
end f_4s;
执行过程
declare
n number;
begin
-- Call the procedure
n:=f_4s('type',2,sysdate,2,3);
dbms_output.put_line(n);

end;
注意:
(1) 如果函数没有参数,那么函数名后不应该要括号;
(2) 创建函数的时候end后面一定要记得写函数名 。

一、oracle本身没有boolean类型,就是说跟数据库相关的类型中不包括boolean,一般采用number(1)和char(1)来实现。
所 以”You cannot insert the values TRUE and FALSE into a database column. Also, you cannot select or fetch column values into a BOOLEAN variable.“
plsql为了实现结构化编程,支持了boolean类型,所以可能会出现的问题是一个存储过程或者函数的返回参数是boolean型的,但在call这个procedure的时候,无法注册boolean类型的返回参数,执行时会抛出”参数类型不对“的exception,解决的方案就是把boolean的参数用别的类型代替。
二 如果是特定boolean类型情况下,Char(1)是比Number(1)更好的选择,因为前者所用的存储空间会比后者少,但这二者在查询时存储空间的节省会提供查效率,但是要注意的是用Char(1)的时候不能让这个字段可以为空,必须有缺省,否则查询效率会降低
三、PL/SQL 中有boolean类型,有null类型
PL/SQL中是有Boolean类型的,只能取2个值:true和false;
存储过程中的变量也支持boolean型;
但数据库中没有boolean类型。
四、存储过程中:
declare
v1 boolean;
begin
v1:=1>2;
if(v1)then
dbms_output.put_line('true');
else
dbms_output.put_line('false');
end if;
end;
打印:false
----------------------------------------
declare
v1 boolean;
begin
v1:=1>2;
dbms_output.put_line(v1);
end;
会报错。运行时得到错误信息:调用'PUT_LINE' 时参数个数或类型错误。这是因为在脚本中不能直接打印boolean类型的值。
五:
oracle 没有boolean,mysql用bit(1)而oracle可以用char(1) check(...(0,1))字段,
如:
create table a ( a char(1) check (a in(0,1)))
然后JDBC用getBoolean()可以返回正确的结果。

SQL SERVER存储过程
if object_id(N'p_test') is not null
drop proc p_test
go
create proc p_test(
@id int,
@username varchar(50)='0' output,
@password varchar(50)='0' output
)
as
begin
begin try
if @id = 3
begin
select @username=username,@password=password from t_user where id=@id
end
else
insert into t_user(username,password) values('123','123')
print('error')

print(@id);
end try
begin catch
print(error_number())
print(error_message())
end catch
end
go
执行过程
declare
@username varchar(50),
@password varchar(50)
exec p_test 2,@username output,@password output
print(@username+'||'+@password)
go

参考资料:http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值