Import Data in Excel Column to SQL Server - sql

I have an excel spreadsheet with two columns; job_number and referred_by. I have a SQL Server table with the same two columns. Most of the Job Numbers from the excel spreadsheet exist in the SQL server but not all of them.
How would I import all the referred_by into the SQL Server where a matching job_number exists?

If you need to query an excel spreadsheet I would set it up as a linked server, so:
EXEC sp_addLinkedServer
#server= 'xlsx_NewSheet',
#srvproduct = 'ACE 12.0',
#provider = 'Microsoft.ACE.OLEDB.12.0',
#datasrc = 'C:\spreadsheet.xlsx',
#provstr = 'Excel 12.0; HDR=Yes'
With that set up you can almost query it like just another table:
SELECT * FROM OPENQUERY (XLSX_NewSheet, 'Select * from [Sheet1$]')
or
SELECT * FROM XLSX_NewSheet...[Sheet1$]
There are some limitations that you may need to explore. I would suggest just dumping the entire contents into a temp table and operating from there.

Related

How to INSERT INTO SQLite with SELECT FROM SQL Server

I want to SELECT a table in a SQL Server database and then INSERT it into a local SQLite database.
IS it possible to do this entirely with a query in the vein of:
INSERT INTO table1 ( column1 )
SELECT col1
FROM table2
but passing connection information?
Setup a local odbc data source, DSN, for SQLite file. Call it SQLite_DataSource
Then setup a linked server in SSMS:
USE [master]
GO
EXEC sp_addlinkedserver
#server = 'SQLite', -- the name you give the server in SSMS
#srvproduct = '', -- Can be blank but not NULL
#provider = 'MSDASQL',
#datasrc = 'SQLite_DataSource'
GO
Then you can use it like any other db:
INSERT into SQLite_DataSource (column1)
SELECT col1
FROM table2
Lookup SQL Server's documentation for sp_addlinkedserver for more details.
You need to create a SQL Server Linked Server to SQLite, take a look at this post for more information .

Database direct import from Excel?

I've worked on SQL Server 2014 and I need to import data in it from Excel.
'Till now, I've imported data by right-clicking on the current database -> Tasks -> Import Data.
I want to know if there is a SQL syntax to import data directly into a table?
You can try OPENROWSET
Insert into SQLServerTable
Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [SheetName$]')

SQL Linked server query very very slow

I am extracting large amount of data via linked server from VIEWS. I am using SQL Server 2012 and linked server is SQL Server 2008
My select statement is
SELECT * INTO MY_LOCAL_TABLE
FROM
( SELECT * FROM LINKEDSERVER.DB.TABLE.VIEW
WHERE DATE>'2012-01-01' AND ID IN (SELECT ID FROM MY_LOCAL_VIEW)
) Q
I am expecting 300K rows for nearly 700+ IDs. before it used to take couple of hours but now its take more than a 20 hr!!
Could you please suggest any alternative solution for this PAIN??
Very many thanks in advance!
When you use a 4-part name such as [server].db.dbo.table, especially in a join, often times the entire table is copied over the wire to the local machine, which is obviously not ideal.
A better approach is to use an OPENQUERY -- which is handled at the source (linked server).
Try:
SELECT *
FROM OPENQUERY([LINKEDSERVER], 'SELECT * FROM DB.TABLE.VIEW WHERE DATE>'2012-01-01')
AND ID IN (SELECT ID FROM MY_LOCAL_VIEW)
With this approach the linked server will return all rows for date > x, and then the local server will filter that by ID's in your local table.
Of course, indexing will still play a factor for doing SELECT * FROM DB.TABLE.VIEW WHERE DATE>'2012-01-01.
Another approach, which I use on large subsets, is to dump the local ID's to the remote server, THEN handle it all remotely, such as:
-- copy local table to linked server by executing remote query
DECLARE #SQL NVARCHAR(MAX)
SET #SQL = 'SELECT ID INTO db.dbo.tmpTable FROM [SERVER].DB.DBO.MY_LOCAL_VIEW'
EXEC(#SQL) AT [LINKEDSERVER]
-- index remote table?!?
DECLARE #SQL NVARCHAR(MAX)
SET #SQL = 'CREATE INDEX [IXTMP] ON db.dbo.tmpTable (ID)'
EXEC(#SQL) AT [LINKEDSERVER]
-- run query on local machine against both remote tables
SELECT *
-- INTO sometable
FROM OPENQUERY([LINKEDSERVER], 'SELECT *
FROM DB.TABLE.VIEW
WHERE DATE>''2012-01-01''
AND ID IN (SELECT ID FROM db.dbo.tmpTable)')
-- now drop remote temp table of id's
DECLARE #SQL NVARCHAR(MAX)
SET #SQL = 'DROP TABLE db.dbo.tmpTable'
EXEC(#SQL) AT [LINKEDSERVER]
If the local view is also large, then you may consider executing a remote query that uses an openquery back to the local machine (assuming the remote machine has the local as a link).
-- copy local table to linked server by executing remote query
DECLARE #SQL NVARCHAR(MAX)
SET #SQL = 'SELECT ID INTO db.dbo.tmpTable FROM OPENQUERY([SERVER], ''SELECT ID FROM DB.DBO.MY_LOCAL_VIEW'')'
EXEC(#SQL) AT [LINKEDSERVER]
Others have already suggested about indexing. So I am not going there. suggest another option, if you could change that inner query
SELECT * FROM LINKEDSERVER.DB.TABLE.VIEW
WHERE DATE>'2012-01-01' AND ID IN (SELECT ID FROM MY_LOCAL_VIEW)
To a joined query using inner join since you said having 700+ inlist elements. give it a try.
SELECT lnv.* FROM LINKEDSERVER.DB.TABLE.VIEW lnv
inner join MY_LOCAL_VIEW mcv
on lnv.ID = mcv.ID
and lnv.DATE > '2012-01-01'

Querying the same table for a list of databases in MS SQL Server

This is my first time posting on SO, so please go easy!
I'm attempting to write a SQL script that queries the same table for a list of databases in a single SQL Server instance.
I have successfully queried the list of databases that I required using the following, and inserting this data into a temp table.
Select name Into #Versions
From sys.databases
Where name Like 'Master%'
Master is suffixed with numerical values to identify different environments.
Select * From #Versions
Drop Table #Versions
The table name I am trying to query, is the same in each of the databases, and I want to extract the newest value from this table and insert it into the temp table for each of the database names returned.
I have tried researching this but to no avail. I am fairly comfy with SQL but I fear I could be out of my depth here.
You can do the following. Once you have the list of your databases, you can build up the query (you need to edit it for your purpose).
Select name Into #Versions
From sys.databases
Where name Like 'test%'
declare #sql as varchar(max) = ''
select #sql = #sql + 'INSERT INTO sometable SELECT TOP 1 * FROM ' + name + '..sourcetable ORDER BY somedate DESC; '
FROM #Versions
exec (#sql)
Drop Table #Versions
Look at The undocumented sp_MSforeachdb procedure and here

How can I save the results of an OPENQUERY query to an Excel or CSV file?

How to store data from an openquery in a .CSV OR EXCEL FILE?
select * from openquery(DB_LINK,'SELECT col1,col2,col3,col4 from oracle_table_name')
select * from openquery(DB_LINK,'SELECT col1,col2,col3,col4,col5 from oracle_table_name2')
The result has to be stored in 2 different files.
I have scheduled the open query such that it runs on 1st of every month.
And the 2 files that are generated have to be kept in client's windows specific folder.
This is the first time I'm using SQL SERVER. So please explain step by step.
I am using this query at the moment.
declare #title varchar(55)
set #title = 'Test'
EXEC sp_makewebtask
#outputfile = 'D:\Test11.xls',
#query = 'select CIRCUIT_DESIGN_ID as [Account Number] from openquery(MSP_PROD,'select CIRCUIT_DESIGN_ID from circuit')',
#FixedFont=0,#resultstitle=#title
Create an Export Data... task using SQL Server Management Studio (by right clicking the database), specify that you want to output the data to CSV, and that your datasource is to be a SQL statement.
Use your select * from openquery... statement as the datasource.
Save the package, then create a SQL Server Agent job that will execute it on your desired schedule.