query excel with sql - sql

Using MS Office+VBA (or, sometimes, Visual Studio 2010) I am looking for a way to query Excel-Files using only SQL Query, similar to this way of querying a textfile:
SELECT * FROM [Text;DATABASE=L:\Testfiles].test1.csv
As a result I would expect something like:
SELECT * FROM [Excel File=L:\Testfiles\test2.xls].[sheet1$A1:B1000]
I am not looking for a way to query Excel-Files with SQL and a ADODB-connection string (as depicted on connectionstrings.com or here on so.com), since I want all information regarding the data source inside the actual SQL-Code, and not split up between SQL and the connection setup in VB/VBA.
Any hints would be greatly appreciated.
Regards
Martin

You can use FROM clause like this:
FROM [sheet1$A3:E22] IN 'C:\path\MyFile.xlsx' [Excel 12.0;HDR=YES;IMEX=0]

Related

Why is MS Access 2010 SQL choking on this query?

The following query will not show up in design view and if you trying to make it show it locks up MS Access and you have to use the Task Manager to stop MS Access. The query actually runs and produces the correct results. If there is a better way I will certainly accept that.
SELECT
log_metric_N.metric_title,
log_metric_N.metric_N
FROM
(
SELECT
tref_log_metrics_weights_and_factors.metric_title,
[metric_base].[metric_count],
[metric_base].[metric_sum],
(([metric_base].[metric_count]*[tref_log_metrics_weights_and_factors].[metric_weight])/[metric_base].[metric_sum]) AS metric_N
FROM
tref_log_metrics_weights_and_factors,
(
SELECT
Count(tref_log_metrics_weights_and_factors.metric_weight) AS metric_count,
Sum(tref_log_metrics_weights_and_factors.metric_weight) AS metric_sum
FROM
tref_log_metrics_weights_and_factors
WHERE (((tref_log_metrics_weights_and_factors.metric_weight)<>0))
) as metric_base
) AS log_metric_N;
#HansUp you were exactly right on. I forgot all about the Domain functions and they work perfectly without the complexity. Below is the resultant SQL statement.
SELECT
tref_log_metrics_weights_and_factors.metric_title,
DCount("[metric_weight]","tref_log_metrics_weights_and_factors","[metric_weight]<>0") AS metric_count,
Dsum("[metric_weight]","tref_log_metrics_weights_and_factors") AS metric_sum,
(([metric_count]*[tref_log_metrics_weights_and_factors].[metric_weight])/[metric_sum]) AS metric_N
FROM
tref_log_metrics_weights_and_factors

SQL Select statement to exclude data

I am trying to write a SQL query to setup a dynamic collection in Configuration Manager 2012. My current query is
select * from SMS_R_System where SMS_R_System.Name LIKE 'cmgr%'
This will grab any server name that starts with cmgr and put it in the specified collection.
My issue is that I need to add another statement in this query to exclude servers that contain the following entries qw, dw and tw. This will prevent my non production servers from being put into the Production collections.
My knowledge of SQL scripting is very limited, so I appreciate any feedback.
Can you use something like this?
select * from SMS_R_System
where SMS_R_System.Name LIKE 'cmgr%'
AND SMS_R_System.Name NOT LIKE '%qw%'
AND SMS_R_System.Name NOT LIKE '%dw%'
AND SMS_R_System.Name NOT LIKE '%tw%'
You might want to take a look at that previous answer, it speaks about using the NOT EXISTS command as part of your query.

Querying another DB while connecting to other Database in SQL

In my SQL script I am using one database(Lets assume MyDb1).
In side that script I need to query extended property of another DB (MyDb2).
I have no Idea to do this.
Please advise me.
You can use this Query :
SELECT * FROM MyDb1.DBO.TableName as tbl1,MyDb2.DBO.TableName as tbl2
WHERE tbl1.ID=tbl2.ID

sql or trick to search through whole database

is there a way to actually query the database in a such a way to search for a particular value in every table across the whole database ?
Something like a file search in Eclipse, it searches accross the whole worspace and project ?
Sorry about that .. its MS SQL 2005
SQL Workbench/J has a built in tool and command to do that.
It's JDBC based and should also work with SQL Server.
You will need to use the LIKE operator, and search through each field separately. i.e.
SELECT * FROM <table name>
WHERE (<field name1> LIKE '%<search value>%') OR
(<field name2> LIKE '%<search value>%') OR
... etc.
This isn't a quick way though.
I think the best way would be to
1) programatically generate the query and run it
2) use a GUI tool for the SQL server you are using which provides this functionality.
In mysql you can use union operator like
(SELECT * from table A where name = 'abc') UNION (SELECT * from
table B where middlename = 'pqr')
and so on
use full text search for efficency
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
Well, your best bet is to write a procedure to do this. But to give you some pointers you can use the INFORMATION_SCHEMA.Tables to get a list of all the tables in a given database and INFORMATION_SCHEMA.Columns to get a list of all columns. These tables also give you the datatype of columns. So you will need a few loops on these tables to do the magic.
It should be mentioned most RDBMSs nowadays support these schemas.
In phpmyadmin, go to your database, reach the search tab.
Here you will be able to select all of your tables and search through your entire db in one time.

tsql : outputting each record to their own text file

is there a simple way to just output each record in a select statement to write to its own file?
for example, if you have the tsql query in sql server 2005,
select top 10 items, names + ':' + address from book
and you ended up with 10 text files with the individual name and addresses in each file.
is there a way to do this without writing an extensive spWriteStringToFile procedure? I'm hoping there is some kind of output setting or something in the select statement.
thanks in advance
SQL returns the result set first, there's no opportunity in there for writing records to specific files until afterwards.
Being SQL Server 2005, it's possible you could use a SQLCLR (.NET 2.0 code) function in a SQL statement without having to make a separate application.
In SSMS, you can do a results to file, but that wouldnt split each record out into its own file. I pretty sure you cannot do this out of the box, so it sounds like you will be rolling your own solution.
You'd do this in some client, be it Java, VBA or SSIS typically.