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
Solved

SQL Query

Posted on 2002-06-05
11
164 Views
Last Modified: 2010-04-04
I have a series of querys in my application that retieve data base on certain conditions.

I have a selection of records displayed in a grid that have been retrieved by a SQL query.

I want to copy/insert these records and all fields and values into a new table.
I need to copy the data into a table but only if a certain condition is met.

Example:

Copy all records that have been retrieved in the grid into my new table but only if they meet a True condition in a field called Flag.

Is it possible to do this in SQL or any other way.
Many Thanks

0
Comment
Question by:HotRod40
11 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7056622
yes, depending on your database this statement sceleton should do it

for a existing empty table
insert into tablename (maybefieldlist_is_optional) select fieldlist from othertablename

for a new table
create table tablename as select fieldlist from othertable

use the execsql-method to fire the sql-statement


there is also a component-method (batchmove)
you need a ttable
fill in the databasename at runtime
fill in the tablename at runtime
use a code like

table1.batchmove(query1.batCopy);

hope this helps

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 75 total points
ID: 7056629
oops typo
table1.batchmove(query1.batCopy);
should be
table1.batchmove(query1,batCopy);


0
 

Author Comment

by:HotRod40
ID: 7056667
If I used your code below:

'for a existing empty table
insert into tablename (maybefieldlist_is_optional) select fieldlist from othertablename'

can I use a Where clause as I only need to copy the records that match a criteria?

Thanks
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7056733
sure you can :)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7056737
sure you can :)

insert into tablename (maybefieldlist_is_optional) select fieldlist from othertablename where condition(s)

only a order by is mostly not allowed

0
 
LVL 10

Expert Comment

by:Jacco
ID: 7057097
depending on the SQL server you use you could also construct a query like this (MSSQL):

insert into tblTwo
(fldOne, fldTwo)
select
  fldOne, fldTwo
from
  tblOne
where
  etc etc

this is also possible for update queries

Regards Jacco
 
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7057554
looks like this, jacco

insert into tablename (maybefieldlist_is_optional) select fieldlist from othertablename where condition(s)

meikl ;-)
0
 

Author Comment

by:HotRod40
ID: 7103292
Thanks Meikl but I have used this SQL query and I receive an Invalid Parameter error when running.

Insert Into SagePost(ContractNumber,
CustomerName, AccountRef, FeeType, MonthlyFee,
JoiningFee, ContractStartDate, ContractEndDate)

Select Contract.'Contract Number', Customer.'Customer Name', Customer.'Acc Ref', Contract.'Fee Type', Contract.'Monthly Fee'Contract.'Joining Fee', Contract.'Contract Start Date',Contract.'Contract End Date'
From Contract, Customer
Where Customer.'Contract Number' = Contract.'Contract Number' and
Contract.'FirstInvoice' = False and Contract.'COption' is null

Any Ideas?

Thanks
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7103301
Insert Into SagePost(
  ContractNumber,
  CustomerName,
  AccountRef,
  FeeType,
  MonthlyFee,
  JoiningFee,
  ContractStartDate,
  ContractEndDate)
Select
  Contract.'Contract Number',
  Customer.'Customer Name',
  Customer.'Acc Ref',
  Contract.'Fee Type',
  Contract.'Monthly Fee', <--- comma missed here
  Contract.'Joining Fee',
  Contract.'Contract Start Date',
  Contract.'Contract End Date'
From
  Contract,
  Customer
Where Customer.'Contract Number' = Contract.'Contract Number'
  and Contract.'FirstInvoice' = False
  and Contract.'COption' is null

was this a typo posting here or is the comma really missed?

meikl ;-)
0
 

Author Comment

by:HotRod40
ID: 7103658
The comma was actually missing. Thanks for that.
But even with the comma in place I still receive the error message 'Invalid Parameter'.
This message even occurs with this query:

INSERT INTO SagePost
SELECT CONTRACT.'CONTRACT NUMBER'
FROM CONTRACT;

Any ideas? or is there a help file anywhere with error code explanations?

Thanks again
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7103699
hmm,
just guessing,

you have a blank in your fieldname,
does this change help?

INSERT INTO SagePost
SELECT "CONTRACT.CONTRACT NUMBER"  //or CONTRACT."CONTRACT NUMBER"
FROM CONTRACT;

singlequotes are on some databases
interpreted as string-value rather than as fieldname

but, just guessing

meikl ;-)

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

790 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