Solved

SQL Query

Posted on 2002-06-05
11
162 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
 
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
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 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

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

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 The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

929 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

10 Experts available now in Live!

Get 1:1 Help Now