Greg
Greg

Reputation: 3

SQL Server 2008 R2 T-SQL: Recommendations for re-writing code to allow creating a view

I have created a T-SQL query in SQL Server 2008 R2 that is a customization. The code works great as a T-SQL query and returns the correct information. Being new to coding, I was happy with the way SQL returns the data for my query. Unfortunately, I proceeded to write this unaware that @variables cannot be declared, if you are going to create a view.

As such, I am unable to create the view and schedule automatic execution of the report. I am looking for specific hints I can use to re-write this query, if it can be done at all. The software is vCM "VMware Configuration Manager", and my T-SQL query looks up information in a created view and returns statistics relative to installation progress of the software package.

I have searched, and worked with a few close colleagues to try and find the answer before posting here. There are two parts; a created view that does work, which places all of my needed information in a view for easy access. And the second part that is working great in SQL Server Management Studio query window. When I attempt to create the view, I get this specific error.

Msg 156, Level I5, State 1, Procedure ECMCUST_ProgressReport, Line 3
Incorrect syntax near the keyword 'Declare'.

From everything I have found online, it appears a re-write is in order. It also appears this issue can be seen creating views, stored procedures, etc...

A hint on which direction to go, would be awesome.

This creates the VIEW that works

/* ------------------------------------------------------------*/
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
/* ------------------------------------------------------------*/

This is the code that works fine in T-SQL, but I cannot create a view from

/* ------------------------------------------------------------*/
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]

Upvotes: 0

Views: 1157

Answers (1)

EricZ
EricZ

Reputation: 6205

Create a stored procedure, with all your code should be easiest way.

However, if you really need a view, this should do it (untested)

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 

Upvotes: 1

Related Questions