How to deal with Excel file in SQL query? - sql

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

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.

How to use CSV data in an SQL query

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.

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.

How can I use a SQL Server table in an openquery to an Oracle database?

I have a database on SQL Server and would like to use a column in one of my tables in a linked server openquery I'm running to an Oracle database in order to match values between the two and insert the result into columns in my table in SQL Server .
Essentially I want it to be like this:
SELECT col1, col2, col3, col4
FROM OPENQUERY(link, 'SELECT * FROM Oracle_Table
WHERE ID = MSSQL.dbo.table.ID`)
So I'd like to be able to use my internal table column values to query an external database. They are related tables but different systems.
Would it be possible to get a big list of the values in the SQL Server table column and use it as a variable in the Oracle query? I've searched extensively online but haven't been able to find this one.
You can't pass parameters like I wanted to, but I ended up creating a bunch of queries in Powershell using a for loop and variables within the string to create my large query, then put a UNION ALL after each SELECT FROM OPENQUERY()

Joining SQL result with string

I have a MSSQL 2005 database with a lot of records that were added since last backup. I want to make another SQL script that puts result values into string representing INSERT statement that I will save for later use.
Something like:
SELECT 'Insert INTO tabname columns VALUES("+Column1"',')' FROM XY
Or simple example:
Column A,Row1=5
SELECT A+"BLAH" FROM X
should return "BLAH5"
Thank you
I'm not sure i understand, if you want to build a script (lets say PHP) just run over the records and either print out or to file something like:
echo 'INSERT INTO tablename (field1,field2) VALUES('.$row['field1'].','.$row['field2'].');';
if you want that string in the result directly from the SQL you could use CONCAT:
SELECT CONCAT('INSERT INTO...VALUES(',field1,',',field2,')') FROM yourtable;
Hope that helps...
You should really mention what SQL database system you're using.
For MySQL, what you want is the CONCAT function.
SELECT CONCAT('INSERT INTO table (columns) VALUES ("', column1, '");') FROM xy;
what version of sql?
for ms sql, you can use + for concatenation and single quotes for strings
for mysql/oracle, use concat(column, 'string')