简体   繁体   English

SQL Server 2005将变量设置为选择查询的结果

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

How do I set a variable to the result of select query without using a stored procedure? 如何在不使用存储过程的情况下将变量设置为select查询的结果?


I want to do something like: OOdate DATETIME 我想做类似的事情: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: 然后我想在此查询中使用OOdate:

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

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. 为此,SELECT必须返回单个列和单个结果,SELECT语句必须在括号中。

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

-- Sql Server 2005 Management studio - Sql Server 2005管理工作室


use Master
go
DECLARE @MyVar bigint
SET @myvar = (SELECT count(*) FROM spt_values);
SELECT @myvar

Result: 2346 (in my db)

-- Note: @myvar = @Myvar - 注意: @myvar = @Myvar

You could use: 你可以使用:

declare @foo as nvarchar(25)

select @foo = 'bar'

select @foo

This will work for original question asked: 这适用于原始问题:

DECLARE @Result INT;
SELECT @Result = COUNT(*)
FROM  TableName
WHERE Condition

You could also just put the first SELECT in a subquery. 您也可以将第一个SELECT放在子查询中。 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. 谓词表达式无法正确优化或使用CONVERT()函数引用的列上的索引。

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. 此版本可以利用涉及FaultDate的索引,并实现相同的目标。

Here it is, rewritten to use a subquery to avoid the variable declaration and subsequent SELECT. 在这里,重写为使用子查询来避免变量声明和后续的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. 请注意,由于在FF.FaultDate上使用了CONVERT(),因此此方法与原始方法具有相同的索引使用问题。 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. 问候。

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...) 在这种情况下,为什么不组合两个查询,通过在第一个查询中搜索第二个查询(SELECT xxx in (SELECT yyy...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM