There is a query that is perfectly executed in the sql managment studio. Sql server 2014
But if you create a view, the message goes:
Not the correct syntax near the keyword "DECLARE"
DECLARE @tbComputers table (ComputerId int)
INSERT INTO @tbComputers
SELECT ComputerId FROM we_Computers WHERE dbo.IsComputerNested(ComputerId, 3) = 1
SELECT
c.ComputerName as "Ð˜Ð¼Ñ ÐºÐ¾Ð¼Ð¿ÑŒÑŽÑ‚ÐµÑ€Ð°",
ISNULL(s.ActivityTime, 0) ,
ISNULL(s.ForegroundTime - s.ActivityTime, 0) ,
ISNULL(s.ForegroundTime, 0),
FROM we_Computers c
INNER JOIN @tbComputers tc ON tc.ComputerId = c.ComputerId
LEFT JOIN (SELECT
ComputerId,
SUM(sa.ActivityTime) As ActivityTime,
SUM(sa.ForegroundTime) As ForegroundTime
FROM we_SummaryActivities sa
WHERE sa.LogDate >= '01.01.2017' AND sa.LogDate <= '20.03.2017'
AND sa.ComputerId IN (SELECT ComputerId FROM @tbComputers)
GROUP BY ComputerId) s ON s.ComputerId = c.ComputerId