Key violation.(20分)

  • 主题发起人 主题发起人 yuki246
  • 开始时间 开始时间
Y

yuki246

Unregistered / Unconfirmed
GUEST, unregistred user!
Key violation.
[Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMSRY
KEY constraint 'PK_lwzjk'.Cannot insert duplicate key in
object 'lwzjk'.
General SQL error.
[Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been
terminated.

请问大家这是什么错误呀??谢了
 
主键冲突,也就是说有一个列a,为主键,
有一个值为aa,而现在你又要插入一个aa,就会主键冲突。
 
可是我的主键没有重复阿??
 
你能把代码贴出来吗?
 
procedure Tjklrfrm.BitBtn2Click(Sender: TObject);
var
ls1,ls2,lsh,dat:string;
lsh1: integer;
jkjez:double;
begin
if messagedlg('请准备好打印机,打印入库单并保存?', mtinformation, [mbyes, mbno], 0) = mrno then
exit;
ComboBox1.Clear;
ComboBox1.Items.Assign(Printer.Printers);
if ComboBox1.Items.CommaText='' then
begin
Messagedlg('你需要安装打印机才能打印!',mtError,[mbOk],0);
exit;
end;

dat:=FormatDateTime('YYYYMMDD', date1.date);
try
datamodule1.wzjkQuery1.MoveBy(6);
except
on e: exception do
begin
showmessage('根据套打要求一次只能入5条');
exit;
end;
end;
if khbmt.Text = '' then
begin
showmessage('没供应商,请选择');
khbmt.SetFocus;
exit;
end;
if qgrt.Text = '' then
begin
showmessage('没请购人,请选择');
qgrt.SetFocus;
exit;
end;
if ywyt.Text = '' then
begin
showmessage('你采购员,请选择');
ywyt.SetFocus;
exit;
end;

if cgyt.Text = '' then
begin
showmessage('没仓管员,请选择');
cgyt.SetFocus;
exit;
end;
if pzrt.Text = '' then
begin
showmessage('没审批人,请选择');
pzrt.SetFocus;
exit;
end;
query1.Close;
query1.SQL.Clear;
query1.SQL.Text :=
'select count(lsh_id) as kk from lwzjk where left(lsh_id,6)=left(''' + dat +
''',6)';
query1.open;
query1.First;
lsh1 := query1.Fieldvalues['kk'];
ls1:=floattostr(lsh1+1);
with datamodule1 do
begin
wzjkquery1.First;
while not wzjkquery1.Eof do
begin
wzjkQuery1.Edit;
if wzjkquery1.FieldByName('wzbm').IsNull then
wzjkquery1.FieldValues['wzbm'] := '';

if wzjkquery1.FieldByName('jkje').IsNull then
wzjkquery1.FieldValues['jkje'] := 0;

if wzjkquery1.FieldByName('jksl').IsNull then
wzjkquery1.FieldValues['jksl'] := 0;

if wzjkquery1.FieldByName('jkdj').IsNull then
wzjkquery1.FieldValues['jkdj'] := 0;

if wzjkquery1.FieldValues['wzbm'] = '' then
begin
messagebox(jklrfrm.Handle, '录入不完整,请继续!', '提示窗口',
mb_iconasterisk);
exit;
end;

if wzjkquery1.FieldValues['jksl'] = 0 then
if wzjkquery1.FieldValues['jkje'] = 0 then
begin
messagebox(jklrfrm.Handle, '录入不完整,请继续!', '提示窗口',
mb_iconasterisk);
exit;
end;

if wzjkquery1.FieldValues['jksl'] <> 0 then
if wzjkquery1.FieldValues['jkje'] <> 0 then
wzjkquery1.FieldValues['jkdj'] := wzjkquery1.FieldValues['jkje'] /
wzjkquery1.FieldValues['jksl'];
wzjkQuery1.Next;
wzjkQuery1.Edit;
end;
end;

with datamodule1 do
begin
wzjkquery1.First;
while not wzjkQuery1.Eof do
begin
lsh1 := lsh1 + 1;
lsh := trim(floattostr(lsh1));
lsh := copy(dat, 1, 6) + copy('0000', length(lsh), (4 - length(lsh)))+lsh;
ls2:=copy(lsh,7,4);
wzjkquery1.Edit;
wzjkquery1.FieldValues['zbm'] := wzjkquery1.FieldValues['wzbm'] +
loginfrm.ckbm;
wzjkquery1.FieldValues['lsh_id'] := lsh;
wzjkquery1.FieldValues['khbm'] := khbmquery1.FieldValues['khbm'];
wzjkquery1.FieldValues['qgr'] := zgbmquery1.FieldValues['zgxm'];
wzjkquery1.FieldValues['ysy'] := cgyquery1.FieldValues['zgxm'];
wzjkquery1.FieldValues['ywy'] := ywyquery1.FieldValues['zgxm'];
wzjkquery1.FieldValues['pzr'] := pzrquery1.FieldValues['zgxm'];
wzjkquery1.FieldValues['ckbm'] := loginfrm.ckbm;
wzjkquery1.FieldValues['llr'] := loginfrm.username;
wzjkquery1.FieldValues['xgbj'] := 'B';
wzjkquery1.fieldvalues['jkrq'] := dat;
wzjkquery1.fieldvalues['lx'] := '采购入库';
wzjkquery1.fieldvalues['bz'] := bz.Text;
wzjkquery1.fieldvalues['fph'] := fphm.Text;
wzjkquery1.fieldvalues['fplx'] := fplx.Text;
if wzkcquery1.Locate('zbm', wzjkquery1.FieldValues['zbm'], []) then
begin
wzkcquery1.Edit;
wzkcquery1.FieldValues['kcje'] := wzkcquery1.FieldValues['kcje'] +
wzjkquery1.FieldValues['jkje'];
wzkcquery1.FieldValues['kcsl'] := wzkcquery1.FieldValues['kcsl'] +
wzjkquery1.FieldValues['jksl'];
if wzkcquery1.FieldValues['kcsl'] < 0 then
if wzkcquery1.FieldValues['kcje'] > 0 then
begin
messagebox(jklrfrm.Handle,
'该物资出现库存数量与金额不符,请通知系统管理员!!', '警告窗口',
mb_iconasterisk);
exit;
end;
if wzkcquery1.FieldValues['kcsl'] > 0 then
if wzkcquery1.FieldValues['kcje'] < 0 then
begin
messagebox(jklrfrm.Handle,
'该物资出现库存数量与金额不符,请通知系统管理员!!', '警告窗口',
mb_iconasterisk);
exit;
end;

if wzkcquery1.FieldValues['kcsl'] <> 0 then
wzkcquery1.FieldValues['pjdj'] := wzkcquery1.FieldValues['kcje'] /
wzkcquery1.FieldValues['kcsl'];
if wzkcquery1.FieldValues['kcsl'] = 0 then
wzkcquery1.FieldValues['pjdj'] := wzkcquery1.FieldValues['pjdj'];
end

else
begin
wzkcquery1.Edit;
wzkcquery1.Append;
wzkcquery1.FieldValues['zbm'] := wzjkquery1.FieldValues['wzbm'] +
wzjkquery1.FieldValues['ckbm'];
wzkcquery1.FieldValues['wzbm'] := wzjkquery1.FieldValues['wzbm'];
wzkcquery1.FieldValues['ckbm'] := wzjkquery1.FieldValues['ckbm'];
wzkcquery1.FieldValues['kcje'] := wzjkquery1.FieldValues['jkje'];
wzkcquery1.FieldValues['kcsl'] := wzjkquery1.FieldValues['jksl'];
wzkcquery1.FieldValues['pjdj'] := wzjkquery1.FieldValues['jkdj'];
end;
wzjkquery1.Next;
wzjkquery1.Edit;
end;
end;
query3.Close;
query3.SQL.Clear;
datamodule1.wzjkQuery1.First;
while not datamodule1.wzjkQuery1.Eof do
begin
query3.Close;
query3.SQL.Clear;
if copy(loginfrm.ckmc,1,4)='股份' then
query3.SQL.Text:='update fz_cg.dbo.nwzmx set jd=''采购入库'',rkrq='''+dat+''' '+
' where wzh='''+datamodule1.wzjkQuery1.FieldValues['wzh']+''' and zt=''股份'' and jd<>''采购入库'' ' ;
if copy(loginfrm.ckmc,1,4)<>'股份' then
query3.SQL.Text:='update fz_cg.dbo.nwzmx set jd=''采购入库'',rkrq='''+dat+''' '+
' where wzh='''+datamodule1.wzjkQuery1.FieldValues['wzh']+''' and zt=''集团'' and jd<>''采购入库'' ';
try
query3.ExecSQL;
except
on e: exception do
begin
showmessage('物资号无法保存,请稍候再输');
exit;
end;
end;
query3.Close;
query3.SQL.Clear;
if copy(loginfrm.ckmc,1,4)='股份' then
query3.SQL.Text:=' update fz_cg.dbo.ncgzl set rkrq='''+dat+''' '+
' where wzh='''+datamodule1.wzjkQuery1.FieldValues['wzh']+''' and zt=''股份'' and rkrq is null ';
if copy(loginfrm.ckmc,1,4)<>'股份' then
query3.SQL.Text:=' update fz_cg.dbo.ncgzl set rkrq='''+dat+''' '+
' where wzh='''+datamodule1.wzjkQuery1.FieldValues['wzh']+''' and zt=''集团'' and rkrq is null ';
try
query3.ExecSQL;
except
on e: exception do
begin
showmessage('物资号无法保存,请稍候再输');
exit;
end;
end;
datamodule1.wzjkQuery1.Next;
end;
datamodule1.wzjkQuery1.First;
while not datamodule1.wzjkQuery1.Eof do
begin
query3.Close;
query3.SQL.Clear;
query3.SQL.Text:='select wzh from fz_cg.dbo.nwzmx where wzh='''+datamodule1.wzjkQuery1.FieldValues['wzh']+'''';
query3.Open;
if query3.IsEmpty then
begin
showmessage('物资号输入有错,仔细审查或找审计部');
exit;
end;
datamodule1.wzjkQuery1.Next;
end;
ls1:=copy('0000', length(ls1), (4 - length(ls1)))+ls1;
jkjez:=0;
datamodule1.wzjkQuery1.First;
while not datamodule1.wzjkQuery1.Eof do
begin
jkjez:=jkjez+datamodule1.wzjkquery1.FieldValues['jkje'];
datamodule1.wzjkQuery1.Next;
end;
jkdyreport1.jkz.Caption:=floattostr(jkjez);
jkdyreport1.ckmc.Caption:=loginfrm.ckmc;
jkdyreport1.n.Caption:=copy(dat,4,1);
jkdyreport1.y.Caption:=copy(dat,5,2);
jkdyreport1.r.Caption:=copy(dat,7,2);
jkdyreport1.h1.Caption:=ls1;
jkdyreport1.h2.Caption:=ls2;
jkdyreport1.xgd.Caption:='';
jkdyreport1.khbm.Caption:=khbmt.text;
jkdyreport1.Print;
gdatabase.StartTransaction;
try
datamodule1.wzjkQuery1.ApplyUpdates;
datamodule1.wzkcQuery1.ApplyUpdates;
datamodule1.wzjkQuery1.CommitUpdates;
datamodule1.wzkcQuery1.CommitUpdates;
except
on e: exception do
begin
showmessage('无法保存,请稍候再输');
datamodule1.wzjkQuery1.CancelUpdates;
datamodule1.wzkcQuery1.CancelUpdates;
gdatabase.Rollback;
exit;
end;
end;
gdatabase.Commit;
showmessage('数据正确保存,按任意键返回');
bitbtn1.Enabled := true;
bitbtn2.Enabled := false;
bitbtn3.Enabled := false;
bitbtn4.Enabled := false;
bitbtn5.Enabled := true;
bitbtn1.SetFocus;
end;
 
你做update操作,试图将主键改为一个已有的值也会造成主键冲突。
例如这样的语句:wzkcquery1.Edit; 就会产生一个update操作。
 
真服了你,一个过程这么多行代码,偑服中
 
后退
顶部