0

I have no clue how to Import data from one database to another with condition.

I have DB Name (Northwind) and the table Name Employee

I have following columns

ID Name

I have another DB (Mater) and the table Name Employee.

I have the following columns Emp.ID Emp.Name

Now i want transfer all data from Northwind.Employee to Master.Employee table with Condition.

Condition is

IF ID=1 then Emp.ID=201 (this is a constant value no logic behind that)

Any idea or suggestion please

Usher
  • 2,146
  • 9
  • 43
  • 81

3 Answers3

1

in order to select from diferent DB you can assist this question: INSERT INTO from two different server database

the id issue is a simple case you can see example here: SQL Case Statement Syntax?

Community
  • 1
  • 1
yossico
  • 3,421
  • 5
  • 41
  • 76
1

if the databases are on the same server, you can just reference the 2 tables from the different databases with SQL such as the following:

INSERT INTO Master.Employee 
SELECT 201 as ID, e2.Name
FROM Northwind.Employee e2
WHERE e2.ID = 1

But if the databases are on different servers, you will have to use either a linked server or SSIS package to achieve this.

Robert Anderson
  • 1,246
  • 8
  • 11
0

If I'm understanding your question correctly, you can use a case statement in your insert:

insert into master.schema.employee (id, name)
select case when id = 1 then 201 else id end, name
from northwind.schema.employee
sgeddes
  • 62,311
  • 6
  • 61
  • 83