?
Solved

SQL Query

Posted on 2002-06-05
11
Medium Priority
?
168 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
[X]
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
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 225 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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