我在SQL Server 2008 R2中創建了一個T-SQL查詢,該查詢是一個自定義項。該代碼很適合作爲T-SQL查詢並返回正確的信息。作爲編程新手,我很滿意SQL爲我的查詢返回數據的方式。不幸的是,如果你打算創建一個視圖,那麼我就不知道@variables不能被聲明。SQL Server 2008 R2 T-SQL:重寫代碼以允許創建視圖的建議
因此,我無法創建視圖並安排自動執行報表。我正在尋找可用於重寫此查詢的特定提示,如果可以完成的話。該軟件是vCM「VMware Configuration Manager」,我的T-SQL查詢在創建的視圖中查找信息,並返回與軟件包的安裝進度有關的統計信息。
我已經搜索過,並與幾位親密的同事合作,在發佈之前嘗試找到答案。有兩部分;一個可以工作的創建視圖,它將我需要的所有信息放在視圖中以便於訪問。第二部分在SQL Server Management Studio查詢窗口中運行良好。當我嘗試創建視圖時,出現此特定錯誤。
消息156,級別I5,狀態1,過程ECMCUST_ProgressReport,3號線附近關鍵字
不正確的語法 '聲明'。
從我在網上找到的所有東西,它似乎重新編寫是爲了。它也出現這個問題可以看到創建意見,存儲過程等...
提示哪個方向去,會很棒。
這將創建工作
/* ------------------------------------------------------------*/
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],
(@[email protected][email protected])/3 as [Percent Complete]
你是對的,局部變量不能在視圖中宣佈 - 但爲什麼你不能創建一個存儲過程代替? –
該軟件需要一個視圖才能通過電子郵件發送此自動報告。 – Greg