SQL Server 2005 Setting a variable to the result of a select query

前端 未结 6 981
面向向阳花
面向向阳花 2021-02-05 00:31

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


I want to do something like: OOdate DATETIME

SET OOdate =          


        
6条回答
  •  无人及你
    2021-02-05 00:52

    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 and the SELECT statement must be in parenthesis.

    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
    

提交回复
热议问题