我在 SQL Server 2008 R2 中创建了一个自定义的 T-SQL 查询。该代码非常适合用作 T-SQL 查询并返回正确的信息。作为编码新手,我对 SQL 为我的查询返回数据的方式感到满意。不幸的是,我开始写这篇文章时没有意识到如果你要创建一个视图,@variables 不能被声明。
因此,我无法创建视图并安排报告的自动执行。我正在寻找可以用来重写此查询的特定提示,如果它可以完成的话。该软件是vCM“VMware Configuration Manager”,我的T-SQL查询在创建的视图中查找信息并返回有关软件包安装进度的统计信息。
我已经搜索过,并与一些亲密的同事一起尝试在此处发布之前找到答案。有两个部分;一个有效的创建视图,它将我需要的所有信息放在一个视图中以便于访问。第二部分在 SQL Server Management Studio 查询窗口中运行良好。当我尝试创建视图时,我得到了这个特定的错误。
消息 156,级别 I5,状态 1,过程 ECMCUST_ProgressReport,第 3 行
关键字“声明”附近的语法不正确。
从我在网上找到的所有内容来看,似乎需要重写。在创建视图、存储过程等时也可以看到这个问题......
一个关于往哪个方向走的提示,会很棒。
这将创建有效的 VIEW
/* ------------------------------------------------------------*/
CREATE VIEW ECMCUST_emcmachineresults as
SELECT a.machine_id, a.machine_name, a.managed, a.[enabled],a.ignored, b.platform_id, b.current_agent_version, c.data_value
FROM ecm_sysdat_machine_state a
JOIN ecm_dat_machines b
ON a.machine_id = b.machine_id
JOIN ecm_sysdat_asset_machine_data c
ON b.machine_id = c.machine_id
where property_id = 3 or property_id =1006
/* ------------------------------------------------------------*/
这是在 T-SQL 中运行良好的代码,但我无法从中创建视图
/* ------------------------------------------------------------*/
Declare @WinTotal1 as FLOAT
Declare @Ignored1 as FLOAT
Declare @IgnoredException1 as FLOAT
Declare @TotalsR1 as FLOAT
Declare @PercentComplete1 as DECIMAL(3,2)
select @WinTotal1 = COUNT(machine_id)
from [ECMCUST_emcmachineresults]
where platform_id = 5
select @Ignored1 = COUNT(machine_id)
from [ECMCUST_emcmachineresults]
where platform_id = 5
and ignored = 1
select @IgnoredException1 = COUNT(machine_id)
from [ECMCUST_emcmachineresults]
where platform_id = 5
and ignored = 1
and data_value like '*%'
SELECT @TotalsR1 =
(SELECT COUNT(*) FROM ECMCUST_emcmachineresults WHERE platform_id = 5) -
(SELECT COUNT(*) FROM ECMCUST_emcmachineresults WHERE platform_id = 5
and ignored = 1
and data_value != '*%')
SELECT @PercentComplete1 = @TotalsR1/@WinTotal1
/* ------------------------------------------------------------*/
Declare @ESXTotal2 as FLOAT
Declare @Ignored2 as FLOAT
Declare @IgnoredException2 as FLOAT
Declare @TotalsR2 as FLOAT
Declare @PercentComplete2 as DECIMAL(3,2)
select @ESXTotal2 = COUNT(machine_id)
from [ECMCUST_emcmachineresults]
where platform_id = 11
select @Ignored2 = COUNT(machine_id)
from [ECMCUST_emcmachineresults]
where platform_id = 11
and ignored = 1
select @IgnoredException2 = COUNT(machine_id)
from [ECMCUST_emcmachineresults]
where platform_id = 11
and ignored = 1
and data_value like '*%'
SELECT @TotalsR2 =
(SELECT COUNT(*) FROM ECMCUST_emcmachineresults WHERE platform_id = 11)-
(SELECT COUNT(*) FROM ECMCUST_emcmachineresults WHERE platform_id = 11
and ignored = 1
and data_value != '*%')
SELECT @PercentComplete2 = @TotalsR2/@ESXTotal2
/* ------------------------------------------------------------*/
Declare @RHELTotal3 as FLOAT
Declare @Ignored3 as FLOAT
Declare @IgnoredException3 as FLOAT
Declare @TotalsR3 as FLOAT
Declare @PercentComplete3 as DECIMAL(3,2)
select @RHELTotal3 = COUNT(machine_id)
from [ECMCUST_emcmachineresults]
where platform_id = 2
select @Ignored3 = COUNT(machine_id)
from [ECMCUST_emcmachineresults]
where platform_id = 2
and ignored = 1
select @IgnoredException3 = COUNT(machine_id)
from [ECMCUST_emcmachineresults]
where platform_id = 2
and ignored = 1
and data_value like '*%'
SELECT @TotalsR3 =
(SELECT COUNT(*) FROM ECMCUST_emcmachineresults WHERE platform_id = 2)-
(SELECT COUNT(*) FROM ECMCUST_emcmachineresults WHERE platform_id = 2
and ignored = 1
and data_value != '*%')
SELECT @PercentComplete3 = @TotalsR3/@RHELTotal3
Select
'Windows' as [Machine Type],
@WinTotal1 AS [Total Servers],
@Ignored1 AS [Ignored Servers],
@IgnoredException1 AS [Ignored Exceptions Servers],
@TotalsR1 AS [Total Managed],
@PercentComplete1 AS [Percent Complete]
union
Select
'ESX' as [Machine Type],
@ESXTotal2 AS [Total Servers],
@Ignored2 AS [Ignored Servers],
@IgnoredException2 AS [Ignored Exceptions Servers],
@TotalsR2 AS [Total Managed],
@PercentComplete2 AS [Percent Complete]
union
Select
'RHEL' as [Machine Type],
@RHELTotal3 AS [Total Servers],
@Ignored3 AS [Ignored Servers],
@IgnoredException3 AS [Ignored Exceptions Servers],
@TotalsR3 AS [Total Managed],
@PercentComplete3 AS [Percent Complete]
union
select
'All' as [Machine Type],
@WinTotal1 + @ESXTotal2 + @RHELTotal3 as [Total Servers],
@Ignored1 + @Ignored2 + @Ignored3 as [Total Ignored],
@IgnoredException1 + @IgnoredException2 + @IgnoredException3 as [Total Ignored Exceptions],
@TotalsR1 + @TotalsR2 + @TotalsR3 as [Total Managed],
(@PercentComplete1+@PercentComplete2+@PercentComplete3)/3 as [Percent Complete]