Solved

SQL Query

Posted on 2002-06-05
11
160 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
Comment Utility
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
Comment Utility
oops typo
table1.batchmove(query1.batCopy);
should be
table1.batchmove(query1,batCopy);


0
 

Author Comment

by:HotRod40
Comment Utility
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
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
sure you can :)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:Jacco
Comment Utility
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
Comment Utility
looks like this, jacco

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

meikl ;-)
0
 

Author Comment

by:HotRod40
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

14 Experts available now in Live!

Get 1:1 Help Now