How to manually move rows from one database to the next? - sql

I have two databases on the same server. One db is newer than the other and has had its schema modified quite a bit. I want to transfer data from a table in the old db to a table in the new db but I need total control over the process so I can mold the old data to fit the new schema.
[NewDB].[dbo].[Aliases]
[OldDB].[Terminal].[Alias]
I'm not very adept at SQL yet. Is there a way I can loop through all the records in the old table and then on each iteration of the loop craft a custom insert statement for the new table?

Try an INSERT SELECT statement.
INSERT INTO [NewDB].[dbo].[Aliases]
SELECT columns
FROM [OldDB].[Terminal].[Alias]
http://msdn.microsoft.com/en-us/library/ms174335(v=sql.100).aspx - derived table section
http://msdn.microsoft.com/en-us/library/ms189499(v=sql.100).aspx

Do it the same way as you would if both tables were in the same database. Just fully qualify the names of the tables. Example:
INSERT INTO [NewDB].[dbo].[Aliases] (col1, col2, col3)
SELECT LEFT(col1,3), col2, col3 FROM [OldDB].[Terminal].[Alias]

Depending on the data size you wish to transfer, you might want to consider using BULK INSERT: http://msdn.microsoft.com/en-us/library/ms188365.aspx

INSERT INTO NEWDB..TABLENAME( *fieldlist* )
SELECT *fieldlist* FROM OLDDB..TABLENAME

Related

how to copy only data from one database table to another database existing table in sql server

how to copy only data from one database table to another database existing table in sql server query?
Copy one database existing table to another database existing table.
anyone know, Please tell me Sql query
Open image
insert into <target table name>(columns)
select columns
from <source table name>
Try this ...
INSERT INTO DataBase2.dbo.table2
SELECT * FROM DataBase1.dbo.table1
Generate scripts in SQL Server Management Studio from one database's table and run the generated script in another database.
Broadly speaking:
INSERT INTO [databaseName].[schemaName].[table2]
SELECT * FROM [databaseName].[schemaName].[table1]
If you are more specific in your question, I can provide a more detailed answer.
I suppose you want to copy data from all columns keeping the same order in two tables.
If both tables exist in your database use
insert into targetTable
select * from sourceTable
If not, use
select * into targetTable
from sourceTable
In the second case, the targetTable will inherit the datatypes from the sourceTable (not the keys, indexes, etc..)

Best way to import a large dataset into Oracle sql database?

I'm new to oracle Database and I'm making a very simple bilingual dictionary ( 2 tables - English and French).
A dictionary often contains thousands of words. My question is that instead of writing thousand of [INSERT INTO ... VALUES] commands, is there any better way to somehow automate the process, like making a import form? Since the book my school provides only mentions about the INSERT command.
You can use a combination of a spreadsheet and an INSERT ALL command to quickly generate an SQL INSERT command to insert your data.
If you have your data in a spreadsheet format, you can use formulas to construct an INSERT statement for each row.
However, you could use the INSERT ALL syntax instead of the INSERT (single row) syntax.
To do this:
Add a column into your spreadsheet with the formula that looks like this: "INTO table (col1, col2, col3) VALUES ('val1', 'val2', 'val3')". You would need to use concatenation to add the values into this formula.
So your formula may look like this (assuming columns of A, B, and C):
="INTO table (col1, col2, col3) VALUES ('"&A2&"', '"&B2&"', '"&C2&"')"
Copy the formula to each row.
Copy and paste all of these formulas to a query window inside your IDE.
Add the words INSERT ALL at the start of your command
Add the words SELECT * FROM dual; at the end of your command
Your command would then look like this:
INSERT ALL
INTO table (col1, col2, col3) VALUES ('val1', 'val2', 'val3')
INTO table (col1, col2, col3) VALUES ('val1a', 'val2a', 'val3a')
INTO table (col1, col2, col3) VALUES ('val1b', 'val2b', 'val3b')
SELECT * FROM dual;
This will insert all records in a single statement and is likely to be much faster than hundreds or thousands of INSERT statements.
Alternatively, you could use a tool like Data Pump Import and Export, but I have limited experience with that so perhaps another user could elaborate on that.
If you have your data in a file, you can use the UTL_FILE package to read, parse and load your data.
Oracle ULT_FILE package
You can use Oracle's external table feature as well. See here
Oracle External tables
Oracle's Sql Loader product is great for quickly loading large amounts of data.

SQL Server INSERT trigger how to get data of current row

I've never created a trigger before and I'm trying to read online but am a little confused.
I want to create a trigger on a table that on insert, it will grab some data from different columns and insert it into a few different other tables.
I'm not sure how to write the T-SQL to get the data from the columns..
insert into [othetTable]
values ([col1 from row that was inserted], [col5 from row that was inserted])
What would the syntax be to get those values?
thanks
Use the inserted virtual table that is available to triggers. Note that there could be multiple rows in this table - your trigger could be processing multiple inserts at once.
Therefore, you need to use something like the following syntax:
insert into othertable
select col1, col5
from inserted
This will insert a row into othertable for each inserted row.

"select * into table" Will it work for inserting data into existing table

I am trying to insert data from one of my existing table into another existing table.
Is it possible to insert data into any existing table using select * into query.
I think it can be done using union but in that case i need to record all data of my existing table into temporary table, then drop that table and finally than apply union to insert all records into same table
eg.
select * into #tblExisting from tblExisting
drop table tblExisting
select * into tblExisting from #tblExisting union tblActualData
Here tblExisting is the table where I actually want to store all data
tblActualData is the table from where data is to be appended to tblExisting.
Is it right method.
Do we have some other alternative ?
You should try
INSERT INTO ExistingTable (Columns,..)
SELECT Columns,...
FROM OtherTable
Have a look at INSERT
and SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
No, you cannot use SELECT INTO to insert data into an existing table.
The documentation makes this very clear:
SELECT…INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it.
You generally want to avoid using SELECT INTO in production because it gives you very little control over how the table is created, and can lead to all sorts of nasty locking and other performance problems. You should create schemas explicitly and use INSERT - even for temporary tables.
#Ryan Chase
Can you do this by selecting all columns using *?
Yes!
INSERT INTO yourtable2
SELECT * FROM yourtable1
Update from CTE? http://www.sqlservercentral.com/Forums/Topic629743-338-1.aspx

Copy Data from a table in one Database to another separate database

Basically I have a two databases on SQL Server 2005.
I want to take the table data from one database and copy it to another database's table.
I tried this:
SELECT * INTO dbo.DB1.TempTable FROM dbo.DB2.TempTable
This didn't work.
I don't want to use a restore to avoid data loss...
Any ideas?
SELECT ... INTO creates a new table. You'll need to use INSERT. Also, you have the database and owner names reversed.
INSERT INTO DB1.dbo.TempTable
SELECT * FROM DB2.dbo.TempTable
SELECT * INTO requires that the destination table not exist.
Try this.
INSERT INTO db1.dbo.TempTable
(List of columns here)
SELECT (Same list of columns here)
FROM db2.dbo.TempTable
It's db1.dbo.TempTable and db2.dbo.TempTable
The four-part naming scheme goes:
ServerName.DatabaseName.Schema.Object
Hard to say without any idea what you mean by "it didn't work." There are a whole lot of things that can go wrong and any advice we give in troubleshooting one of those paths may lead you further and further from finding a solution, which may be really simple.
Here's a something I would look for though,
Identity Insert must be on on the table you are importing into if that table contains an identity field and you are manually supplying it. Identity Insert can also only be enabled for 1 table at a time in a database, so you must remember to enable it for the table, then disable it immediately after you are done importing.
Also, try listing out all your fields
INSERT INTO db1.user.MyTable (Col1, Col2, Col3)
SELECT Col1, COl2, Col3 FROM db2.user.MyTable
We can three part naming like database_name..object_name
The below query will create the table into our database(with out constraints)
SELECT *
INTO DestinationDB..MyDestinationTable
FROM SourceDB..MySourceTable
Alternatively you could:
INSERT INTO DestinationDB..MyDestinationTable
SELECT * FROM SourceDB..MySourceTable
If your destination table exists and is empty.
Don't forget to insert SET IDENTITY_INSERT MobileApplication1 ON to the top, else you will get an error. This is for SQL Server
SET IDENTITY_INSERT MOB.MobileApplication1 ON
INSERT INTO [SERVER1].DB.MOB.MobileApplication1 m
(m.MobileApplicationDetailId,
m.MobilePlatformId)
SELECT ma.MobileApplicationId,
ma.MobilePlatformId
FROM [SERVER2].DB.MOB.MobileApplication2 ma
Im prefer this one.
INSERT INTO 'DB_NAME'
(SELECT * from 'DB_NAME#DB_LINK')
MINUS
(SELECT * FROM 'DB_NAME');
Which means will insert whatsoever that not included on DB_NAME but included at DB_NAME#DB_LINK. Hope this help.
INSERT INTO DB1.dbo.TempTable
SELECT * FROM DB2.dbo.TempTable
If we use this query it will return Primary key error.... So better to choose which columns need to be moved, like
INSERT INTO db1.dbo.TempTable // (List of columns here)
SELECT (Same list of columns here)
FROM db2.dbo.TempTable
Try this
INSERT INTO dbo.DB1.TempTable
(COLUMNS)
SELECT COLUMNS_IN_SAME_ORDER FROM dbo.DB2.TempTable
This will only fail if an item in dbo.DB2.TempTable is in already in dbo.DB1.TempTable.
This works successfully.
INSERT INTO DestinationDB.dbo.DestinationTable (col1,col1)
SELECT Src-col1,Src-col2 FROM SourceDB.dbo.SourceTable
You can copy one table to other db table even with some additional columns.
insert into [SchoolDb1].[dbo].Student(Col1, Col2,Col3, CreationTime, IsDeleted)
select Col1, Col2,Col3,,getdate(),0 from [SchoolDb2].[dbo].Student
These are additional columns: (CreationTime is datatime and IsDeleted is boolean)
select * from DBA1.TABLENAMEA;
create table TABLENAMEA as (select * from DBA1.TABLENAMEA);
These manual way provides more flexibility, but at the same time, works for table whose size is smaller to few thousands.
Do select * from <table name> from DB, once whole table is displayed, scroll till it's bottom.
Right click and do Export table as Insert statement, provide the name of the destination table and export the table as .sql file.
Use any text editor to further do regular find and replace operation to include more column names etc.
Use the INSERT statement in destination DB.