Copy data from table and insert in different SQL Server database - sql-server

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

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

How to append data from one table to another table in Snowflake

I have a table of all employees (employees_all) and then created a new table (employees_new) with the same structure that I would like to append to the original table to include new employees.
I was looking for the right command to use and found that INSERT lets me add data as in the following example:
create table t1 (v varchar);
insert into t1 (v) values
('three'),
('four');
But how do I append data coming from another table and without specifying the fields (both tables have the same structure and hundreds of columns)?
With additional research, I found this specific way to insert data from another table:
insert into employees_all
select * from employees_new;
This script lets you append all rows from a table into another one without specifying the fields.
Hope it helps!
Your insert with a select statement is the most simple answer, but just for fun, here's some extra options that provide some different flexibility.
You can generate the desired results in a select query using
SELECT * FROM employees_all
UNION ALL
SELECT * FROM employees_new;
This allows you to have a few more options with how you use this data downstream.
--use a view to preview the results without impacting the table
CREATE VIEW employees_all_preview
AS
SELECT * FROM employees_all
UNION ALL
SELECT * FROM employees_new;
--recreate the table using a sort,
-- generally not super common, but could help with clustering in some cases when the table
-- is very large and isn't updated very frequently.
INSERT OVERWRITE INTO employees_all
SELECT * FROM (
SELECT * FROM employees_all
UNION ALL
SELECT * FROM employees_new
) e ORDER BY name;
Lastly, you can also do a merge to give you some extra options. In this example, if your new table might have records that already match an existing record then instead of inserting them and creating duplicates, you can run an update for those records
MERGE INTO employees_all a
USING employees_new n ON a.employee_id = n.employee_id
WHEN MATCHED THEN UPDATE SET attrib1 = n.attrib1, attrib2 = n.attrib2
WHEN NOT MATCHED THEN INSERT (employee_id, name, attrib1, attrib2)
VALUES (n.employee_id, n.name, n.attrib1, n.attrib2)

2 nvarchar fields are not matching though the data is same?

I want to join 2 tables using an Inner Join on 2 columns, both are of (nvarchar, null) type. The 2 columns have the same data in them, but the join condition is failing. I think it is due to the spaces contained in the column values.
I have tried LTRIM, RTRIM also
My query:
select
T1.Name1, T2.Name2
from
Table1 T1
Inner Join
Table2 on T1.Name1 = T2.Name2
I have also tried like this:
on LTRIM(RTRIM(T1.Name1)) = LTRIM(RTRIM(T2.Name2))
My data:
Table1 Table2
------ ------
Name1(Column) Name2(Column)
----- ------
Employee Data Employee Data
Customer Data Customer Data
When I check My data in 2 tables with
select T1.Name1,len(T1.Name1)as Length1,Datalength(T1.Name1)as DataLenght1 from Table1 T1
select T2.Name2,len(T2.Name2)as Length2,Datalength(T2.Name2)as DataLenght2 from Table2 T2
The result is different Length and DataLength Values for the 2 tables,They are not same for 2 tables.
I can't change the original data in the 2 tables. How can I fix this issue.
Thank You
Joins do not have special rules for equality. The equality operator always works the same way. So if a = b then the join on a = b would work. Therefore, a <> b.
Check the contents of those fields. They will not be the same although you think they are:
select convert(varbinary(max), myCol) from T
Unicode has invisible characters (that only ever seem to cause trouble).
declare #t table (name varchar(20))
insert into #t(name)values ('Employee Data'),('Customer Data')
declare #tt table (name varchar(20))
insert into #tt(name)values ('EmployeeData'),('CustomerData')
select t.name,tt.name from #t t
INNER JOIN #tt tt
ON RTRIM(LTRIM(REPLACE(t.name,' ',''))) = RTRIM(LTRIM(REPLACE(tt.name,' ','')))
I would follow the following schema
Create a new table to store all the possible names
Add needed keys and indexes
Populate with existing names
Add columns to your existing tables to store the index of the name
Create relative foreign keys
Populate the new columns with correct indexes
Create procedure to perform an insert in new tables names only in case the value is not existing
Perform the join using the new indexes

how to update table(new_DB) from old table(old_DB)

What I have:
1 table(table is in both DB's)
2 databases(currently used + archived from last year(old))
"ID" is the primary key for the table.
my issue:
archived database table has rows in it that is not present in the currently used database table. Can anyone tell me how I go about updating the currently used database table from the old database table(i.e. insert * unique rows from old database table into new database table)
It sounds simple enough but wanted some advice before proceeding as I DO NOT want duplicate rows, I just want to throw the rows in the old table(that IS NOT present in the currently used database table) into the new one(copy only is fine).
I hope I explained clearly enough.
Insert rows from the new table only if row with same id not exists in old table:
insert into old_table select * from new_table nt
where not exists (select 1 from old_table
where id = nt.id)
(Specifying columns, both inserted and selected, is nice - but I'm lazy here...)
You can usually address tables from other databases by prefixing the database name: new_db.foo_table or old_db.foo_table. This way you can look for rows in the old table that have no duplicates in the new table:
select *
from old_db.foo_table as old_foo
where not exists (
select 1
from new_db.foo_table as new_foo
where new_foo.key_field = old_foo.key_field
-- add more comparisons as needed
);
Then you can use the insert into new_db.foo_table select ... syntax to put the records into the new table.
Use LEFT JOIN filtering NULLs in target table. I think it will be faster
INSERT INTO NEW_TABLE
SELECT ot.* FROM OLD_TABLE ot
LEFT JOIN NEW_TABLE nt on ot.ID = nt.ID
WHERE nt.ID IS NULL

Moving data from T1 in Database D1 to T1 in Database D2

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

Resources