Create and copy table from db to another

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
bwilt
Posts: 15
Joined: Wed 07 Aug 2013 01:45

Create and copy table from db to another

Post by bwilt » Wed 07 Aug 2013 01:51

Hello,
I have Unidac 5.0.1 connected to a MDB.

I was wanting to copy a table from one MDB to another database which does not contain the table.

Is there a method to create the database schema from one database to another ?

Previously I had used CreateTable()

murkay
Posts: 14
Joined: Thu 08 Aug 2013 09:18

Re: Create and copy table from db to another

Post by murkay » Thu 08 Aug 2013 10:02

I use to create table in another msaccess database

UniConnection.ExecSQL('SELECT * INTO [' + DatabaseName + '].' + kod +
' FROM ' + kod + ' WHERE 0 = 1;');
Databasename is destination msaccess database name with full path.
UniConnection is your source connection.
kod is table name.

To copy you have many ways.
CRBatchMove , Uniloader
or
UniConnection.ExecSQL('INSERT INTO [' + DatabaseName + '].' + kod + ' SELECT * FROM ' + kod);
Last edited by murkay on Thu 08 Aug 2013 10:11, edited 1 time in total.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Create and copy table from db to another

Post by AlexP » Thu 08 Aug 2013 10:09

Hello,

To retrieve DDL and data transfer, you can use the following code, this code demonstrates retrieving of field names, types and size. To retrieve other data (keys, default values, etc.), you can modify the code by specifying the needed MetaDataKind

Code: Select all

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Uni,
  AccessUniProvider,
  CRBatchMove;

const
  TableName = 'test';

var
  UniConnectionSrc, UniConnectionDst: TUniConnection;
  UniTableSrc, UniTableDst: TUniTable;
  UniMetaDataSrc: TUniMetaData;
  BatchMove: TCRBatchMove;
  SQL: string;
begin
  UniConnectionSrc := TUniConnection.Create(nil);
  try
    UniConnectionSrc.Connect('ProviderName=Access;Database=d:\src.MDB');
    UniMetaDataSrc := TUniMetaData.Create(nil);
    try
      UniMetaDataSrc.Connection := UniConnectionSrc;
      UniMetaDataSrc.MetaDataKind := 'Columns';
      UniMetaDataSrc.Restrictions.Values['Table_Name'] := TableName;
      UniMetaDataSrc.Open;
      SQL := 'CREATE TABLE ' + TableName + ' ('#13#10;
      while not UniMetaDataSrc.Eof do begin
        SQL := SQL + UniMetaDataSrc.FieldByName('COLUMN_NAME').AsString + ' ' +
                     UniMetaDataSrc.FieldByName('DATA_TYPE').AsString;
        if Pos('CHAR', UniMetaDataSrc.FieldByName('DATA_TYPE').AsString) > 0 then
          SQL := SQL + '(' + UniMetaDataSrc.FieldByName('DATA_LENGTH').AsString + ')';
        UniMetaDataSrc.Next;
        if not UniMetaDataSrc.Eof then
          SQL := SQL + ',';
        SQL := SQL + #13#10;
      end;
      SQL := SQL + ')';
    finally
      UniMetaDataSrc.Free;
    end;
    UniConnectionDst := TUniConnection.Create(nil);
    try
      UniConnectionDst.Connect('ProviderName=Access;Database=d:\Dst.MDB');
      UniConnectionDst.ExecSQL(SQL);
      UniTableSrc := TUniTable.Create(nil);
      try
        UniTableSrc.Connection := UniConnectionSrc;
        UniTableSrc.TableName := TableName;
        UniTableSrc.Open;
        UniTableDst := TUniTable.Create(nil);
        try
          UniTableDst.Connection := UniConnectionDst;
          UniTableDst.TableName := TableName;
          UniTableDst.Open;
          BatchMove := TCRBatchMove.Create(nil);
          try
            BatchMove.Source := UniTableSrc;
            BatchMove.Destination := UniTableDst;
            BatchMove.Execute;
          finally
            BatchMove.Free;
          end;
        finally
          UniTableDst.Free;
        end;
       finally
        UniTableSrc.Free;
      end;
    finally
      UniConnectionDst.Free;
    end;
  finally
    UniConnectionSrc.Free;
  end;
end.

Post Reply