SQL Server2008R2でカスタマイズされたT-SQLクエリを作成しました。このコードはT-SQLクエリとして機能し、正しい情報を返します。コーディングに慣れていないので、SQLがクエリのデータを返す方法に満足していました。残念ながら、ビューを作成する場合は、@variablesを宣言できないことに気づかずにこれを書き始めました。
そのため、ビューを作成してレポートの自動実行をスケジュールすることができません。このクエリを書き直すために使用できる特定のヒントを探しています。ソフトウェアはvCM「VMwareConfigurationManager」であり、私のT-SQLクエリは、作成されたビューで情報を検索し、ソフトウェアパッケージのインストールの進行状況に関連する統計を返します。
私はここに投稿する前に、検索し、数人の親しい同僚と協力して答えを見つけようとしました。2つの部分があります。動作する作成されたビュー。必要なすべての情報をビューに配置して、簡単にアクセスできるようにします。そして、SQL ServerManagementStudioのクエリウィンドウでうまく機能している2番目の部分。ビューを作成しようとすると、この特定のエラーが発生します。
メッセージ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]