importing data from excel to SQL server 2008 - sql-server

I have the following problem
1-i have a database with a table called prod and 3 attributes Codep varchar (15) Namep varchar (40) and DateR datetime
2- I have a excel file with a sheet called prod and 3 columns with the same names of table columns(the first row have the columns names ) I have some info like 50 rows...
3 - when I try to import the excel data to SQL server 2008 it try to create a new table with the name prod$ I don't want that. I want import the data to my existing table prod no create a new one
What should I do to SQL import wizard recognize my excel sheet and insert the data into my table ?

the wizard lets you choose the destination, just click on the drop down

Tutorial with screenshots
https://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/
Query:
SELECT * INTO XLImport3 FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\test\xltest.xls;Extended Properties=Excel 8.0')...[Customers$]

Related

Inserting data from excel sheet into sql temp table

I have created the following temp table in SQL Server Management Studio:
CREATE TABLE ##LoginMap
(
ObjectId NVARCHAR(50),
UserPrincipleName NVARCHAR(500),
Username NVARCHAR(250),
Email NVARCHAR(500),
Name NVARCHAR(250)
)
These are the same names as the column headers in the excel sheet I am trying to get the data from.
The file name is called Test Loadsheet and the sheet name is AgilityExport_04Aug2022_164839.
I am trying to insert the data into the temp table like so:
INSERT INTO ##LoginMap
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=C:\temp\Test LoadSheet.xlsx', [AgilityExport_04Aug2022_164839]);
GO
However, I am getting the error:
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" does not contain the table "AgilityExport_04Aug2022_164839". The table either does not exist or the current user does not have permissions on that table.
Where have I gone wrong with this? And what do I need to do in order to successfully get the data from each column into my table?
You have file name as Test Loadsheet in one spot, but then in your query you have it as Test LoadSheet.xlsx. Try and see if this is holding it up.
Found a link on importing data from excel to SQL if you are interested:
https://learn.microsoft.com/en-us/sql/relational-databases/import-export/import-data-from-excel-to-sql?view=sql-server-ver16#openrowset
I went about this a different way. In the excel sheet I am using a made a formula like so:
="INSERT INTO ##LoginMap(ObjectId, DisplayName, AzureEmail, AzureUsername) VALUES('"&A2&"', '"&B2&"', '"&C2&"', '"&D2&"');"
I then had this repeated for each row. This then gave me an insert statement for each that I could simply copy and paste into SSMS and allow to run the query

How to import data from CSV into a SQL Server 2008 table?

I have a file which is fill of million of records and it looks like below:
20 Apr 2016 21:50:01,022 - username,,ResetGUI,Account,finished,8182819710127A590BAF3E5DFD9AE8B0.tomcat32,7
20 Apr 2016 21:50:01,516 - username,12345678,loadaccount,AccountAdmin,Starts,40A5D891F145C785CD03596AAD07209F.tomcat32,
I want to automate importing the data into a table.
I am not sure how it works?
Please advise!
If it is a one time data load you can use the SQL Server Import Export Wizard.
Right Click your DB in Management Studio -> Tasks -> Import Data
The wizard will guide you through selecting a database to import to, and a data source (other DB or flat file).
Make sure you create csvtest table first. Make all cols as varchar(200) first to test import
If you can give me col names ill construct a create table script for u. I just need to know your table name. Col names and data types ( from source ).
If u plan to regurly import this file Process can be...
If table exists truncate it.
If table does not exist create one.
Bulk load csv into new table
Anyways heres how to import into existing table from csv
BULK
INSERT CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

Updating a table from values in an excel file

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.

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.

Resources