--创建一个测试用表
10余年的六盘水网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整六盘水建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“六盘水网站设计”,“六盘水网站推广”以来,每个客户项目都认真落实执行。
CREATE TABLE test_main (
id INT,
value VARCHAR(10),
PRIMARY KEY(id)
);
--插入测试数据
INSERT INTO test_main(id, value) VALUES (1, 'ONE');
INSERT INTO test_main(id, value) VALUES (2, 'TWO');
INSERT INTO test_main(id, value) VALUES (3, 'THREE');
--游标举例
DECLARE
-- 定义游标.
CURSOR c_test_main IS
SELECT id, value FROM test_main;
-- 保存游标数据的变量
v_main_data c_test_main%ROWTYPE;
BEGIN
-- 打开游标.
OPEN c_test_main;
LOOP
-- 填充数据(主表).
FETCH c_test_main INTO v_main_data;
-- 假如没有检索到(主表)数据,结束循环处理
Exit when c_test_main%NOTFOUND;
dbms_output.put_line(TO_CHAR(v_main_data.id)
|| ':' || v_main_data.value );
END LOOP;
-- 关闭游标
CLOSE c_test_main;
END;
/
希望对你有帮助。
SQL code
DECLARE
-- Define a varray of twelve strings.
TYPE months_varray IS VARRAY(12) OF STRING(9 CHAR);
-- Define an associative array of strings.
TYPE calendar_table IS TABLE OF VARCHAR2(9 CHAR)
INDEX BY BINARY_INTEGER;
-- Declare and construct a varray.
month MONTHS_VARRAY :=
months_varray('January','February','March'
,'April','May','June'
,'July','August','September'
,'October','November','December');
-- Declare an associative array variable.
calendar CALENDAR_TABLE;
BEGIN
-- Check if calendar has no elements.
IF calendar.COUNT = 0 THEN
-- Print a title
DBMS_OUTPUT.PUT_LINE('Assignment loop:');
DBMS_OUTPUT.PUT_LINE('----------------');
-- Loop through all the varray elements.
FOR i IN month.FIRST..month.LAST LOOP
-- Initialize a null associative array element.
calendar(i) := '';
-- Print an indexed element from the associative array.
DBMS_OUTPUT.PUT_LINE(
'Index ['||i||'] is ['||calendar(i)||']');
-- Assign the numeric index valued varray element
-- to an equal index valued associative array element.
calendar(i) := month(i);
END LOOP;
-- Print a title
DBMS_OUTPUT.PUT(CHR(10));
DBMS_OUTPUT.PUT_LINE('Post-assignment loop:');
DBMS_OUTPUT.PUT_LINE('---------------------');
-- Loop through all the associative array elements.
FOR i IN calendar.FIRST..calendar.LAST LOOP
-- Print an indexed element from the associative array.
DBMS_OUTPUT.PUT_LINE(
'Index ['||i||'] is ['||calendar(i)||']');
END LOOP;
END IF;
END;
下面语句用于批量生成语句,生成后在sqlplus执行select 'select id,sum(result) from '||table_name||' group by id;' from user_tables where table_name like 'MRO_%_201702';
不用循环不行么,一个sql就搞定啦
select c from
(with test as (select '21,32,43' c from dual)
select substr(t.ca,instr(t.ca, ',', 1, c.lv) + 1,instr(t.ca, ',', 1, c.lv + 1) - (instr(t.ca, ',', 1, c.lv) + 1)) AS c
from (select ',' || c || ',' AS ca,length(c || ',') - nvl(length(REPLACE(c, ',')),0) AS cnt FROM test) t,
(select LEVEL lv from dual CONNECT BY LEVEL = 100) c where c.lv = t.cnt )
'21,32,43' --这个你换成你要查的字符串,数字字母什么都可以,只要逗号分隔就好
declare
teacher_name varchar(20)------------跟teacher表中老师名字类型保持一致
cursor t_name is select teachername from teacher---------申明游标t_name为从teacher表中查询老师名字
begin
open t_name;------打开游标t_name
loop-------开始循环(遍历)
fetch t_name into teacher_name-------将老师名字值赋予变量teacher_name
if t_name%found-------------开始遍历有值时插入以下数据
then
select name,count(*) into new_table
from table_teacher_student
where name=teacher_name group by name-----将一个老师名字依据条件插入新表数据
else
dmbs_output.put_line(‘完成所有工作’);---------遍历结束时输出完成工作
exit;
end if;
end loop;
仓促写下以上内容,可能部分语法报错,思路就是这样,很基本的一个游标使用。