#1  
Old 29th June 2012, 10:06 PM
daniele daniele is offline
Junior Member
 
Join Date: Jul 2009
Location: Italy
Posts: 25
Default How to copy single record?

Hi,
i've 2 tables into 2 distinct alias.

I need to copy a single record from table A into table B, what is the fastest way?

The two tables have the same structure.


My example:

Code:
      while not TableA.Eof do
      begin
          if not TableB.Locate('CUSTOMER_ID', TableA['CUSTOMER'].AsString+ IntToStr(TableA['ID'].AsINTEGER), []) then
          begin
               //COPY current record of TableA into TableB
          end;

          TableA.Next;
      end;
There is a way to copy all fields value?
  #2  
Old 30th June 2012, 12:24 AM
Wolfgang
 
Posts: n/a
Default Re: How to copy single record?

var i: Integer;
Fld: TField;

for i := 0 to TableA.FieldCount - 1 do begin
Fld := TableB.FindField(TableA.Fields[i].FullName);
if Fld <> Nil then
Fld.Value := TableA.Fields[i].Value;
end;

On 29.06.2012 1:06 PM, daniele wrote:
> Hi,
> i've 2 tables into 2 distinct alias.
>
> I need to copy a single record from table A into table B, what is the
> fastest way?
>
> The two tables have the same structure.
>
>
> My example:
>
>
> Code:
> --------------------
>
> while not TableA.Eof do
> begin
> if not TableB.Locate('CUSTOMER_ID', TableA['CUSTOMER'].AsString+ IntToStr(TableA['ID'].AsINTEGER), []) then
> begin
> //COPY current record of TableA into TableB
> end;
>
> TableA.Next;
> end;
>
> --------------------
>
>
> There is a way to copy all fields value?
>
>


  #3  
Old 30th June 2012, 12:58 AM
daniele daniele is offline
Junior Member
 
Join Date: Jul 2009
Location: Italy
Posts: 25
Default

Thank you Wolfgang,
do you think that is the best way?

I've used TableB.CopyFields(TableA) but is very slow.

There is a way to speedup using a sql?

My table is like this:

TABLE A:
ID (Autoinc)
CUSTOMER_ID (SHORTSTRING)
Other field

I have more copy of TableA, one for each customer, and I want that all records of any TableA is copied into a TableB.

Can i do it by a sql?
  #4  
Old 30th June 2012, 01:13 AM
Alessandro Romano
 
Posts: n/a
Default Re: How to copy single record?

Hi (Ciao),


insert into TableB
select * from TableA [where....]


Alessandro Romano

"daniele" ha scritto nel messaggio
news:daniele.5exc1i@nx-forums.nexusdb.com...


Thank you Wolfgang,
do you think that is the best way?

I've used TableB.CopyFields(TableA) but is very slow.

There is a way to speedup using a sql?

My table is like this:

TABLE A:
ID (Autoinc)
CUSTOMER_ID (SHORTSTRING)
Other field

I have more copy of TableA, one for each customer, and I want that all
records of any TableA is copied into a TableB.

Can i do it by a sql?


--
daniele

  #5  
Old 30th June 2012, 01:56 AM
Samuel
 
Posts: n/a
Default Re: How to copy single record?

If both tables are in same nxServer, you can use:

INSERT INTO ALIASB.TABLEB (Field1, Field2, Field3.. FieldN)
(SELECT Field1, Field2, Field3.. FieldN FROM ALIASA.TABLEA WHERE ...)




Em 29/06/2012 08:06, daniele escreveu:
> Hi,
> i've 2 tables into 2 distinct alias.
>
> I need to copy a single record from table A into table B, what is the
> fastest way?
>
> The two tables have the same structure.
>
>
> My example:
>
>
> Code:
> --------------------
>
> while not TableA.Eof do
> begin
> if not TableB.Locate('CUSTOMER_ID', TableA['CUSTOMER'].AsString+ IntToStr(TableA['ID'].AsINTEGER), []) then
> begin
> //COPY current record of TableA into TableB
> end;
>
> TableA.Next;
> end;
>
> --------------------
>
>
> There is a way to copy all fields value?
>
>



  #6  
Old 30th June 2012, 03:52 AM
daniele daniele is offline
Junior Member
 
Join Date: Jul 2009
Location: Italy
Posts: 25
Default

Hi Samuel,
thank you for your reply.

This is my scenario:

I have a copy TableA of every customer in their specific alias,

eg:
TableA (Customer 001)
ID | Customer | Other fields...
1 001
2 001

TableA (Customer 002)
ID | Customer | Other fields...
1 002
2 002
3 002

I need a sql that insert all records of previous table, into TableB like this:

TableB (Customer 001 + Customer 002)
ID | Customer | Other fields...
1 001
2 001
3 002
4 002
5 002

Ok, this is easy, tomorrow i will have:

TableA (Customer 001)
ID | Customer | Other fields...
1 001
2 001
3 001 <-- new record
4 001 <-- new record

TableA (Customer 002)
ID | Customer | Other fields...
1 002
2 002
3 002
4 002 <-- new record
5 002 <-- new record
6 002 <-- new record

The new TableB must be:

TableB
ID | Customer | Other fields...
1 001
2 001
3 002
4 002
5 002
6 001 <-- new record from TableA Customer 001
7 001 <-- new record from TableA Customer 001
8 002 <-- new record from TableB Customer 002
9 002 <-- new record from TableB Customer 002
10 002 <-- new record from TableB Customer 002
11 002 <-- new record from TableB Customer 002


Can you help me?
Can i do it by a query?
What is the quickest way?

Thank you,
regards
Daniele
  #7  
Old 30th June 2012, 04:20 AM
Samuel
 
Posts: n/a
Default Re: How to copy single record?

Hi Daniele,

The best way, if possible, is use only one SQL because the data does not
"travel" over the network (everything is handled on the server).

Assuming that the ID field is the primary key, you could do the following:

INSERT INTO ALIASB.TABLEB (ID, CUSTOMER, OTHER FIELDS...)
(SELECT ID, CUSTOMER, OTHERFIELDS... FROM ALIASA.TABLEA A WHERE
NOT EXISTS (SELECT 'X' FROM ALIASB.TABLEB B WHERE B.ID = A.ID) )

[]s

Samuel
  #8  
Old 1st July 2012, 12:35 AM
daniele daniele is offline
Junior Member
 
Join Date: Jul 2009
Location: Italy
Posts: 25
Default

Thank you Samuel,
Grazie Alessandro.

Samuel, with your query work fine, i've only one problem with AutoInc field of destination table, because if i use

INSERT INTO ALIASB.TABLEB
(SELECT * FROM ALIASA.TABLEA A WHERE
NOT EXISTS (SELECT 'X' FROM ALIASB.TABLEB B WHERE B.ID = A.ID) )

the query copy Autoinc values and then, in the destination table, i've duplicate values of Autoinc field.

There is a way to copy all fields, without AutoInc field (and without list all fields) ?

Thank you
regards
Daniele
  #9  
Old 1st July 2012, 01:03 AM
Dave Sellers
 
Posts: n/a
Default Re: How to copy single record?

Something like:

INSERT INTO ALIASB.TABLEB (field1, field2 ... field 99)
(SELECT field 1, field 2 etc etc FROM ALIASA.TABLEA A WHERE
NOT EXISTS (SELECT 'X' FROM ALIASB.TABLEB B WHERE B.ID = A.ID) )

Should do it I think.

Dave
  #10  
Old 1st July 2012, 01:39 AM
daniele daniele is offline
Junior Member
 
Join Date: Jul 2009
Location: Italy
Posts: 25
Default

Thanks Dave,
I do not want to list all the fields one at a time, if it's possible....


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Self-referencing DB. How to tell if a record is a parent of another record? Jerry Hayes nexusdb.public.support.sql 10 22nd December 2007 08:29 AM
Copy Current Record Andrew Sowerby nexusdb.public.support 19 13th June 2007 07:42 AM
Copy Record, Speed, tnxTable Robert Hultberg nexusdb.public.support 6 16th May 2007 04:02 AM
Is there a method to copy record fields to fields in my class? Peter Sanders nexusdb.public.support 2 19th February 2007 07:23 PM
Single Package BPL.. Keith Johnson [NDX] nexusdb.public.discussions 8 25th April 2006 01:57 AM


All times are GMT +11. The time now is 04:45 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.