Solved

SQL copy record

Posted on 2001-07-04
18
3,631 Views
Last Modified: 2011-09-20
I'm not that familar with databases so this question may be impossible to solve or sound stupid to you, but I hope you understand it :)

I have selected a record in a TQuery and wants to insert this record into another table in another database.

The function should be generic and handle all kinds of datatypes: VarChar, Timestamp, Blob etc.

I was told not to use BDE if it's possible, that means I can't use TBatchMove, isn't that correct?

The way I thought of is to have two TQueries, One to select the record another on to do the insertion, with this SQL code:
INSERT INTO :TABLE VALUES(:VALUES)

I would then fill out the params, but how does I fill out VALUES. When I can get a lot of different types it isn't just to loop through, like this (may have errors, but you should get my point)
for i:=0 to Query1.FieldCount-1 do
  Values := Values+'"'+Query1.Fields[i]+'";'

What to do?
0
Comment
Question by:sorentop
  • 11
  • 6
18 Comments
 

Expert Comment

by:didil
ID: 6254005
hi there,
    maybe you're correct about the code, but you have to know the datatype. string type have to use ' .
but how about blob type ? is anyone know ?

hmm...
   how about if you define 2 tables, than you read the table1 to eof then you put each record into table2.

Table1.TableName := TableSourceName;
Table2.TableName := TableDestinationName;

While Not Table1.Eof do
Begin
   Table2.Insert;
   For i := 0 to Table1.FieldCount -1 do
      Table2.Field[i] := Table1.Field[i];
   Table2.Post;
   Table1.Next;
End;

But i don't know it use BDE or not ?

hope it's helpful
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6254256
why not use the bde (didnt have any problem with this) like

query1.sql.text := 'select * from atable';
query1.open;
table1.batchmove(query1,batcopy);  //will create a table, you will have to set first the databasename and tablename (table must not exist)
query1.colse

you could use also a other select like

insert into ADestTable select * from ASourcetable

in this case the ADestTable must exist first

meikl ;-)
0
 

Author Comment

by:sorentop
ID: 6254707
insert into ADestTable select * from ASourcetable
You can't just that, because the two tables are in different databases
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6254726
yup, the tables must have the same structure of course, by doing this,
but by different structure then you can do this

insert into ADestTable (FieldList) select FieldList from ASourcetabe

whereas fieldlist is

FieldName1, FieldName2, ...,

which must not identical by name for desttabe and sourcetable, just only the typ must match

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6254732
about different databases you never said this before

insert into ::aliasname1::adesttable (fieldlist) select fieldlist ::aliasname2::asourcetable

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6254737
oops, from missed on my last comment

insert into ::aliasname1::adesttable (fieldlist) select fieldlist from ::aliasname2::asourcetable

0
 

Author Comment

by:sorentop
ID: 6254766
Read my third line, I think it says so.

The tables have 100% the same structure, same fieldname and type.

What does the double colon's mean? Should they mean a param with : around it. TQuery says there aren't any params in the SQL statement.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6254789
well you're right overlooked this ;-)

:: shows the bde that an aliasname follows,

you can also use double quotes like

"aliasname.tablename"

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6254794
as it the same structure, you could use

insert into ::aliasname1::adesttable select * from ::aliasname2::asourcetable

you can also append a wherecondition like

insert into ::aliasname1::adesttable select * from ::aliasname2::asourcetable where condition

whereas the where is pointed to asourcetable

meikl ;-)

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6258441
any results? applyable? problems?
0
 

Author Comment

by:sorentop
ID: 6262566
If I enter this into a Query I get an error saying that it can't find the table

insert into "Alias1.TableName"
select * from "Alias2.TableName"
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6264618
? try the :: syntax
0
 

Author Comment

by:sorentop
ID: 6264654
have done give's me the same error
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6265096
did the table on both sides exists?
0
 

Author Comment

by:sorentop
ID: 6266262
yes of course :)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6266432
? hmm, it works for me

which database do you use?
(i've tested it with paradox->interbase and vice versa)

meikl ;-)
0
 

Author Comment

by:sorentop
ID: 6267059
interbase
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 150 total points
ID: 6280267
works fine to me too
interbase<->interbase

(don't use a databasename in the query)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 120
Dev Express grid collapse 2 39
Controlled Assessment GCSE - desperate help needed 4 75
CheckListBox usage 3 51
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now