删除表记录有关问题
删除表记录问题
我在删除一表记录时这样写的一段代码
procedure TfrmDepInfo.SpeedButton3Click(Sender: TObject);
begin
if Application.MessageBox( '确定要删除吗 ', '系统提示 ',MB_OKCANCEL)= IDOK then
begin
with ADOQuery1 do
begin
ADOQuery1.Delete;
MessageBeep(0);
ShowMessage( '记录已经删除 ')
end
end
end; 可是如果表有外键索引的话,这样写会出错,应该怎么写,我试着写一SQL语句判断该删表中外键索引的字段是不是在外表中存在,如存在就不能删除,
eg:select * from T2 where colmun in (select colmun from T1 where colmun=value) 如果这条语句的RecordCount> 0的话,就可以判断不能删除了,不过,这个Value是怎么得到呢?有其它的方法也更好,谢谢!
------解决方案--------------------
ADOQuery1.FieldByName( '主键 ').value; //这里就应该你要的
ADOQuery1.Delete;
------解决方案--------------------
或者你用
try
...
except
end;
会更好一点!
我在删除一表记录时这样写的一段代码
procedure TfrmDepInfo.SpeedButton3Click(Sender: TObject);
begin
if Application.MessageBox( '确定要删除吗 ', '系统提示 ',MB_OKCANCEL)= IDOK then
begin
with ADOQuery1 do
begin
ADOQuery1.Delete;
MessageBeep(0);
ShowMessage( '记录已经删除 ')
end
end
end; 可是如果表有外键索引的话,这样写会出错,应该怎么写,我试着写一SQL语句判断该删表中外键索引的字段是不是在外表中存在,如存在就不能删除,
eg:select * from T2 where colmun in (select colmun from T1 where colmun=value) 如果这条语句的RecordCount> 0的话,就可以判断不能删除了,不过,这个Value是怎么得到呢?有其它的方法也更好,谢谢!
------解决方案--------------------
ADOQuery1.FieldByName( '主键 ').value; //这里就应该你要的
ADOQuery1.Delete;
------解决方案--------------------
或者你用
try
...
except
end;
会更好一点!