我有查询计算CPU使用率,需要将输出复制到表但得到错误
关键字'DECLARE'附近的语法不正确。
请查找查询。
Insert into dbo.cpudata([database],[system cpu],[database cpu],[%],[Date&time])
DECLARE @total INT
SELECT @total=sum(cpu) FROM sys.sysprocesses sp (NOLOCK)
join sys.sysdatabases sb (NOLOCK) ON sp.dbid = sb.dbid
SELECT sb.name 'database', @total 'system cpu', SUM(cpu) 'database cpu', CONVERT(DECIMAL(4,1), CONVERT(DECIMAL(17,2),SUM(cpu)) / CONVERT(DECIMAL(17,2),@total)*100) '%', GETDATE () as [Date&time]
FROM sys.sysprocesses sp (NOLOCK)
JOIN sys.sysdatabases sb (NOLOCK) ON sp.dbid = sb.dbid
--WHERE sp.status = 'runnable'
GROUP BY sb.name
ORDER BY CONVERT(DECIMAL(4,1), CONVERT(DECIMAL(17,2),SUM(cpu)) / CONVERT(DECIMAL(17,2),@total)*100) desc
答案 0 :(得分:0)
您是否可以转到填充插入上方@total的语句。
DECLARE @total INT
SELECT @total=sum(cpu) FROM sys.sysprocesses sp (NOLOCK)
join sys.sysdatabases sb (NOLOCK) ON sp.dbid = sb.dbid
Insert into dbo.cpudata([database],[system cpu],[database cpu],[%],[Date&time])
...............
答案 1 :(得分:0)
DECLARE
语句中间不能INSERT
变量。您已在INSERT INTO <columnlist>
和SELECT <columnlist>
之间完成了此操作。
将声明移到INSERT
的开头上方。