---
title: "plslq 语句编写规范"
author: "soft_xiaohui"
date: "2012-11-06T10:19:42Z"
tags: []
categories: ["数据库"]
slug: "plslq"
draft: false
lastmod: "2012-11-06T10:19:42Z"
id: 1717287
url: "https://soft-xiaohui.iteye.com/blog/1717287"
---

项目测试过程发现，公司测试无问题，客户测试报错，经过现场分析，发现为plsql中编写批量插入语句，

没有明确指明表的字段导致。

因为客户端数据表的字段顺序和公司数据库表字段顺序不一致导致。

-1400- 位置=,ORA-01400: cannot insert NULL into ("PMSDATA"."TFZ\_GRADE\_DETAIL\_REC\_HIS"."SCORE") stpb\_id=3901430  
原因：公司tfz\_grade\_detail\_rec\_his字段的顺序和平安不一致，导致不能插入正确值。  
insert into tfz\_grade\_detail\_rec\_his  
select v\_id,  
x.grin\_id,  
x.supply\_id,  
x.user\_id,  
x.score,  
x.remark,  
sysdate  
from tfz\_grade\_user\_result x  
where x.stpb\_id = i\_stpb\_id  
and x.stpr\_id = i\_stpr\_id  
and x.bundle\_code = i\_bundle\_code  
and x.user\_id = v.user\_id;

公司

batch\_id NUMBER(9) not null,  
grin\_id NUMBER(9) not null,  
supply\_id NUMBER(9) not null,  
user\_id NUMBER(9) not null,  
score NUMBER(5,2) not null,  
remark VARCHAR2(4000),  
cdate DATE default sysdate not null

平安

(  
BATCH\_ID NUMBER(9) not null,  
GRIN\_ID NUMBER(9) not null,  
SUPPLY\_ID NUMBER(9) not null,  
USER\_ID NUMBER(9) not null,  
REMARK VARCHAR2(4000),  
SCORE NUMBER(5,2) not null,  
CDATE DATE default sysdate not null  
)

更改：

insert into tfz\_grade\_detail\_rec\_his  
(batch\_id,grin\_id,supply\_id,user\_id,score,remark,cdate)  
select v\_id,  
x.grin\_id,  
x.supply\_id,  
x.user\_id,  
x.score,  
x.remark,  
sysdate  
from tfz\_grade\_user\_result x  
where x.stpb\_id = i\_stpb\_id  
and x.stpr\_id = i\_stpr\_id  
and x.bundle\_code = i\_bundle\_code  
and x.user\_id = v.user\_id;

指明要插入的表字段，是个好习惯，更规范。

 

深入分析：

导致公司和客户端表字段顺序不一致的原因是tfz\_grade\_detail\_rec\_his表之前已经客户数据库中，通过后续ddl更改，导致

表字段两边顺序不一致。因此如果对原始表更改较大，或许先drop掉重新建表会好点。