Solved

Enlarging Field Size Programatically

Posted on 2002-07-10
5
245 Views
Last Modified: 2010-07-27
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
Comment
Question by:JohnMoshis
5 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 7145041
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
 
LVL 8

Expert Comment

by:TOndrej
ID: 7145893
For Paradox, you can use BDE API.
Examples on http://info.borland.com/devsupport/bde/delphifiles.html
0
 
LVL 4

Expert Comment

by:jsweby
ID: 7172850
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
 
LVL 2

Accepted Solution

by:
mikepj earned 100 total points
ID: 7177841
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
 

Author Comment

by:JohnMoshis
ID: 7178657
Thanks, mikepi. I'm sure one of the packages on Torry will do the job.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now