Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Enlarging Field Size Programatically

We have a situation where we have around 200 Paradox tables in which we must change the size of a field from 4 bytes to 6 bytes.  Has anyone done anything like this without creating new tables and moving data field by field? I know the BDE has a dbidoRestructure function. Any help or suggestions greatly appreciated.

I should add that these 200 tables are in programs used by over 12,000 customers. We must do it programatically. Database Explorer, etc. is not an option here.
0
JohnMoshis
Asked:
JohnMoshis
1 Solution
 
aikimarkCommented:
From the following Borland newsgroup discussion,
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=3649ea8d.2190155%40news.erols.com&rnum=5&prev=/groups%3Fq%3Dparadox%2Bfield%2Bsize%2Bincrease%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D3649ea8d.2190155%2540news.erols.com%26rnum%3D5,

feed something like the following through a TQuery:
ALTER TABLE MARTINTABLE
ADD string1 CHAR(500);

UPDATE MARTINTABLE
SET string1 = COMMENTS;

ALTER TABLE MARTINTABLE
DROP COMMENTS;

ALTER TABLE MARTINTABLE
ADD COMMENTS CHAR(500);

UPDATE MARTINTABLE
SET COMMENTS = string1;

ALTER TABLE MARTINTABLE
DROP string1;

===================================
You can probably also use the Database Desktop utility, but that might be a chore with 200 tables.
0
 
TOndrejCommented:
For Paradox, you can use BDE API.
Examples on http://info.borland.com/devsupport/bde/delphifiles.html
0
 
jswebyCommented:
I'm with aikimark - I think this is the only generic SQL that handles it, especially if you're using Paradox which only supports very limited SQL. With SQL Server, there is the facility to modify fields, but not with Paradox, you have to do it the way aikimark gave.

J.
0
 
mikepjCommented:
Here's what I'd do:

use TFieldUpdate from http://www.torry.net/db_bde.htm

OR

Could also programmatically locate all the tables which contain the field which must be updated.

ExpandClientCodeField('clients');
ExpandClientCodeField('clientpurchases');
ExpandClientCodeField('clientcontracts');

ExpandClientCodeField
  Opens table exclusively
  Adds the larger field under a new name (NewClientCode)
  Close table
  Execute query:  UPDATE thetable SET newclientcode=clientcode
  Rename the fields so the new fields are named as the old fields (fix indexes, ref integrity).
 

// add field
type
  ChangeRec = packed record
                szName: DBINAME;
                iType: word;
                iSubType: word;
                iLength: word;
                iPrecision: byte;
              end;

const
  TypeMap: array[TFieldType] of Byte = (
     fldUNKNOWN, fldZSTRING, fldINT16, fldINT32, fldUINT16, fldBOOL,
     fldFLOAT, fldFLOAT, fldBCD, fldDATE, fldTIME, fldTIMESTAMP, fldBYTES,
     fldVARBYTES, fldBLOB, fldBLOB, fldBLOB);

procedure AddField(Table: TTable; NewField: ChangeRec);
{ This example will add a field to the end of an existing table.
  NOTE: You must fill in all options in the ChangeRec with 0 or '' if the option
  is not used in the restructure.  FillChar can be used to do this:
  Fillchar(MyChangeRec, sizeof(MyChangeRec), 0); }
var
  Alloc1,Alloc2:Word;
  Props: CURProps;
  hDb: hDBIDb;
  TableDesc: CRTblDesc;
  pFlds: pFLDDesc;
  pOp: pCROpType;
  B: byte;
begin
  { Make sure the table is open exclusively so we can get the db handle...}
  if Table.Active = False then
    raise EDatabaseError.Create('Table must be opened to restructure');
  if Table.Exclusive = False then
    raise EDatabaseError.Create('Table must be opened exclusively to restructure');

  { Get the table properties to determine table type...}
  Check(DbiSetProp(hDBIObj(Table.Handle), curxltMODE, integer(xltNONE)));
  Check(DbiGetCursorProps(Table.Handle, Props));

  Alloc1:=(Table.FieldCount + 1) * sizeof(FLDDesc);
  pFlds := AllocMem(Alloc1);
  FillChar(pFlds^, (Table.FieldCount + 1) * sizeof(FLDDesc), 0);
  Check(DbiGetFieldDescs(Table.handle, pFlds));

  for B := 1 to Table.FieldCount do
    begin
      pFlds^.iFldNum := B;
      Inc(pFlds, 1);
    end;

  try
    StrCopy(pFlds^.szName, NewField.szName);
    pFlds^.iFldType := NewField.iType;
    pFlds^.iSubType := NewField.iSubType;
    pFlds^.iUnits1  := NewField.iLength;
    pFlds^.iUnits2  := NewField.iPrecision;
    pFlds^.iFldNum  := Table.FieldCount + 1;
  finally
    Dec(pFlds, Table.FieldCount);
  end;

  Alloc2:=(Table.FieldCount + 1) * sizeof(CROpType);
  pOp := AllocMem(Alloc2);
  Inc(pOp, Table.FieldCount);
  pOp^ := crADD;
  Dec(pOp, Table.FieldCount);

  { Blank out the structure...}
  FillChar(TableDesc, sizeof(TableDesc), 0);

  { Get the database handle from the table's cursor handle...}
  Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));

  { Put the table name in the table descriptor...}
  StrPCopy(TableDesc.szTblName, Table.TableName);

  { Put the table type in the table descriptor...}
  StrPCopy(TableDesc.szTblType, Props.szTableType);

  { Close the table so the restructure can complete...}
  TableDesc.iFldCount := Table.FieldCount + 1;
  Tabledesc.pfldDesc := pFlds;
  TableDesc.pecrFldOp := pOp;
  Table.Close;

  { Call DbiDoRestructure...}
  try
    Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));
  finally
    FreeMem(pFlds,Alloc1);
    FreeMem(pOp,Alloc2);
    Table.Open;
  end;
end;

0
 
JohnMoshisAuthor Commented:
Thanks, mikepi. I'm sure one of the packages on Torry will do the job.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now