当前位置:首页 » 存储配置 » 存储过程循环输出

存储过程循环输出

发布时间: 2022-08-30 19:00:26

① 写一个存储过程,用游标循环每一个学生,每一次循环统计每一学生各个科目的成绩,打印到控制到输出如下

--学生表
create table t_xsb (xsbh number(10),xsxm varchar2(300));
comment on table t_xsb is '学生表';
comment on column t_xsb.xsbh is '学生编号';
comment on column t_xsb.xsxm is '学生姓名';
alter table t_xsb add constraints pk_xsb primary key (xsbh) using index;
--科目表
create table t_kmb (kmbh number(10),kmmc varchar2(300));
comment on table t_kmb is '科目表';
comment on column t_kmb.kmbh is '科目编号';
comment on column t_kmb.kmmc is '科目名称';
alter table t_kmb add constraints pk_kmb primary key (kmbh) using index;
--成绩表
create table t_cjb
(cjbh number(10),
cjfs number(10,1),
xsbh number(10),
kmbh number(10));
comment on table t_cjb is '成绩表';
comment on column t_cjb.cjbh is '成绩编号';
comment on column t_cjb.cjfs is '成绩分数';
comment on column t_cjb.xsbh is '学生编号';
comment on column t_cjb.kmbh is '科目编号';
alter table t_cjb add constraints pk_cjb primary key (cjbh) using index;
alter table t_cjb add constraints fk_cjb_xsbh foreign key (xsbh)
references t_xsb(xsbh);
alter table t_cjb add constraints fk_cjb_kmbh foreign key (kmbh)
references t_kmb(kmbh);
--创建序列
create sequence sq_ls
increment by 1
start with 1000000000
maxvalue 9999999999
nocycle
nocache;
--创建学生表的before insert触发器,实现对学生表主键的自动增长列
create or replace trigger r_xsb
before insert on t_xsb
for each row
declare
v_xsbh number(10) := null;
begin
v_xsbh := sq_ls.nextval;
:new.xsbh := v_xsbh;
end;
--写一个存储过程,用游标循环每一个学生,
--每一次循环统计每一学生各个科目的成绩,打印到控制
create or replace procere p_xscjtj is
v_xsbh number(10) := null;
v_xsxm varchar2(300) := null;
v_sx_nm number(10) := null;
v_yw_nm number(10) := null;
v_yy_nm number(10) := null;
v_zf_nm number(10) := null;
v_sx_ch varchar2(300) := null;
v_yw_ch varchar2(300) := null;
v_yy_ch varchar2(300) := null;
v_zf_ch varchar2(300) := null;
cursor c_xs is
select t.xsbh, t.xsxm from t_xsb t;
begin
dbms_output.enable(buffer_size => null);
open c_xs;
dbms_output.put_line(rpad(STR1 => '姓名',
PAD => ' ',
LEN => lengthb('姓名') + 5) ||
rpad(STR1 => '数学',
PAD => ' ',
LEN => lengthb('数学') + 5) ||
rpad(STR1 => '语文',
PAD => ' ',
LEN => lengthb('语文') + 5) ||
rpad(STR1 => '英语',
PAD => ' ',
LEN => lengthb('英语') + 5) ||
rpad(STR1 => '总分',
PAD => ' ',
LEN => lengthb('总分') + 5));
loop
fetch c_xs
into v_xsbh, v_xsxm;
exit when c_xs%notfound;
begin
select nvl(sum(case
when t2.kmmc = '数学' then
t1.cjfs
else
0
end),
0) sx,
nvl(sum(case
when t2.kmmc = '语文' then
t1.cjfs
else
0
end),
0) yw,
nvl(sum(case
when t2.kmmc = '英语' then
t1.cjfs
else
0
end),
0) yy,
nvl(sum(t1.cjfs), 0) zf
into v_sx_nm, v_yw_nm, v_yy_nm, v_zf_nm
from t_cjb t1, t_kmb t2
where t1.kmbh = t2.kmbh
and t1.xsbh = v_xsbh
group by t1.xsbh;
exception
when others then
v_sx_nm := 0;
v_yw_nm := 0;
v_yy_nm := 0;
v_zf_nm := 0;
end;
v_xsxm := rpad(STR1 => v_xsxm, PAD => ' ', LEN => lengthb('姓名') + 5);
v_sx_ch := rpad(STR1 => v_sx_nm, PAD => ' ', LEN => lengthb('数学') + 5);
v_yw_ch := rpad(STR1 => v_yw_nm, PAD => ' ', LEN => lengthb('语文') + 5);
v_yy_ch := rpad(STR1 => v_yy_nm, PAD => ' ', LEN => lengthb('英语') + 5);
v_zf_ch := rpad(STR1 => v_zf_nm, PAD => ' ', LEN => lengthb('总分') + 5);
dbms_output.put_line(v_xsxm || v_sx_ch || v_yw_ch || v_yy_ch ||
v_zf_ch);
end loop;
close c_xs;
exception
when others then
dbms_output.put_line(sqlerrm);
end;

② 使用存储过程中的游标以及循环把表内的若干字段拼接起来输出

你写的也太简单了。
游标是需要基本Sql的。拼接的算法是啥你都没说。
要是没啥Sql选择要求。你还不如用
Selec @SQL = ''
While(写上你的循环条件)
Begin
Selec @SQL = @SQL + XXXX
END

③ Oracle存储过程游标for循环怎么写

一、不带参数的游标for循环

1
首先编写存储过程的整体结构,如下:
create or replace procere test_proc is
v_date date; --变量定义
begin
select sysdate into v_date from al;
end test_proc;

2
定义游标:
create or replace procere test_proc is
v_date date; --定义变量
cursor cur is select * from ldcode; --定义游标
begin
select sysdate into v_date from al;
end test_proc;

3
编写for循环:
create or replace procere test_proc is
v_date date; --定义变量
cursor cur is select * from ldcode where rownum<10; --定义游标
begin
select sysdate into v_date from al;
--游标for循环开始
for temp in cur loop --temp为临时变量名,自己任意起
Dbms_Output.put_line(temp.Code); --输出某个字段,使用"变量名.列名"即可。
end loop;
--游标for循环结束
end test_proc;

4
测试运行,点击【DBMS Output】标签页查看结果如下图:

END
二、带参数的游标for循环

1
定义带参数的游标:
cursor cur(v_codetype ldcode.Codetype%TYPE) is
select * from ldcode where codetype = v_codetype; --定义游标
定义游标格式:
cursor 游标名称(变量定义) is 查询语句;
注意:
where条件中的变量名v_codetype要与游标定义cur(v_codetype ldcode.Codetype%TYPE)中的一致。

2
编写for循环部分:
--游标for循环开始
for temp in cur('llmedfeetype') loop
--temp为临时变量名,自己任意起
--cur('llmedfeetype')为"游标名称(传入的变量)"
Dbms_Output.put_line(temp.Code); --输出某个字段,使用"变量名.列名"即可。
end loop;
--游标for循环结束

3
测试运行,点击【DBMS Output

④ oracle存储过程中循环for in是如何使用的

1、首先编写存储过程的整体结构,如下图所示定义变量。

⑤ oracle存储过程如何输出信息

可用DBMS_OUTPUT.PUT_LINE()对存储过程的进行输出。

编写存储过程:

create or replace procere test_pro(in_num number)

as

M number;

begin

M := in_num;

if 0 < M then

dbms_output.put_line('输出SQL语句1');

elsif M < 3 then

dbms_output.put_line('输出SQL语句2');

else

dbms_output.put_line('nothing');

end if;

end;

(5)存储过程循环输出扩展阅读;

存储在数据库的数据字典中,存储在当前的应用中安全性由数据库提供安全保证,必须通过授权才能使用存储子程序,安全性靠应用程序来保证,如果能执行应用程序,就能执行该子程序。模式描述IN参数用来从调用环境中向存储过程传递值,不能给IN参数赋值,给此参数传递的值可以是常量、有值的变量、表达式等。

⑥ oracle存储过程怎么写循环

写循环的操作方法和步骤如下:

1、第一步,编写存储过程的整体结构,然后定义变量,见下图。

⑦ sql语句编写存储过程,使用游标循环打印学生表中的数据,求大神

写一个例子给楼主看下就知道了:

在sqlserver2000中新建一个存储过程:
CREATEPROCEDUREPK_Test
AS
//声明1个变量
declare@namenvarchar(20)

//声明一个游标mycursor,select语句中参数的个数必须要和从游标取出的变量名相同


//打开游标
openmycursor

//从游标里取出数据赋值到我们刚才声明的2个变量中

fetchnextfrommycursorinto@name

//判断游标的状态
//0fetch语句成功
//-1fetch语句失败或此行不在结果集中
//-2被提取的行不存在
while(@@fetch_status=0)
begin

//显示出我们每次用游标取出的值
print'游标成功取出一条数据'
print@name

//用游标去取下一条记录
fetchnextfrommycursorinto@name

end

//关闭游标
closemycursor

//撤销游标
deallocatemycursor

GO

⑧ 如何php调用oracle存储过程返回的是一个结果集,该怎么从php页面中吧数据循环输出呀

如果返回值是资源的话,可以用函数mysql_fetch_array()mysql_fetch_assoc()mysql_fetch_row()中的任意一个!

热点内容
bika安卓怎么下载 发布:2025-03-18 00:43:49 浏览:216
mysql删除数据库所有表 发布:2025-03-18 00:43:41 浏览:397
加减法括号的运算法则 发布:2025-03-18 00:35:44 浏览:553
怎么修改手机配置 发布:2025-03-18 00:34:51 浏览:750
安卓应用名称怎么修改 发布:2025-03-18 00:30:46 浏览:662
android默认桌面 发布:2025-03-18 00:29:59 浏览:872
超级超级解压的史莱姆合集 发布:2025-03-18 00:20:58 浏览:691
星火云服务器 发布:2025-03-18 00:13:06 浏览:708
ci框架indexphp 发布:2025-03-18 00:11:16 浏览:679
编程设计基础 发布:2025-03-18 00:09:36 浏览:300