我认为这是一个非常简单的 SQL 查询,但是在使其运行时遇到了问题。
if (object_id('#InfoTable')is not null)
Begin
Drop Table #InfoTable
End
create table #InfoTable (NameOfObject varchar(50), NameOfCounter varchar(50), SampledValue float(30), DayStamp datetime)
insert into #InfoTable(NameOfObject, NameOfCounter, SampledValue, DayStamp)
select vPerformanceRule.ObjectName AS NameOfObject, vPerformanceRule.CounterName AS NameOfCounter, Perf.vPerfRaw.SampleValue AS SampledValue, Perf.vPerfHourly.DateTime AS DayStamp
from vPerformanceRule, vPerformanceRuleInstance, Perf.vPerfHourly, Perf.vPerfRaw
where (ObjectName like 'Logical Disk' and CounterName like '% Free Space' AND SampleValue > 95 AND SampleValue < 100)
order by DayStamp desc
select NameOfObject, NameOfCounter, SampledValue, DayStamp
from #InfoTable
Drop Table #InfoTable
我尝试了各种其他形式的语法,但无论我做什么,我都会收到这些错误消息。
Msg 207, Level 16, State 1, Line 10
Invalid column name 'NameOfObject'.
Msg 207, Level 16, State 1, Line 10
Invalid column name 'NameOfCounter'.
Msg 207, Level 16, State 1, Line 10
Invalid column name 'SampledValue'.
Msg 207, Level 16, State 1, Line 10
Invalid column name 'DayStamp'.
Msg 207, Level 16, State 1, Line 22
Invalid column name 'NameOfObject'.
Msg 207, Level 16, State 1, Line 22
Invalid column name 'NameOfCounter'.
Msg 207, Level 16, State 1, Line 22
Invalid column name 'SampledValue'.
Msg 207, Level 16, State 1, Line 22
Invalid column name 'DayStamp'.
第 10 行是第一个“插入”行,第 22 行是第二个选择行。有什么想法吗?
答案1
除非您的数据库上下文是 TEMPDB,否则 DROP 不会执行任何操作,因此 #InfoTable 可能存在于具有不同列名的先前步骤中。
尝试将 DROP 更改为:
if (object_id('tempdb..#InfoTable')is not null)
Begin
Drop Table #InfoTable
End