ORA-00913错误:PL/SQL: ORA-00913: too many values
ORA-00913错误
Name Null? Type
----------------- -------- ----------------------------
CUST_CODE NOT NULL VARCHAR2(10)
NAME VARCHAR2(30)
CITY VARCHAR2(20)
STATE CHAR(2)
SQL>CREATE SEQUENCE tcustmer_cust
INCREMENT BY 1
START WITH 1
MAXVALUE 100000000
CACHE 10000
NOCYCLE;
SQL> begin
2 for i in 1..10 loop
3 insert into tcustmer
4 values (tcustmer_cust.nextval,'T','test'||i,'BEIJING','CN');
5 if mod(i,10)=0 then
6 commit;
7 end if;
8 end loop;
9 commit;
10 end;
11 /
insert into tcustmer
*
ERROR at line 3:
ORA-06550: line 3, column 20:
PL/SQL: ORA-00913: too many values
ORA-06550: line 3, column 8:
PL/SQL: SQL Statement ignored
检查发现插入的values值,列数超过了tcustmer表的列数
调整例如以下:
SQL>begin
for i in 1..10 loop
insert into tcustmer
values ('T'||tcustmer_cust.nextval,'test'||i,'BEIJING','CN');
if mod(i,10)=0 then
commit;
end if;
end loop;
commit;
end;
/
PL/SQL procedure successfully completed.
描写叙述:PL/SQL: ORA-00913: too many values
目标:编写一个能够循环插入数据的脚本
操作过程:
SQL> desc tcustmerName Null? Type
----------------- -------- ----------------------------
CUST_CODE NOT NULL VARCHAR2(10)
NAME VARCHAR2(30)
CITY VARCHAR2(20)
STATE CHAR(2)
SQL>CREATE SEQUENCE tcustmer_cust
INCREMENT BY 1
START WITH 1
MAXVALUE 100000000
CACHE 10000
NOCYCLE;
SQL> begin
2 for i in 1..10 loop
3 insert into tcustmer
4 values (tcustmer_cust.nextval,'T','test'||i,'BEIJING','CN');
5 if mod(i,10)=0 then
6 commit;
7 end if;
8 end loop;
9 commit;
10 end;
11 /
insert into tcustmer
*
ERROR at line 3:
ORA-06550: line 3, column 20:
PL/SQL: ORA-00913: too many values
ORA-06550: line 3, column 8:
PL/SQL: SQL Statement ignored
检查发现插入的values值,列数超过了tcustmer表的列数
调整例如以下:
SQL>begin
for i in 1..10 loop
insert into tcustmer
values ('T'||tcustmer_cust.nextval,'test'||i,'BEIJING','CN');
if mod(i,10)=0 then
commit;
end if;
end loop;
commit;
end;
/
PL/SQL procedure successfully completed.
总结:
对于tcustmer_cust.nextval理解错误,创建序列的目的正是消除主键的干扰,所以在使用的时候须要将其放到列值中。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步