2012-06-14 37 views
0

我在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] 
+1

你是對的,局部變量不能在視圖中宣佈 - 但爲什麼你不能創建一個存儲過程代替? –

+0

該軟件需要一個視圖才能通過電子郵件發送此自動報告。 – Greg

回答

1

一個視圖中創建一個存儲程序,所有你的代碼應該是最簡單的方法。

但是,如果你真的需要一個觀點,這應該這樣做(未經測試)

CREATE VIEW ECMCUST_emcmachineresults 
AS 
WITH cte1 AS 
(
    SELECT 
     platform_id 
     ,SUM(CASE WHEN machine_id IS NOT NULL THEN 1 ELSE 0 END) AS WinTotal 
     ,SUM(CASE WHEN machine_id IS NOT NULL AND ignored = 1 THEN 1 ELSE 0 END) AS Ignored 
     ,SUM(CASE WHEN machine_id IS NOT NULL AND ignored = 1 AND data_value LIKE '*%' THEN 1 ELSE 0 END) AS IgnoredException 
     ,COUNT(*) AS Total 
     ,SUM(CASE WHEN ignored = 1 AND data_value <> '*%' THEN 1 ELSE 0 END) AS R1 
    FROM [ECMCUST_emcmachineresults] 
    WHERE platform_id IN (5,11,2) 
    GROUP BY platform_id 
) 
, cte2 AS 
(
    SELECT 
     CASE 
     WHEN platform_id = 5 THEN 'Windows' 
     WHEN platform_id = 11 THEN 'ESX' 
     WHEN platform_id = 2 THEN 'RHEL' 
     END         AS [Machine Type],   
     WinTotal        AS [Total Servers], 
     Ignored        AS [Ignored Servers], 
     IgnoredException AS [Ignored Exceptions Servers], 
     Total - R1 AS [Total Managed], 
     CAST((Total - R1) * 1.00 /WinTotal AS DECIMAL (3, 2)) AS [Percent Complete] 
    FROM cte1 
) 
SELECT 
     [Machine Type], 
     [Total Servers], 
     [Ignored Servers], 
     [Ignored Exceptions Servers], 
     [Total Managed], 
     [Percent Complete] 
FROM cte2 
UNION ALL 
SELECT 
     'ALL' AS [Machine Type], 
     SUM([Total Servers]), 
     SUM([Ignored Servers]), 
     SUM([Ignored Exceptions Servers]), 
     SUM([Total Managed]), 
     SUM([Percent Complete])/3 
FROM cte2 
+0

非常感謝。我需要一個視圖,因爲我正在使用的軟件包將其自動運行到視圖中並將其通過電子郵件發送出去。謝謝你教我如何釣魚,當我回家時我會試試這個,然後回來。 – Greg

+0

經過測試,這工作太棒了。我不得不給新的一個不同的視圖名稱,但就是這樣。 – Greg

相關問題