SQL Query in Excel

Slade2000

Board Regular
Joined
Feb 3, 2009
Messages
118
Hi

Yesterday i got to do with SQL for the first time. I am running a query in SQuirrel and get my data and then copy it to excel. how can i use a macro to do that query directly in excel? my query looks like this.

Code:
SELECT DISTINCT NODE, Summary,Firstoccurrence, LastOccurrence, Tally, text
FROM Slas_Report.dbo.NTC_Logging_Data NTC_Logging_Data
WHERE NTC_Logging_Data.Node in ('calypdr1','vcibjnbgtaprd1')
AND ((LASTOCCURRENCE > '2011-09-01 00:00:00') AND (LASTOCCURRENCE < '2011-09-02 00:00:00')) 
AND (SEVERITY > 4) AND (Text IS NOT null) AND ( Summary not like '%GT_REPORT%' ) AND (text like '%closed%') 
AND (Summary not like '%health OK%') AND (Agent Not like '%Syman%') AND ( AlertGroup Not like '%Ping%' ) 
AND (Summary not like '%(SPACE)%') AND (Summary not like '%Disk Usage%') AND (Summary not like '%CPU usage%') 
AND (Summary not like '%storage volume%') AND (Summary not like '%memory usage%')
--BOT + Goldtier

UNION

SELECT DISTINCT NODE, Summary, Firstoccurrence, LastOccurrence, Tally, text
FROM Slas_Report.dbo.NTC_Logging_Data NTC_Logging_Data
WHERE NTC_Logging_Data.Node in ('10.182.202.106','vcibjnbcognprd1')
AND (Severity = 5) AND ((LASTOCCURRENCE > '2011-09-01 00:00:00') AND (LASTOCCURRENCE < '2011-09-02 00:00:00')) 
AND (TEXT IS NOT null) AND (text like '%closed%') AND (Summary not like '%health OK%') AND (Agent Not like '%Syman%') 
AND ( AlertGroup Not like '%Ping%' ) AND (Summary not like '%(SPACE)%') AND (Summary not like '%Disk Usage%') 
AND (Summary not like '%CPU usage%') AND (Summary not like '%storage volume%')  AND (Summary not like '%memory usage%')
--ET
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Which database are you connecting to?

You can use the 'Get External Data' feature in Excel to connect to most databases, and type your own SQL in MS Query which acts as a sort of intermediary between the database and Excel for anything more than trivial queries (and yours isn't trivial because of the Union)

In your SQL I see this:
--BOT + Goldtier
and this:
--ET
That doesn't seem like standard SQL to me, is that SQuirreL specific?
 
Upvote 0
They're comments (so not part of the SQL executable code). MSQuery (Data>Import External Data) would seem to be the way to go as Hermanito said.
 
Upvote 0
A bit OT but you should consider storing the query as a stored procedure or view in the SQirreL and simply executing it in MSQuery
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top