Updating a table from values in an excel file - sql-server

I have a set of excel files with a total of around 130,000 lines. Each line has a column with an ID and a column with a name. I need to update an existing column in one table in the database and fill each ID row with its matching name.
This only needs to be done once so I was going to just use a formula in excel to make each line a query (=CONCATENATE("UPDATE Table SET Name = '", $C1, "' WHERE ID = ", $A1)) then copy all of those queries out and run them in Sql Server Management studio. Is this an OK way to do it or will the server choke on 130,000 individual queries?
What is the proper way to do it?
Thanks!

Import your Excel workbook to a new table, then join that with your existing table on the ID field and build an update query from that.

Create an SSIS package to import the data. You can have Sql Server create the SSIS package for you by right clicking on the target database name, then select "Task" from the pop up window, and then select import data. Follow the GUI and in the first window select "Microsoft Excel" as the data source.

Related

ETL Script to dynamically map multiple EXECUTE SQL resultset to multiple tables (table name based on sql file provided)

ETL Script to dynamically map multiple execute sql resultset to multiple tables (table name based on sql file provided)
I have a source folder with sql files ( I can put it up as stored procedures as well ) . I know how to loop and execute sql tasks in a foreach container. Now the part where I'm stuck is I need to use the final result set of each sql queries and shove it into a table with the same name as the sql file.
So, Folder -> script1.sql , script2.sql etc -> ETL -> goes to table script1, table script2 etc.
EDIT : Based on the comment made by Joe, I just want to say that I'm aware of using insert within a script but I need to insert it onto a table in a different server.And Linked servers are not the ideal solutions
Any psuedocode or link to tutorials will be extremely helpful . Thanks!
I would add the table creation to the script. It is probably the simplest way to do this. If your script is Select SomeField From Table1, you could change it to Select SomeField Into Table script1 From Table1. Then there is no need to map in SSIS which is not easy to do from my experience.

Append one ore more rows from Excel to existing SQL Server table

I have imported an Excel table into SQL Server 2016 Express and made a table using the import wizard.
Now when I update my Excel sheet, e.g add one ore more row to it, I also want to update my SQL Server table, that means, adding the one ore more rows to the table as well. What's the easiest way to do this? In an "append" rows manner. I don't want to add the whole Excel sheet again..
You asked for the easiest solution. Since you are already comfortable using the wizard it would seem to me that the easiest way is to import the "updated" Excel sheet / file into SQL Server Express using the wizard as well. Yet, import it into a new table (without removing the old one).
Afterwards, insert new rows or update the existing records on the SQL Server with a simple SQL MERGE statement. Afterwards, you can drop / delete the imported table again (because the existing table has been updated).
While I do not know your tables the following SQL code sample shows a simple merge on a basic customer table where tblCustomers would be the exiting table (to be updated / insert new rows) and tblCustomersNEW would be the new import (which will be deleted again once the update / append is complete):
merge dbo.tblCustomers as target
using dbo.tblCustomersNEW as source
on source.ID = target.ID
when matched then
update set target.Name = source.Name,
target.Country = source.Country
when not matched by target then
insert (Name, Country)
values (
source.Name,
source.Country
);
Note, that the MERGE statement requires a semicolon at the end similar to CTE requiring a semicolon before you start ; With myTable as....
For more information on the MERGE statement you might want to read the following on article on MSDN: https://msdn.microsoft.com/en-us/library/bb510625.aspx

Excel file Import to Sql Server, Sql Server Table have indexes and Excel file have Names

I want to import Excel File to SQL Server, but my problem is the Table which I created in SQL for excel file save indexes and Foreign Keys.
Like SQLTable have ID, StudentName, ClassID, SectionID, etc...but my Excel file have not Class ID it has Class name in it. Similarly Section Name instead of SectionID.
How I will import the file to database.
Thank you
You obviously need to map the names to IDs before you can load the Excel data into the table. I can think of three ways to do it:
a) Import the Excel data into a new table, and then write some SQL to join to the relevant tables to get the IDs, and then do the insert into the target table.
b) Query your database directly from Excel to get the IDs that you need, and then use the SQL import wizard to import the data into the target table.
c) Write an SSIS package to read the Excel data, query the database for the additional IDs, and insert into your target table.

Create SQL Server Database from Excel Table

I currently have a report in Excel based off a database which contains the following columns:
Table Name, Column Name, Data Type, Max Length, Precision, Scale
I would like to know if I can import this into another copy of SQL Server to recreate the empty database. I currently have no access to the source database and so need to go from this meta data.
Thanks for the responses - my approach was to concatenate up a create statement in Excel which could then be copied and pasted into SQL Server.
So
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
Was formed into
"CREATE TABLE" & table_name"&
"("&
column_name1 &" "& data_type &"("& size &"),"&
....
and run down my list of fields.
Took around 5 minutes to enter in a few hundred tables into SQL from the report.

How can i import indexes into ms SQL Server?

I created a new databases , and added tables using the import data wizard. But the wizard din't create the indexed and constraint's on the table. How can I Import indexes?
If your source is also SQL Server, you should be able to run Tasks -> Generate Scripts and select "Script Indexes" in the list of options for the old database and execute the script on the new database with maybe a change in database name.
Just manually add indexes to your table
Here is an example from MSDN:
This example creates an index on the au_id column of the authors table.
SET NOCOUNT OFF
USE pubs
IF EXISTS (SELECT name FROM sysindexes
WHERE name = 'au_id_ind')
DROP INDEX authors.au_id_ind
GO
USE pubs
CREATE INDEX au_id_ind
ON authors (au_id)
GO
The other way you can do this is open the table in design mode management studio, highlight the field you want to index and look at the options at the top. One of them is for indexes and you can simply manually add it and give it a name right in management studio.

Resources