How to use CSV data in an SQL query - sql

I have a some data in an excel sheet in the form of a table. I want to run an sql query on that data. I am okay to use the data raw from the csv.
Is it possible to create a temporary view or table using that data within the query itself. I don't want to create a separate table for this excel data.

There are a bunch of answers for this already... Have you searched?
You want to do something like this;
Select *
into [temp_table$]
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\spreadsheet.xls;HDR=YES;IMEX=1',
'SELECT * FROM [SHEET1$]')
Like in this question: Get Excel sheet into temp table using a script

For SQL Server 2008 and above you can use OPENROWSET
In simplest form it will look like this:
SELECT * FROM OPENROWSET(BULK 'data.csv', SINGLE_CLOB) AS DATA;
Just remember to specify full file path.

Related

Creating searchable table/array in SQL from excel column

I have some programming experience but am brand new to SQL.
Basically I have about 300 terms that I want to search for in a single search.
What is the best way to store those terms in a way that I can iterate through them in a query? They're currently in an excel column and I'd prefer not to have to manually write each one in
SELECT * INTO EXCEL_IMPORT
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=C:\Excel\Spreadsheet.xls; HDR=YES; IMEX=1',
'SELECT * FROM [Sheet1$]');
Will create a temporary table that you can run your queries against.
SELECT * FROM
SEARCHABLEDATABASE
WHERE column_of interest IN
( SELECT search_terms FROM EXCEL_IMPORT )
Or use the SQL Server import wizard
OR Simply run the select queries directly against the sheet.
EDIT: These two queries will match entries in a column of database to terms from a excel spreadsheet, providing they are exactly the same) you could TRIM them both to prevent differences in whitespace causing issue.

Query to save to csv with counter

I need your assistance. I am running the below sql query from Oracle SQL Developer so the results can be save to a csv file. I run this query using the Run Script and I can see the records that are being save to the csv file.
How can I modify the query so it can show me the number of records being save to the file? For example something like printing 1000 of 200000.
spool c:\temp\myoutputfile.csv
select * from mytable;
spool off;
You can add rownum column to your query result like below
select rownum, mytable.* from mytable;
Note, SQL doesn't have other options to print some text separately while selection is under progress. Of course you have option to print some hard coded text as part of a resulted column. Otherwise you may have to write PL SQL procedure to meet your requirement.

Import Excel data into an existing SQL Server 2005 table

I'm trying to import some data from an Excel sheet into an existing SQL Server 2005 table.
The table has the following columns:
Name, Surname, PhoneNumber ,Bill
and they are in the same order in the Excel sheet.
I've never attempted such a thing before, but after searching for a while I was told I could use:
INSERT INTO table
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\temp\test.xls',
'SELECT * FROM [Sheet1$]')
Do I need to reference the column names from the Excel sheet to the SQL Server table columns?
I'm certainly not a SQL guru, but would consider myself a good beginner.
Can anyone help?
See here in you Management Studio
:
Choose you db?
Choose your source :
and from here is all just following the GUI !!

How to deal with Excel file in SQL query?

i had a problem .... i have and old excels files and i want to save them into SQL database ... MY Q is : if i want to do for example in .xls from (C16:C28) want to take it and put it into table1 in field NAMES .. how i can write it in query ?
note am using Microsoft SQL Server Management .. and try to get all my xls and import it into data base via sql query take from those files data and put it into my database ..
INSERT INTO [table1] (Names) VALUES ('&C16&') to ('&C28&')
is this true ? idk how to write it .. need help
When you use openrecordset you can specify a range [Sheet1$C16:C28] on the table in the query parameter
INSERT INTO [table1] (Names)
SELECT *
FROM OPENROWSET
('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\Foo.xls;HDR=NO',
'select * from [Sheet1$C16:C28]') AS t

To get data from table in script from sql server 2005

I am using sql server 2005
I have a table [say tblHistory] and this table contains 100 rows.
I have created the same table at the server, but the table doesn't have the data, I want data from tblHistory to convert into
INSERT INTO tblHistory ------
so that I could run the script on the server to fill the database.
To generate all the INSERT INTO statements you need based on table data, take a look at this project: http://www.codeproject.com/KB/database/sqlinsertupdategenerator.aspx
you need to create a linked server between the two servers and then you do something like this
INSERT INTO tblHistory
select * from LinkedServerNAme.DatabaseName.SchemaName.tblHistory
To add a linked server read this http://msdn.microsoft.com/en-us/library/ms190479.aspx
BTW you can also use OPENROWSET, OPENDATASOURCE, SSIS or bcp out and bcp in
Not sure I understand the question... you just want to copy the contents of one table into another?
INSERT INTO newTable SELECT * FROM tblHistory
If the new table doesn't already exist, you can use SELECT INTO:
SELECT *
INTO new_table
FROM tblHistory
But that's the caveat - it has to be a new table, no data already in there.
Otherwise, use:
INSERT INTO new_table
SELECT x.* --preferrable to actually define the column list than use *
FROM OLD_TABLE x
You should check out the SSMS Tools Pack - one of its feature is the ability to generate those INSERT scripts you're looking for!
Get the SSMS Tool Pack from this site - it's an add-in for SQL Server Management Studio - highly recommended!