存储过程update
1. oracle 存储过程循环执行update语句
其实二楼写的最简单,但对于新手,最好别那么写,至于1楼,如果数据不是很多,没必要搞个游标。你也可以看看我写的
create or replace procere P_Update(o_vc_message out varchar2)
is
type column1 is table of table1.column1%type index by binary_integer;
col1s column1;
type rid is table of rowid index by binary_integer;
rids rid;
temp table1.column1%type;
begin
select column1,rowid bulk collect into col1s,rids from table1;
if (column1.count != 0) then
for i in col1s.first..col1s.last loop
temp := col1s(i);--处理 col1s(i) 想干嘛干嘛
update table1 set column1 = temp where rowid = rids(i);
end loop;
end if;
o_vc_message := 'OK!';
exception
when others then
o_vc_message := 'exception happend.' || sqlcode || sqlerrm;
rollback;
return;
end P_Update;
如果仅仅是简单处理column1,比如加1什么的,就别搞那么复杂,一个sql就ok了。
2. ORACLE中怎么执行存储过程
在Oracle数据库中执行存储过程的方式相对简单。使用SQL*Plus工具可以直接运行存储过程,具体命令为:
在SQL*Plus中,你可以使用exec 存储过程名命令来执行存储过程。如果存储过程需要传递参数,那么你需要在命令中写明参数。例如,如果存储过程名为my_procere,并且需要两个参数,你可以这样调用它:
exec my_procere(参数1, 参数2);
参数的具体类型和值应根据存储过程的定义进行设置。确保参数数量和类型与存储过程要求一致。如果不提供正确的参数,可能会导致执行失败或产生错误结果。
例如,假设有一个存储过程update_user_info,它需要三个参数:用户ID、用户名和电子邮件地址,你可以这样调用:
exec update_user_info(123, '张三', '[email protected]');
这样调用存储过程时,参数值会被传递给存储过程,存储过程根据这些参数执行相应的操作。
注意,执行存储过程时,如果存储过程内部有异常处理逻辑,它会根据异常处理规则返回结果或错误信息。执行过程中如果出现错误,SQL*Plus会显示错误信息,帮助你了解问题所在。
此外,对于一些复杂的存储过程,可能还需要查看存储过程的源代码,以确保参数传递的正确性。你可以通过以下命令查看存储过程的定义:
desc 存储过程名;
这将显示存储过程的参数列表和返回类型,帮助你更好地理解存储过程的使用。
总结来说,在Oracle中使用SQL*Plus执行存储过程,只需使用exec 存储过程名(参数列表)的命令,确保参数正确无误,执行过程即可顺利进行。
3. UPDATE 存储过程
先在数据库中创建test表,表中有列名为name,类型为varchar(50)
然后先执行
create procere proc_insert
@name varchar(50)
as
begin
insert into test values(@name)--插入数据
end
go
create procere proc_update
@newname varchar(50),@oldname varchar(50)
as
begin
update test set name=@newname where name=@oldname--更新数据
end
go
--其中proc_insert为存储过程名,可自定义 procere可使用简写proc
上面执行完成后调用存储过程
exec proc_insert '晓华'--将"晓华"添加到test表中
exec proc_update '小明','晓华' --将表中'晓华' 改为'小明',必须与存储过程变量顺序相同
exec proc_update @oldname='小明',@newname='晓华'--与存储过程变量顺序可以不同
drop procere proc_insert 删除存储过程proc_insert.
4. 存储过程中 ,如何先select出一个结果, 再insert或update到另一个表中
create procere myproc
as
begin
declare @p1 varchar(100)
declare @p2 int
select @p2=count(*) from table1 --第一种赋值方式
set @p1='第二种赋值方式'
insert table2 (col1,col2) values(@p1,@p2) --插入方式
update table3 set col3=@p2,@col4=@p1 where id=5 --更新/update方法
end
5. oracle存储过程中update语句一直在执行中,无法更新完成
可能这个表被别的用户锁了;
select sess.sid,
sess.serial#,
lo.oracle_username,
lo.os_user_name,
ao.object_name,
lo.locked_mode
from v$locked_object lo, dba_objects ao, v$session sess
where ao.object_id = lo.object_id
and lo.session_id = sess.sid;
--杀掉会话
alter system kill session 'sid,serial#';