重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
declare
创新互联专注于西吉企业网站建设,成都响应式网站建设公司,成都做商城网站。西吉网站建设公司,为西吉等地区提供建站服务。全流程按需搭建网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
x varchar2(20);
v_length int;
y int;
v_str varchar2(1);
begin
x:='wqr3331412rr';
select length(x) into v_length from dual;
y:=1;
while y=v_length loop
select substr(x,y,1) into v_str from dual;
dbms_output.put_line(v_str);
y:=y+1;
end loop;
end;
直接运行吧,结果如图
--创建一个测试用表
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;
/
希望对你有帮助。
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;
仓促写下以上内容,可能部分语法报错,思路就是这样,很基本的一个游标使用。
写个for循环就可以遍历一遍,例如meminfo 表中有member_id 你现在有的id需要在meminfo 中查询出现次数
declare
i number(5);
id number(15);
begin
for rec in(select member_id from meminfo) loop
if member_id=id
then i:=i+1;
end if;
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;
不用循环不行么,一个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' --这个你换成你要查的字符串,数字字母什么都可以,只要逗号分隔就好