473,508 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert based on a select query

Hi,

I have a SQL query:

select products.name, products.notes, products.purchase_date,
products.serial_no, products.total_value, products.product_code,
products.quantity, products.product_group, products.lhs1_name,
lhs1.department, lhs1.address1, lhs1.city, lhs1.country
from products, lhs1
where products.lhs1_id = lhs1.id

I want to insert the results of this query into a table called 'temp' in the
database. I used to copy and paste this into excel then import it but it
doesn't always work.

Is there a way to do it all in a SQL script. Please be aware that my
knowledge of SQL is fairly basic so please explain things clearly.

Thanks,

Darren
Sep 20 '05 #1
4 13737
INSERT INTO Temp
select products.name, products.notes, products.purchase_date,
products.serial_no, products.total_value, products.product_code,
products.quantity, products.product_group, products.lhs1_name,
lhs1.department, lhs1.address1, lhs1.city, lhs1.country
from products, lhs1
where products.lhs1_id = lhs1.id

if it does not exists at runtime:

SELECT
products.name, products.notes, products.purchase_date,
products.serial_no, products.total_value, products.product_code,
products.quantity, products.product_group, products.lhs1_name,
lhs1.department, lhs1.address1, lhs1.city, lhs1.country
from products, lhs1
where products.lhs1_id = lhs1.id
INTO Temp
HTH, Jens Suessmeyer.

Sep 20 '05 #2
Hi Jens,

Thanks for the quick reply. Your help is much appreciated. What would I need
to do in order to insert the results into a table called 'temp' that resides
in a different database e.g. Logi?

Thanks

"Jens" <Je**@sqlserver2005.de> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
INSERT INTO Temp
select products.name, products.notes, products.purchase_date,
products.serial_no, products.total_value, products.product_code,
products.quantity, products.product_group, products.lhs1_name,
lhs1.department, lhs1.address1, lhs1.city, lhs1.country
from products, lhs1
where products.lhs1_id = lhs1.id

if it does not exists at runtime:

SELECT
products.name, products.notes, products.purchase_date,
products.serial_no, products.total_value, products.product_code,
products.quantity, products.product_group, products.lhs1_name,
lhs1.department, lhs1.address1, lhs1.city, lhs1.country
from products, lhs1
where products.lhs1_id = lhs1.id
INTO Temp
HTH, Jens Suessmeyer.

Sep 20 '05 #3
Refering to the four part notation this is gernerally:

[servername].[databasename].[owner].[objectname]

SELECT
products.name, products.notes, products.purchase_date,
products.serial_no, products.total_value, products.product_code,
products.quantity, products.product_group, products.lhs1_name,
lhs1.department, lhs1.address1, lhs1.city, lhs1.country
from products, lhs1
where products.lhs1_id = lhs1.id
INTO logi..Temp

HTH, Jens Suessmeyer.

Sep 20 '05 #4
>> Products.lhs1_id = lhs1.id <<

Why did a data element change names from table to table? Why do so
many of your data elements violate the rules of data modeling and
ISO-11179?

I would bet that this magical table, Temp, is not needed but is used to
mimic a scratch tape in a procedural solution. It is a common Newbie
error. You probably ought to get some help on the entire schema.

Otherwise, go to BOL and look up INSERT INTO .. SELECT..

Sep 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5502
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
3
13156
by: traceable1 | last post by:
I am inserting data rows into a table via a stored procedure. After the inserts, I query the rows in the table and I want them to spit back out in the same order I put them in. However,...
0
3119
by: jtocci | last post by:
I'm having a big problem with CREATE RULE...ON INSERT...INSERT INTO...SELECT...FROM...WHERE when I want to INSERT several (20~50) records based on a single INSERT to a view. Either I get a 'too...
16
16975
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
52069
by: maltchev | last post by:
i need to insert data from an xml file into sql server table. the xml file contains only one record. how to insert the data? how to map the names of the fields in the xml file and the table?...
6
3088
by: efgh | last post by:
I'm fairly confident in my knowledge of SQL but I'm stumped with regards with an Insert Into statement in Access 2003. I've got a 4 column table that I've simplified about as much as I can, no...
10
2941
by: Serdge Kooleman | last post by:
i have a query: select id, name from Dictionary field "id" is guid (so not null, and not identity) i show only "name" in the dataGrid how to set new id when i insert a new record? ...
8
3487
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
3
45310
by: weird0 | last post by:
I have two tables accounts and ATM and i am trying to insert a tuple in ATM with accountId as foreign key. But even this simple work,I encounter the following error: The INSERT statement...
2
3728
by: lenygold via DBMonster.com | last post by:
Hi Everebody: I have a table: CREATE TABLE CROSS_REFERENCE (ROW# INTEGER NOT NULL ,KEY_WORD CHAR(16) NOT NULL ,QUERY_DESCR VARCHAR(330) NOT NULL ,PRIMARY KEY (ROW#,KEY_WORD)); It is a...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.