我想在Java代码中使用此查询:
DECLARE @BRPS BIGINT
SELECT @BRPS=cntr_value
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE 'Batch Requests/sec%'
WAITFOR DELAY '000:00:10'
SELECT (cntr_value-@BRPS)/10.0 AS "Batch Requests/sec"
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE 'Batch Requests/sec%'
当我删除DECLARE @BRPS BIGINT
时,我收到错误消息。你能帮我把这段代码重写成简单的SQL查询吗?同样的结果呢?
答案 0 :(得分:2)
那里有很多瑕疵......
首先:不,如果您想将值存储到稍后要使用的变量中,则无法删除DECLARE
。
第二:如果将值写入变量,则结果必须是标量。这意味着:一行中的一列。使用LIKE 'Batch Requests/sec%'
看起来你期望得到几个可能的结果......试试吧
DECLARE @BRPS BIGINT=(SELECT cntr_value
FROM sys.dm_os_performance_counters
WHERE counter_name = 'Batch Requests/sec');
SELECT @BRPS;
第三:你期待什么?尝试:
SELECT cntr_value FROM sys.dm_os_performance_counters WHERE counter_name = 'Batch Requests/sec';
WAITFOR DELAY '000:00:10';
SELECT cntr_value FROM sys.dm_os_performance_counters WHERE counter_name = 'Batch Requests/sec';
GO
SELECT cntr_value FROM sys.dm_os_performance_counters WHERE counter_name = 'Batch Requests/sec';
GO
SELECT cntr_value FROM sys.dm_os_performance_counters WHERE counter_name = 'Batch Requests/sec';
第四:你想要达到什么目标?
答案 1 :(得分:1)
DECLARE @ctn VARBINARY(128)
SELECT @ctn = CAST(cntr_value AS BINARY(8))
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE 'Batch Requests/sec%'
SET CONTEXT_INFO @ctn
GO ----
SELECT [Batch Requests/sec] = (cntr_value - CAST(SUBSTRING(CONTEXT_INFO(), 1, 8) AS BIGINT)) / 10.
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE 'Batch Requests/sec%'