Solved

Enlarging Field Size Programatically

Posted on 2002-07-10
5
254 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 274
Working with hours 3 65
Press three keys together and trigger a function 3 62
Wincontrol not (correctly) drawn 15 42
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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