How to update multiple tables from excel file in SQL server - 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 .

Related

How to update specific entries of table on SQL Server using data from excel

I am trying to update rows on a SQL Server table using data from an excel sheet. I want to choose the rows of the table on the SQL server to update using the Primary Key of the table (StockCode), but I don't want to drop the rows which I am not updating.
Below are some screenshots of the excel data headers and the SQL table headers.
There are some StockCode's which do not have a box weight. They must remain NULL.
The StockCode's which do have box weights must be updated using the excel data.
Here is the excel data with the first row of data:
Here is the SQL Table with the first row of data:

view created table in microsoft sql server

I am running part of a query in Microsoft SQL server management studio
Select Table1.Column1
into #Table2
from Table1
now it has created the table but I actually want to view this table with my eyes but I cannot seem to find where the table is stored. Please could someone help me find it?
That is a temporary table. It will be created in the tempdb system database and you can see it by going to tempdb -> Temporary Tables.
Any tables where its name start with # is a Temporary Table. Exactly as the name suggests, it's temporary, and only exists for the same time the connection that created it does (or it is dropped).
If you want to view the data from a temporary table, you would do so like any other table SELECT * FROM #Table2;. .
I imagine what your really after is to not use a temporary table, so drop the # from the name, and the new table will be created in the database you are connected to.

import sybase data to sql server when any change make in table

I have a Sybase database which i want to migrate to SQL SERVER 2008R2. I have done this, but i got a new requirement. When any data is modified or new insert in table in Sybase that data only migrate from Sybase to Sql Server. The one table data is around 11,000135 so every time this is not possible to migrate all the data from Sybase to Sql Server. Is any possible way to do this?
I don't see any straight forward solution here. I would use following steps to deal with that issue:
Create table with unique key of source_table:
Create table mod_date
(
key int unique,
modified_date datetime
)
Create insert/update trigger for source_table that will be inserting/updating modified_date table.
When selecting data from source_table join mod_date and filter out only dates grater than last update.
Opposite to create new table yuo could also add modified_date to your source_table and use it during select. GL!

Importing data from excel to multiple tables in Sql Server database

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.

What is the equivalent of 'CREATE TABLE ... LIKE ..." in SQL Server

I am working with SQL Server (I am a SQL Server noob) and trying to alter a table. I want to CREATE TABLE LIKE to safely store data while I drop keys and constraints and all the other rigamorole that SQL Server seems to require when altering on the original table but I have not been able to find a match to that command...
you want to recreate the same structure?
how about this
SELECT *
into test
FROM myRealTable
where 0=1
no data will be inserted into the new table
You can do
SELECT * INTO #MyTable_tmp FROM MyTable
Then modify your MyTable, and copy your data back in. Other approaches I've seen is to create a new table call it Mytable_Tmp (Not a temp table), which will be your new table.
Then copy your data doing any migrations you need. Then you will drop the original table and do a rename on Mytable.
Or you can get one of the many excellant tools that compare databases and generate difference scripts or VSTS DB Edition (Comes with developer) and you can do a diff script from a project file to a DB.
Edit
When you run SELECT * INTO #MyTable FROM MyTable, SQL Server creates a new temporary table called #MyTable that matches each column and data type from your select clause. In this case we are selecting * so it will match MyTable. This only creates the columns it doesn't copy defaults, constraints indexes or anything else.

Resources