Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么SQL Server中的CTE在不满足任何条件的情况下执行内部联接?_Sql_Sql Server 2005_Common Table Expression - Fatal编程技术网

为什么SQL Server中的CTE在不满足任何条件的情况下执行内部联接?

为什么SQL Server中的CTE在不满足任何条件的情况下执行内部联接?,sql,sql-server-2005,common-table-expression,Sql,Sql Server 2005,Common Table Expression,我有一个表mse,它的所有行StatusId=1。但在像这样的查询中,无论列StatusId的值是多少,都会执行内部联接视图。如何预防 WITH cte201401291517 AS ( SELECT 'QuantityOutPerShift' = SUM([vsqo].[QuantityOutPerShift]) , [mse].[ShiftGroup] , [mse].[Station] , [vsqo].[Shift] FRO

我有一个表mse,它的所有行StatusId=1。但在像这样的查询中,无论列StatusId的值是多少,都会执行内部联接视图。如何预防

WITH cte201401291517 AS 
( 
   SELECT
      'QuantityOutPerShift' = SUM([vsqo].[QuantityOutPerShift])
      , [mse].[ShiftGroup]
      , [mse].[Station]
      , [vsqo].[Shift]
   FROM
      [dbo].[mse] AS mse
   INNER JOIN 
      [dbo].[vmsqo] AS vsqo ON [mse].[Station] = [vsqo].[FromStation]
                            AND ( [mse].[ShiftGroup] = [vsqo].[ShiftGroup]
                                  OR [mse].[ShiftGroup] = 'ALL') -- order is important!
   WHERE
      [mse].[StatusId] = 3
   GROUP BY
      [mse].[ShiftGroup]
      , [mse].[Station]
      , [vsqo].[Shift])
UPDATE
  [dbo].[mse]
SET 
  [dbo].[mse].[QuantityOutPerShift] = [cte].[QuantityOutPerShift]
  , [dbo].[mse].[ShiftCurrent] = [cte].[Shift]
  --OUTPUT INSERTED.*
FROM
   cte201401291517 AS cte
WHERE
   [dbo].[mse].[Station] = [cte].[Station]
   AND ( [dbo].[mse].[ShiftGroup] = [cte].[ShiftGroup]
         OR [dbo].[mse].[ShiftGroup] = 'ALL' )  -- order is important!
   AND [dbo].[mse].[StatusId] = 3;
若并没有CTE,我无法做到这一点,因为我正在用UPDATE语句中无法使用的SUM更新表

我正在使用SQLServer2005