本来我是这样写的:
begin
  adoquery.SQL.Text := 'select top 1 * from tablename';
  adoquery.Open;
  try
    adoquery.FieldByName('id').FocusControl;
  except
    begin
    adoquery.SQL.Text := 'alter table tablename add id IDENTITY(1,1)    PRIMARY KEY ';
    adoquery.ExecSQL;
    end;
  end;
end;
在不是主键的字段不会出问题,但是在主键字段就会出问题,还有怎么删除主键,然后再赋值给别的字段!???

解决方案 »

  1.   

    如果你是用的oracle
    procedure check(sColumn,sTable:String):Boolean;
    var
      sSql:string;
      dtst:TClientDataset;//也可以用adoquery,query等等
    begin
      sSql:='select column_name from all_cons_columns where '+
            'table_name='''+sTable+''' and constraint_name=('+
            'select constraint_name '+
            'from 'all_constraints where owner=''YXGL'''+
            ' and table_name='''+stable+''' and constraint_type=''P'''+
            ') order by position';
      //在这里加几条语句把sSql检索到dtst中
      if dtst.Locate('column_name',sColumn,[]) then
        result:=true
      else
        Result:=false;
    end;