Hello, I have what I think is a chicken and egg question. I am trying to use a CTE, but once I explain the scenario, this may not be the best approach.
I am reading data from one table, created with a CSV, that contains computer system info. I'm comparing each computer's info to tables that contain expected results, and creating a 'results' table of exceptions. Here's the basic CTE code:
WITH cte(ComputerName, PercentFreeSpace) ( SELECT ComputerName, PercentFreeSpace FROM ReportTable WHERE PercentFreeSpace < 20 )
-- and what I'm trying to do with it (in english/ sql): If (SELECT ComputerName, PercentFreeSpace FROM cte WHERE (select count(*) from cte) > 0 ) Then: If the column PercentFreeSpace does not exist in the ResultsTable, Add it, and then insert all the results.
Is this a valid approach, or is there a better way? Thanks!
asked Mar 24, 2010 at 01:56 PM in Default
why not just insert the exception rows?
or if you need to do 'something' to the ResultsTable first
Don't think of a cte as a temporary table that gets created, it's just a definition like an in-line view or derived table. In your question you seem to want to create it, query it, and dependant on the answer do something with it.