Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy column value from one database.table to another database.table

Let's keep it short and sweet

I want to do this (I've failed numerous times now, and one attempt even updated the Row with blanks):

UPDATE Database2.Table1 
SET (Database2.Table1.Column1, Database2.Table1.Column2, Database2.Table1.Column3) 
VALUES 
   (Database1.Table1.Column1, Database1.Table1.Column2, Database1.Table1.Column3)
WHERE Database2.Table1.Column1 = Database1.Table1.Column1

These two already contain the same value, but the other 2 columns do not, and that is what I wish to change with this query..

Here's how each table look:

Database1.Table1

[id]
[name]
[applicationdate]
[startdate]
[shortdescription]
[longdescription]
[displayimg]
[contact]
[website]
[created]
[urlbase]
[site]
[keywords]
[type]
[location]

Database2.Table1

[id]
[name]
[applicationdate]
[startdate]
[content]
[keywords]
[customerid]
[urlbase]
[shortdescription]
[meta]
[type]
[site]
[searchurlbase]
[lang]
[educationlength]
[locations]
[educationwebsite]
[contact]
[tags]
[educationtypes]
[created]
[category]

Any help is greatly appreciated, thank you for your time :)

Sorry if it doesn't make any sense, I easily get confused when trying to explain problems I'm having

like image 854
Jesper Avatar asked Apr 15 '13 14:04

Jesper


People also ask

How do I copy a column data from one table to another in SQL?

Using SQL Server Management StudioOpen the table with columns you want to copy and the one you want to copy into by right-clicking the tables, and then clicking Design. Click the tab for the table with the columns you want to copy and select those columns. From the Edit menu, click Copy.

How do I copy data from one table of one database to another table of another database in SQL?

Select and right-click on the Source Database, go to Tasks > Export Data. Import/Export Wizard will be opened and click on Next to proceed. Enter the data source, server name and select the authentication method and the source database. Click on Next.

Can we copy data from one table to another table?

If you want to copy the data of one SQL table into another SQL table in the same SQL server, then it is possible by using the SELECT INTO statement in SQL. The SELECT INTO statement in Structured Query Language copies the content from one existing table into the new table.


1 Answers

UPDATE dest
  SET column2 = src.column2,
      column3 = src.column3
FROM Database2.dbo.Table1 AS dest 
INNER JOIN Database1.dbo.Table1 AS src
ON dest.column1 = src.column1;

Given your collate problem, you can specify collate in the equality operation. Having no idea which side is giving the problem, and assuming you don't care about case sensitivity:

UPDATE dest
  SET column2 = src.column2,
      column3 = src.column3
FROM Database2.dbo.Table1 AS dest 
INNER JOIN Database1.dbo.Table1 AS src
ON dest.column1 COLLATE Finnish_Swedish_CI_AS
 = src.column1  COLLATE Finnish_Swedish_CI_AS;

If you care about case sensitivity, then change both clauses to the _CS_AS_ one.

like image 123
Aaron Bertrand Avatar answered Nov 16 '22 04:11

Aaron Bertrand