views:

8737

answers:

7

How do I set a variable to the result of select query without using a stored procedure?

+1  A: 

What do you mean exactly? Do you want to reuse the result of your query for an other query?

In that case, why don't you combine both queries, by making the second query search inside the results of the first one (SELECT xxx in (SELECT yyy...)

Luk
+1  A: 

You could use:

declare @foo as nvarchar(25)

select @foo = 'bar'

select @foo
JPrescottSanders
A: 

I want to do something like: OOdate DATETIME

SET OOdate = Select OO.Date from OLAP.OutageHours as OO where OO.OutageID = 1

Then i want to use OOdate in this query: Select COUNT(FF.HALID) from Outages.FaultsInOutages as OFIO inner join Faults.Faults as FF ON FF.HALID = OFIO.HALID WHERE CONVERT(VARCHAR(10),OO.Date,126) = CONVERT(VARCHAR(10),FF.FaultDate,126)) AND OFIO.OutageID = 1

test
+4  A: 

You can use something like

SET @cnt = SELECT COUNT(*) FROM User

or

SELECT @cnt=COUNT(*) FROM User

For this to work the SELECT must return a single column and a single result.

Edit: Have you tried something like this?

DECLARE @OOdate DATETIME

SET @OOdate = Select OO.Date from OLAP.OutageHours as OO where OO.OutageID = 1

Select COUNT(FF.HALID) 
from Outages.FaultsInOutages as OFIO 
inner join Faults.Faults as FF 
 ON FF.HALID = OFIO.HALID 
WHERE @OODate = FF.FaultDate
 AND OFIO.OutageID = 1
rslite
A: 

Thanks JPrescottSanders that's what I was looking for

test
A: 

Thanks for the walk through rslite much appreciated

test
A: 

You could also just put the first SELECT in a subquery. Since most optimizers will fold it into a constant anyway, there should not be a performance hit on this.

Incidentally, since you are using a predicate like this:

CONVERT(...) = CONVERT(...)

that predicate expression cannot be optimized properly or use indexes on the columns reference by the CONVERT() function.

Here is one way to make the original query somewhat better:

DECLARE @ooDate datetime
SELECT @ooDate = OO.Date FROM OLAP.OutageHours AS OO where OO.OutageID = 1

SELECT 
  COUNT(FF.HALID)
FROM
  Outages.FaultsInOutages AS OFIO 
  INNER JOIN Faults.Faults as FF ON 
    FF.HALID = OFIO.HALID 
WHERE
  FF.FaultDate >= @ooDate AND
  FF.FaultDate < DATEADD(day, 1, @ooDate) AND
  OFIO.OutageID = 1

This version could leverage in index that involved FaultDate, and achieves the same goal.

Here it is, rewritten to use a subquery to avoid the variable declaration and subsequent SELECT.

SELECT 
  COUNT(FF.HALID)
FROM
  Outages.FaultsInOutages AS OFIO 
  INNER JOIN Faults.Faults as FF ON 
    FF.HALID = OFIO.HALID 
WHERE
  CONVERT(varchar(10), FF.FaultDate, 126) = (SELECT CONVERT(varchar(10), OO.Date, 126) FROM OLAP.OutageHours AS OO where OO.OutageID = 1) AND
  OFIO.OutageID = 1

Note that this approach has the same index usage issue as the original, because of the use of CONVERT() on FF.FaultDate. This could be remedied by adding the subquery twice, but you would be better served with the variable approach in this case. This last version is only for demonstration.

Regards.

Pittsburgh DBA