Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8088893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:13:33+00:00 2026-06-05T19:13:33+00:00

I have created a T-SQL query in SQL Server 2008 R2 that is a

  • 0

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]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:13:35+00:00Added an answer on June 5, 2026 at 7:13 pm

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a SQL query that runs on SQL Server 2008. For some
I have a database that I have created using SQL Server Developer 2008. I
I have created a complex sql server 2008/coldfusion search page, that searches thru a
I have created an stored procedure in SQL Server 2008 and it contains the
Me and my colleagues have a question regarding SQL Server 2008 query length and
Tables have been created in our SQL Server 2008 R2 database on a local server,
I have a database column in a SQL Server 2008 database that contains VARCHAR
I have a SQL Server 2008 stored procedure (pseudocode below) that goes as follows:
I have a table in a sql server 2008 database that contains bunch of
Have an SSRS I created in VS 2008. SQL server = 2008r2, authenticated login

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.