Importing data from excel to multiple tables in Sql Server database - sql-server

I have an excel sheet where in the data looks similar to the table below :
EmployeeName Manager Name
E1 M1
E2 M2
E3 M3
E4 M1
I need to export this into a database which has two separate table for employee and manager.
Sql tables which I am exporting to has structure like this:
Create Table Manager (Manager_Id int Primary Key Identity(1,1),
ManagerName nvarchar(50))
Create Table Employee (EmployeeID int Primary Key Identity(1,1) ,
EmployeeName nvarchar(50),
Manager_Id int)
ManagerId is a foreign reference in the employee table.
Since I need Manager Table before the Employee table, I successfully imported the manager data first using SQL Server Import and Export wizard.
My next step was to import data into the employee table.This is where the problem comes in.
Now, I want manager Id from a table sitting in the Sql Server database and the employee names from the excel sheet. I though there must be a way in the Import and Export wizard in the Sql Statement option to write a query which can refer to both excel and the sql table but I get an error "This is not a query"
Is there a better way of importing data to multiple tables?

Instead of importing the Employee data to the Employee table, import it into a Temp table on SQL server. Then you will be able to write a query which retrieves the employee name from the Temp table, joins it to the Manager table and then inserts into the real Employee table.

There are many solutions to this, but if the data set is relatively small, I'd just do this in Excel:
Separate the data into Manager and Employee tabs (leave both columns in the Employee tab)
Add some ID columns to both tabs.
Use the vlookup function on the Manager's name to find the relevant Manager IDs for each employee. Copy&PasteValues to get rid of the functions you just made.
Use the import wizard to get both tables into SQL Server.

Related

Import/export only new data by ID

There are two databases A and B. Each has the same tables T, even the same records ID. But table A has been updated with new data. How pass only new data from A to B by id (skip existing id). Using import/export wizard and t-sql?
Simple.. Create unique index on ID of B table and set ignore duplicate values=True Make sure that your ID has PRIMARY KEY.
You can export all records from A then import it into a staging table in B. Then you use query to insert into Table B by selecting the staging table WHERE ID not exists in (TABLE B).

SSIS designer Visual Studio foreign keys integration

I need to integrate two similar databases into third DB3. DB3 is almost the same as DB1.
First database DB1:
Addresses table with: primary key AddressId
People table with: primary key PersonId , foreign key AddressId
Second database DB2:
It is pretty similar, but in other language
Data from DB1 to DB3 flows smoothly, table after table. For example I have 1000 records in DB3 table named Addresses from DB1 and 1000 records in table named People from DB1.
Let's suppose Person with number 30 in DB3 (after transfering from DB1) has the IdAddress number 20.
Address with number 40 in DB2 has the ID number 1040 in DB3 and the Person has ID number 30 in DB2 and 1030 in DB3.
While transferring table People from B2 to B3 we need to know the address ID is not 40 but 1040.
I'm trying to use lookup to find existing record, but I'm newbie in SSIS VS designer. Could you help me? How can I resolve this problem?
Suggestions
You can do this using Lookup Transformation component as you mentioned, but first you have to:
Select the basic information of each table that can distinguish each logical entity. Example if talking about Persons you can choose the Fullname+ Mothername + Date Of Birth , ...
After selecting this attributes you have to add a Lookup Transformation
Map thiese columns between Source and Lookup table
Select the ID column (from Lookup table) as Output and rename the column to be NewID
Select Ignore failure option to handle non matches situation
After doing these steps, if the same person was inserted previously you will get the ID in NewID column, else NewID is NULL
Additional Information
Microsoft Docs - Lookup Transformation
UNDERSTAND SSIS LOOKUP TRANSFORMATION WITH AN EXAMPLE STEP BY STEP
SSIS Lookup Transformation
Lookup Transformation in SSIS

How to create identity column when importing data from Excel into MS SQL Server (with Import and Export Wizard)?

I need to import great amount of data from excel into MS SQL Server, using the Import/ Export Wizard. Then I'll continue importing more data into the same table on a weekly basis.
The bad thing is that my excel data doesn't have an identity column, which to use as a primary key. The only option with what available is to use 2 string columns as a primary key, which is not a good idea.
Is there a way the sql server to add auto-identity column (integer) when importing the data, and what's the trick? I prefer such a column to be automatically added, because I'll need to import big amount of data into the same table on a weekly basis.
I tested a couple of times (with no success) and looked for a solution in the internet, but didn't found an answer to that particular question. Thanks in advance!
You can create the table first along with the new identity column.
CREATE TABLE YourTable
(
id INT IDENTITY,
col1....
col2....
col3....
PRIMARY KEY(id)
)
Then run the import/export wizard. When you get to the destination section pick your newly created table and map all the fields except the identity column. After the import you can check the table and see the id column has been populated.
Column names in Excel sheet should be same as that of sql Table.
Map Excel columns with that of SQL table columns by Clicking Edit Mapping.
Just don't map that (identity) column of sql table to anything .
In Import-Export Wizard don't check Enable identity insert Checkbox. (Leave that
un selected).
and go ahead import .. This worked for me. !!!!!
Previously when i used to check Enable identity insert it used to give me error.
I had a similar issue. I have a SQL table with an identity column (auto increment ID value) defined. I needed to import an Excel spreadsheet into this table.
I finally got it to work via the following:
Do NOT add a column to the Excel spreadsheet for your identity column in the SQL table.
When you run the import wizard and get to the Edit Mappings step, do NOT select the Enable identity insert checkbox. This, in particular, was tripping me up.

How to update multiple tables from excel file in SQL server

Hi I am new to SQL server.
I need help to update two tables from excel file.
Excel file includes all the data in 1 sheet and that data need to update on SQL server tables.
example data on the Excel file
Employeename, Deparment
SQL tables are
employer details and departments
I know how to add to the table but I want to know for the multiple tables
Use the following steps,
Create a temporary table with the same structure as excel.
Import data from excel to temp. table using SSIS
package/Export/Import Wizard/ Just Copy & Paste
Insert into department table by selecting distinct departments from
temp. table.
Insert into employee table by selecting employee details, department
pk can be selected from department table by department name.
Remove temporary table.
Create and use a SSIS package to do your task. You can alternatively use Sql Server Export/Import Wizard .

How to use the master table as a lookup to populate the foreign key in the Reference table during SSIS data load

if i have an excel file with data in the following columns/format
DeptName
DeptLocation
Description
I also have an SQL table for Employees with the following columns
EmployeeId
DeptId(foreign key)
DeptLocation
Description
And then another SQL reference Departments table with following columns
DeptId(Primary key)
DeptName
Departments table is already populated with master data. I now want to populated Employees table with the data in the excel file using SSIS Dataflow task, an excel source and an OLE DB Destination.
Given the DeptName in the Excel file, how can i lookup its corresponding DeptId from the Departments table and have it set as the DeptId foreign key value in table Employees.
Basically, i want to get the DeptName in the excel file, lookup its corresponding DeptId value from the Department Table and set it to DeptId(foreign key) in Employees table during data load.
Merge Join between Excel Source and the Departments table data (join on Department Name) gives DepartmentId for each Department Name. Make sure to trim and use same case when joining both sources.
Use the output of the above merge join to join with Employee Table(join on Dept ID column).

Resources