Solved

Enlarging Field Size Programatically

Posted on 2002-07-10
5
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 46

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month6 days, 12 hours left to enroll

623 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