Comparing two excel sheet in MSexcel 2010 - sql-server

I am tring to compare 4 excel sheet with upto 1000 records,in excel2010 there is no compare tool(so i cant try if it works or not),i m also not able to implement vlookup.. i tried online sites..but no success.
Now i m trying to insert value from local excel sheet into MSSQL..for that found given command
INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=G:\Test.xls;', 'SELECT * FROM [Sheet1$]')
giving error below.
Incorrect syntax near ')'.
Then i changed this command as
INSERT INTO mytable values('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=G:\Test.xls;', 'SELECT * FROM [Sheet1$]')
in which 'mytable' is a dummy table with same format as of my excel sheet
which gives error
Column name or number of supplied values does not match table
definition.
Any help would be appreciated.

After further research..i found another method..in which I directly imported the whole data of excel sheet into MSSQL.
which can be done as follows:
MSSQL-->object explorer--> Database--> right click on your DB(new or existing) --> Task--> Import Data
Then select MS Excel as Data source,choose file from local-->next
Then choose "MS OLEDB provider for SQL Server" as destination..then two nexts and click on finish.
This will load all the data of your excel file in DBMS..then you can write query for the type of data you want.

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 an Excel file into a SQL Server database?

I want to import an Excel sheet into a SQL Server database but I'm not able to do that. I tried the procedure manually by right click on database name then import data then data source then file destination by finish. All processes are ok. I got all the green tick marks but 0 rows transferred. I don't what to do, I tried it so many times but not able to that. Please help.
USE [DatabaseName]
GO
SELECT * INTO Schema.TableName
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=DriveLetter:\FolderName\FileName.xlsx', [Sheet1$]); -- Modify the path to the file
GO

How can I delete these rows and columns that were created from excel?

I have this doubt. I have charged a sheet of excel from SQL Server 2008 and this shows all the data what contains the sheet. I wish to have the data that I want and I can save these records in a some table. But I need to know How
I can delete all the data that I dont need rows and columns of the sheet.
Please I need your help.
I have it that I allow me import my sheet of Excel
select * into #TBL_DATA from openrowset('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0; Database=C:\Microsoft\siac.xls; HDR=YES; IMEX=1',
'select * from [Campo23$]')
This the image as shows in the query.
Thanks in advance.
If the data you don't want to import has a specific value in one of the columns you could use a WHERE clause in your SQL statement to filter the data.
For example: WHERE colName <> 'value'

Import from MSSQL to Excel when the data contains embedded line breaks

I am trying to export a sql query (MS SQL 2014) into excel 2010. The problem is column values contain line breaks, so the remaining data gets copied to the next line in excel. Is there a way to get rid of this? Keeping the column as is? or maybe encapsulating the column so the sql considers it as one column and ignores the line breaks?
Here is my SQL Query:
select * from tbl_case
where (casenature not like '%<strong>%'
and casenature not like '%<br />%'
and casenature like '%from:%')
and userid in (select employeelogin from tbl_employees where riding='15010')
Works fine if I use the normal way to import data from MSSQL to Excel which is: in Excel, Data->From other sources->SQL server.
To import data resulting from an arbitrary SQL query:
At the last step of the wizard (where you select the range), press Properties...
In the resulting Connection properties window:
Definition->Command type - SQL
In the Command text field, write your query
You can replace enter keys with space in select statement, and then export to Excel

SQL Server stored procedure export to Excel workbook with multiple sheets

How can I export to an Excel workbook from a stored procedure to multiple sheets with few sql statements?
I am currently using the following statement:
EXEC proc_generate__excel 'db', 'temp',#filename, #SeqNo, #ext, #sqlorder
It will create three Excel workbooks, if there are three sql statement.
How can I export data from three sql statement to three sheets in one Excel workbook?
Create an empty Excel file with the sheets in it you need (my example sales.xls with sheets "sheet1","sheet2")
Copy empty file to desired location/name
Using your select statement to get the desired information for sheet1; insert the data into the excel file:
insert into OPENROWSET(
'Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=d:\export\sales.xls;;HDR=YES',
'SELECT * FROM [Sheet1$]')
select * from sales_part1
Using your select statement to get the desired information for sheet2; insert the data into the excel file:
insert into OPENROWSET(
'Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=d:\export\sales.xls;;HDR=YES',
'SELECT * FROM [Sheet2$]')
select * from sales_part2
Check these links for reference:
http://www.sqlservercentral.com/Forums/Topic487837-19-1.aspx
http://www.sqlservercentral.com/Forums/Topic660148-338-1.aspx
http://www.databasejournal.com/features/mssql/article.php/10894_3331881_1
Some SO threads:
SQL Server export to Excel with OPENROWSET
error on sql script with 'openrowset'

Resources