dbTalk Databases Forums  

simple SQL question to insert records in an existing table - Newbie

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss simple SQL question to insert records in an existing table - Newbie in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
pamela fluente
 
Posts: n/a

Default simple SQL question to insert records in an existing table - Newbie - 02-20-2007 , 09:31 AM







Hi, I would like to know how to modify the following selects
so that - assuming that NewTable already exists - the result of the
SELECT is appended to the existing table.

If possible I need the appropriate changes for both the 2 syntax
below.

-1-
SELECT * INTO NewTable FROM (
SELECT
S.OrderID AS "OrderID",
S.CustomerID AS "CustomerID"
FROM
Source1 S
) tmp

-2-
CREATE TABLE NewTable
SELECT
S.OrderID AS "OrderID",
S.CustomerID AS "CustomerID"
FROM
Source1 S


Thank you very much for any help,

Pam


Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: simple SQL question to insert records in an existing table - Newbie - 02-20-2007 , 10:40 AM






To insert into a table that already exists you use INSERT INTO. In your case
it will be something like this:

INSERT INTO NewTable (OrderID, CustomerID)
SELECT OrderID, CustomerID
FROM Source1

This assumes table NewTable already exists. If the table does not exist you
can create it with SELECT INTO like this:

SELECT OrderID, CustomerID
INTO NewTable
FROM Source1

HTH,

Plamen Ratchev
http://www.SQLStudio.com




Reply With Quote
  #3  
Old   
jim_geissman@countrywide.com
 
Posts: n/a

Default Re: simple SQL question to insert records in an existing table - Newbie - 02-20-2007 , 02:15 PM



You can also create a table using SELECT INTO
without putting anything in it ---

SELECT OrderID, CustomerID
INTO NewTable
FROM SourceTable
WHERE 1=2


Reply With Quote
  #4  
Old   
pamela fluente
 
Posts: n/a

Default Re: simple SQL question to insert records in an existing table - Newbie - 02-22-2007 , 01:25 PM



Thank you all. Very helpful.

the CREATE TABLE syntax is more an Oracle thing. Right?

I was wondering if the
SELECT INTO
INSERT INTO
syntax is also accepted by Oracle or it needs adjustments ?

-Pam




Reply With Quote
  #5  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: simple SQL question to insert records in an existing table - Newbie - 02-22-2007 , 02:02 PM



"pamela fluente" <pamelafluente (AT) libero (DOT) it> wrote

Quote:
Thank you all. Very helpful.

the CREATE TABLE syntax is more an Oracle thing. Right?

Yes, using CREATE TABLE to create a table from another table works in Oracle
but not in SQL Server. In Oracle you can write:

CREATE TABLE NewTable AS
SELECT OrderID, CustomerID
FROM Source1

The equivalent in SQL Server is:

SELECT OrderID, CustomerID
INTO NewTable
FROM Source1

Quote:
I was wondering if the
SELECT INTO
INSERT INTO
syntax is also accepted by Oracle or it needs adjustments ?
There is SELECT INTO in Oracle but has different meaning. The INTO clause is
used to direct the result of the SELECT to a variable list or a record.

INSERT INTO in its basic form works the same way in Oracle and SQL Server.
Oracle has more variations of the syntax.

Quote:
-Pam



HTH,

Plamen Ratchev
http://www.SQLStudio.com




Reply With Quote
  #6  
Old   
pamela fluente
 
Posts: n/a

Default Re: simple SQL question to insert records in an existing table - Newbie - 02-26-2007 , 02:22 AM



On 22 Feb, 21:02, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
"pamela fluente" <pamelaflue... (AT) libero (DOT) it> wrote in message

news:1172172321.965386.210210 (AT) h3g2000cwc (DOT) googlegroups.com...

Thank you all. Very helpful.

the CREATE TABLE syntax is more an Oracle thing. Right?

Yes, using CREATE TABLE to create a table from another table works in Oracle
but not in SQL Server. In Oracle you can write:

CREATE TABLE NewTable AS
SELECT OrderID, CustomerID
FROM Source1

The equivalent in SQL Server is:

SELECT OrderID, CustomerID
INTO NewTable
FROM Source1

I was wondering if the
SELECT INTO
INSERT INTO
syntax is also accepted by Oracle or it needs adjustments ?

There is SELECT INTO in Oracle but has different meaning. The INTO clause is
used to direct the result of the SELECT to a variable list or a record.

INSERT INTO in its basic form works the same way in Oracle and SQL Server.
Oracle has more variations of the syntax.



-Pam

HTH,

Plamen Ratchevhttp://www.SQLStudio.com
Thank you. Very useful.

-Pam




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.