Home > Archive > Microsoft SQL Server forum > September 2005 > Insert based on a select query









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Insert based on a select query
Mintyman

2005-09-20, 9:23 am

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


Jens

2005-09-20, 9:23 am

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.

Mintyman

2005-09-20, 9:23 am

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" <Jens@sqlserver2005.de> wrote in message
news:1127219694.666930.255500@f14g2000cwb.googlegroups.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.
>



Jens

2005-09-20, 9:23 am

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.

--CELKO--

2005-09-21, 7:23 am

>> 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..

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com