Oracle 中一次执行多条语句

一枚大帅哥 2015-01-15 01:48:48
小弟之前一直是用SQL SERVER ,最近公司项目要用ado.net 访问Oracle 数据库。遇到问题:
我想一次性在数据库插入多条数据,需求是这样的。
先通过 select fab_combo_seq.nextval from dual 触发器创建一个ID,
然后取到这个ID 写一条插入
Insert into....

以前在SQL直接就拼好一个事务的语句丢到数据库执行即可
begin tran
declare @id bigint,@err int
select @id=id from table1
insert into table2 values(@id,'aa')
set @err+=@@error
select @id=id from table1
insert into table2 values(@id,'bb')
set @err+=@@error
...此处省略N行
if(@err<>0)
begin
commit tran
end
else
begin
rollback tran
end

现在在Oracle该怎么实现呢?想一次性拼好语句一次执行,以减少流量。请教Oracle高手
...全文
818 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
块状编程 declare --此处变量定义 begin --处理逻辑 exception when others then ...--异常处理 end;
一枚大帅哥 2015-01-15
  • 打赏
  • 举报
回复
引用 3 楼 wmxcn2000 的回复:


declare
  m_id  int;
  m_str varchar2(30);
begin
  select fab_combo_seq.nextval into m_id from dual ;  -- 这样写
  insert into t (id) values (m_id);
  commit ;  -- 显式的提交
exception
  when others then
  rollback ; -- 显式的让这个事务回滚  
  m_str = '发生了错误';
end;
谢谢。
卖水果的net 2015-01-15
  • 打赏
  • 举报
回复


declare
  m_id  int;
  m_str varchar2(30);
begin
  select fab_combo_seq.nextval into m_id from dual ;  -- 这样写
  insert into t (id) values (m_id);
  commit ;  -- 显式的提交
exception
  when others then
  rollback ; -- 显式的让这个事务回滚  
  m_str = '发生了错误';
end;
一枚大帅哥 2015-01-15
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:

-- 大概这样,写成 PLSQL 块就可以了。
declare
  m_id  int;
  m_str varchar2(30);
begin
  m_id := 100;
  insert into t (id) values (m_id);
exception
  when others then
    m_str = '发生了错误';
end;

exception when others then m_str = '发生了错误'; 这里是什么意思? 就是插入失败了? 失败了会回滚吗? 还有m_id := 100; 这种赋值,我可以这样吗? m_id:=select fab_combo_seq.nextval from dual 取得ID吗?因为这个值不是固定的。
卖水果的net 2015-01-15
  • 打赏
  • 举报
回复

-- 大概这样,写成 PLSQL 块就可以了。
declare
  m_id  int;
  m_str varchar2(30);
begin
  m_id := 100;
  insert into t (id) values (m_id);
exception
  when others then
    m_str = '发生了错误';
end;

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧