Moving data from T1 in Database D1 to T1 in Database D2 - sql-server

I have two databases: D1 and D2.
D1 has Table Basic_Details; D2 also has a table Basic_details. Both The Basic_details tables have the same design.
D1 has data. So how do I copy all the data from Table in D1 to the Table in D2?

Assuming you are using SQL Server, you should be able to run
INSERT INTO D2.Basic_details SELECT * FROM D1.Basic_details
Note: if you table has identity field, you will need to enable identity inserts.
Update
INSERT INTO D2.dbo.Basic_details SELECT * FROM D1.dbo.Basic_details
or
INSERT INTO D2..Basic_details SELECT * FROM D1..Basic_details

Related

How do you save a join as a Snowflake table?

I understand how a Join is made in Snowflake but I need to temporarily save it as a table, how can it be possible make it using consola?
(For this project I can't do it with any connectors)
You are looking for the CREATE TABLE AS SELECT or CTAS pattern:
CREATE TABLE tmp_table AS
SELECT a.*, b.*
FROM (SELECT * FROM VALUES ('a'),('b')) a(string)
CROSS JOIN (SELECT * FROM VALUES (1),(2)) b(vals);
SELECT * FROM tmp_table;
gives:
STRING
VALS
a
1
a
2
b
1
b
2
and if you want it to be temporary for the session you might want to add TEMP
CREATE TEMP TABLE tmp_table AS

Copy data from table and insert in different SQL Server database

I have two databases
databaseone
databasetwo
and I have a similar table in the database, name of the table is tableemployeedetails.
In my databaseone, I have 500 columns in the table tableemployeedetails.
In my databasetwo, I have 10 columns in the table tableemployeedetails.
I cannot use insert into select query because I want to insert the data into different database.
What is the best way to do this in my situation?
I just want to merge tableemployeedetails in both the databases
Try this,
insert into databasetwo..tableemployeedetails
SELECT * FROM databaseone..tableemployeedetails A
WHERE NOT EXISTS (SELECT 1 FROM databasetwo..tableemployeedetails B
WHERE A.COLUMN=B.COLUMN
)
If both databases has different records then you need two insert statements as below. If they have same then you need to prefer which database records are latest and then write an update in addition to the below insert.
insert into databasetwo..tableemployeedetails
SELECT * FROM databaseone..tableemployeedetails d1
left join databasetwo..tableemployeedetails d2 on A.PKKEY=B.PKKEY
where d2.PKKEY is null
insert into databaseone..tableemployeedetails
SELECT * FROM databasetwo..tableemployeedetails d2
left join databaseone..tableemployeedetails d1 on A.PKKEY=B.PKKEY
where d1.PKKEY is null

How to Bulk Insert from a select query?

I want to Bulk Insert the result of this query into a table. What should I do?
Here is my query :
select *
from table1 t1
inner join table2 t2 on t1.code = t2.stucode
bulk insert is not the term you are looking for. bulk insert imports a data file. You are not importing a data file.
You are looking to create a table based on the results of a query. Which is easily done with select ... into.
select *
into t3
from table1 t1
inner join table2 t2 on t1.code=t2.stucode
Alternatively, you can create the table first, and use the minimally logged bulk load operation of insert ... select with trace flag 610 for clustered indexes, or no trace flag required for heaps. Find more about the other limitations to this here:
The Data Loading Performance Guide

save union (merging) command directly in new database

Does anyone have an idea how if we could get the results of a "union" sqlite3 command and stock the returning database directly in a new database object?
For ex.:
we have table 1 (t1) in database 1 and table 2 (t2) in database 2.
Command tool:
sqlite> select * from t1 union select * from t2;
It prompts the new table in the command line tool but I cannot figure out how to stock the results in a new database?
CREATE TABLE t3(...);
INSERT INTO t3 SELECT * FROM t1 UNION SELECT * FROM t2;
Or if you don't care about column types and constraints:
CREATE TABLE t3 AS SELECT * FROM t1 UNION SELECT * FROM t2;

UPDATE one table based on values in another table

I have two tables, Table 1 has three fields (a1,a2,a3) and table 2 has three fields (b1,b2,b3) I want to update table one base on table two when a1=b1 . This is essay in Microsoft access but I want t to do it in Microsoft Access project Sql server 2005 as a back end.
UPDATE t1
SET a1 = t2.b1, a2 = t2.b2, a3 = t2.b3
where a1=b1
INNER JOIN the tables. Then you won't need to SET t1.a1=t2.b1 because those are already equal based on the join condition. So just SET the other two field values ...
UPDATE t1 INNER JOIN t2
ON t1.a1=t2.b1
SET t1.a2 = t2.b2, t1.a3 = t2.b3
Note this suggestion is Access SQL and should work from MS Access regardless of whether t1 and t2 are native Access tables, links to SQL Server tables, or a mix of both types.
Try the following for SQL Server:
UPDATE t1
SET a1 = t2.b1, a2 = t2.b2, a3 = t2.b3
FROM t1
INNER JOIN t2 ON t1.a1 = t2.b1

Resources