Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Enlarging Field Size Programatically

Posted on 2002-07-10
5
Medium Priority
?
257 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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

670 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