Solved

SQL Query

Posted on 2002-06-05
11
163 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

822 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