Create new tables from old tables [duplicate] - sql-server

This question already has answers here:
How to create table using select query in SQL Server?
(4 answers)
Closed 9 years ago.
I want to create a table from select query result in SQL Server, I tried
create table temp AS select.....
but I got an error
Incorrect syntax near the keyword 'AS'

Use following syntax to create new table from old table in SQL server 2008
Select * into new_table from old_table

use SELECT...INTO
The SELECT INTO statement creates a new table and populates it with
the result set of the SELECT statement. SELECT INTO can be used to
combine data from several tables or views into one table. It can also
be used to create a new table that contains data selected from a
linked server.
Example,
SELECT col1, col2 INTO #a -- <<== creates temporary table
FROM tablename
Inserting Rows by Using SELECT INTO
Standard Syntax,
SELECT col1, ....., col# -- <<== select as many columns as you want
INTO [New tableName]
FROM [Source Table Name]

Please be careful,
MSSQL: "SELECT * INTO NewTable FROM OldTable"
is not always the same as
MYSQL: "create table temp AS select.."
I think that there are occasions when this (in MSSQL)
does not guarantee that all the fields in the new table are of the same type as the old.
For example :
create table oldTable (field1 varchar(10), field2 integer, field3 float)
insert into oldTable (field1,field2,field3) values ('1', 1, 1)
select top 1 * into newTable from oldTable
does not always yield:
create table newTable (field1 varchar(10), field2 integer, field3 float)
but may be:
create table newTable (field1 varchar(10), field2 integer, field3 integer)

Please try:
SELECT * INTO NewTable FROM OldTable

Try using SELECT INTO....
SELECT ....
INTO TABLE_NAME(table you want to create)
FROM source_table

Select [Column Name] into [New Table] from [Source Table]

Related

Copy data from one table into another new table where columns are unknown

I'm trying to copy all data from Table1 into Table2.
I don't know what and how many columns are their in table 1. I mean I want to copy even column names from table 1 to table 2.
There is option like
insert *
into #table2
from Table1
but I even can't use this because there are many select query which has already been written at past. So I have to do something like this.
insert *
into #table2
from (select * from Table1)
This is throwing an error
Incorrect syntax near )
Try This:
Select * into #table2 from (select * from table1 ) as X
---To copy along with data..
select * into newtable from oldtable
--to copy only schema..
select * into newtable from oldtable where 1=2

Pass Column value to parameter in Dataflow task SSIS

I have a SQL table TABLE1 which has columns ID and LastModifiedDate.Now I have one oracle query SELECT * From Table where NEWID =? where parameter will be the value from ID column from TABLE1 and I need to insert these records into a destination table on a SQL Server.Please advise the best approach for this.I am using SQL 2008.
Where #Id will be parameter
INSERT INTO DESTINATIONTABLE
SELECT *
FROM TABLE
WHERE NEWID IN(SELECT ID From Table1 WHERE ID=#ID)
or if you want to specify the columns you could use
INSERT INTO DESTINATIONTABLE(Col1,Col2)
SELECT Col1,Col2
FROM TABLE
WHERE NEWID IN(SELECT ID From Table1 WHERE ID=#ID)

How to get ID from one table and associate with record in another table in SQL Server

I've tried searching for the answer to this one to no avail. There is no good logic behind the way this was setup. The guy does not know what he's doing, but it's what I have to work with (long story).
I'm using SQL Server 2008R2 I need to take records from one table and transfer the data to 4 separate tables all with a one to one relationship (I know - not smart). I need to get the value from the Identity field in the first table the data is inserted into, then populate the other 3 tables with the same ID and disperse the data accordingly. for example:
OldTable: Field1, Field2, Field3, Field4
NewTable1: Identity field, Field1
NewTable2: ID, Field2
NewTable3: ID, Field3
NewTable4: ID, Field4
I'd like to handle this in a stored procedure. I'd like to do a loop, but I read that loops in SQL are inadvisable.
Loop moving through each record in OldTable... (??)
INSERT INTO NewTable1
(Field1)
Select Field1 from OldTable
INSERT INTO NewTable2
(ID, Field2)
Select SCOPE_IDENTITY?, Field2 From OldTable Where OldTable.ID = ??
etc for other 2 tables
Loop to next record in OldTable
I am not sure how to use SCOPE_IDENTITY, but I have a feeling this will be involved in how I accomplish this.
Also, I'm probably going to need to setup a trigger for whenever a new record is created in NewTable1. I know, it's insanity, but I can't do anything about it, just have to work around it.
So, I need to know
1: the best way to initially populate the tables
2: how to make triggers for new records
The solution to 1 might involve 2.
Please help!
You can use the output clause of the merge statement to get a mapping between the existing primary key in OldTable and the newly generated identity ID in NewTable1.
-- Temp table to hold the mapping between OldID and ID
create table #ID
(
OldID int primary key,
ID int
);
-- Add rows to NewTable1 and capture the ID's in #ID
merge NewTable1 as T
using OldTable as S
on 1 = 0
when not matched by target then
insert(Field1) values(S.Field1)
output S.ID, inserted.ID into #ID(OldID, ID);
-- Add rows to NewTable2 using #ID to get the correct value for each row
insert into NewTable2(ID, Field2)
select I.ID, O.Field2
from #ID as I
inner join OldTable as O
on I.OldID = O.ID
insert into NewTable3(ID, Field3)
select I.ID, O.Field3
from #ID as I
inner join OldTable as O
on I.OldID = O.ID
insert into NewTable4(ID, Field4)
select I.ID, O.Field4
from #ID as I
inner join OldTable as O
on I.OldID = O.ID
drop table #ID;
SQL Fiddle
See also Using merge..output to get mapping between source.id and target.id
How about using the OUTPUT clause of the insert statement? Assuming that Field1 is a unique key on the OldTable...
Declare #IDinserted table(ID int, Field1 varchar(255));
Insert Into NewTable1(Field1)
Output inserted.ID, inserted.Field1 into #IDinserted
Select OldID, Field1 from OldTable;
Insert Into NewTable2(RowID, Field2)
Select i.ID, o.#Field2
from #IDinserted i Inner Join OldTable o
on i.Field1=o.Field1;

SELECT INSERT INTO

Is it possible to insert data from select statement into a a dynamically created table? I will not know how many columns are there in select statement until I run the query. It has to create the appropriate number of columns at run time.
Thank you,
Smith
Just use the SELECT INTO syntax:
SELECT *
INTO NewTable
FROM MyTable
WHERE ...
The new table NewTable will be created with the same columns as your select statement.
You can use a CTAS pattern for this
USE AdventureWorks
GO
----Create new table and insert into table using SELECT INSERT
SELECT FirstName, LastName
INTO TestTable
FROM Person.Contact
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
----Clean Up Database
DROP TABLE TestTable
GO
Have a look at This Article on CTAS
-- This creates temporary table
select *
into #newtable
from YourTable
select * from #newtable
-- This creates physical table in the DB
select *
into newtable
from YourTable
select * from newtable

How to save select query results within temporary table?

I need to save select query output into temporary table. Then I need to make another select query against this temporary table. Does anybody know how to do it?
I need to make this on SQL Server.
select *
into #TempTable
from SomeTale
select *
from #TempTable
You can also do the following:
CREATE TABLE #TEMPTABLE
(
Column1 type1,
Column2 type2,
Column3 type3
)
INSERT INTO #TEMPTABLE
SELECT ...
SELECT *
FROM #TEMPTABLE ...
DROP TABLE #TEMPTABLE
In Sqlite:
CREATE TABLE T AS
SELECT * FROM ...;
-- Use temporary table `T`
DROP TABLE T;

Resources