2014-12-05 56 views
0

因此,我有一些包含數據的表,我需要根據舊數據獲取具有新數據的新表,然後將它們插入臨時表以供下一次處理。從SELECT中檢查數據,然後插入新值

但我有一些問題,我的SQL

DECLARE @PGNPGE float, 
      @PGHTTP400PCT float, 
      @PGHTTP500PCT float, 
      @PGSLPCT float, 
      @PGTME float; 

    DECLARE @tempTable TABLE (
     PViews int, 
     Http400 int, 
     Http500 int, 
     PTime int, 
     ExclSimul int, 
     DispSimul int, 
     SlowPages int, 
     AESWA int 
    ); 

    IF EXISTS (SELECT COUNT(*) FROM [ApplicationThreshold] WHERE [ApplicationID] = @AppID) 
    BEGIN 
     SELECT @PGNPGE = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 6; 
     SELECT @PGHTTP400PCT = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 31; 
     SELECT @PGHTTP500PCT = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 12; 
     SELECT @PGSLPCT = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 49; 
     SELECT @PGTME = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 72; 
    END 
    ELSE 
    BEGIN 
     SELECT @PGNPGE = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 6; 
     SELECT @PGHTTP400PCT = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 31; 
     SELECT @PGHTTP500PCT = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 12; 
     SELECT @PGSLPCT = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 49; 
     SELECT @PGTME = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 72; 
    END 

    INSERT INTO @tempTable (
     PViews, 
     Http400, 
     Http500, 
     PTime, 
     ExclSimul, 
     DispSimul, 
     SlowPages, 
     AESWA) 
    SELECT 
     [PViews] = CASE WHEN sel.[PGNPGE] < @PGNPGE THEN 1 ELSE 0 END, 
     [Http400] = CASE WHEN sel.[PGHTTP400PCT] > @PGHTTP400PCT THEN 1 ELSE 0 END, 
     [Http500] = CASE WHEN sel.[PGHTTP500PCT] > @PGHTTP500PCT THEN 1 ELSE 0 END, 
     [PTime] = CASE WHEN sel.[PGTME] > @PGTME THEN 1 ELSE 0 END, 
     [ExclSimul] = CASE WHEN ([PViews] + [Http400] + [Http500] + [PTime]) >= 1 THEN 1 ELSE 0 END, 
     [DispSimul] = CASE WHEN ([PViews] + [Http400] + [Http500] + [PTime]) > 1 THEN ([PViews] + [Http400] + [Http500] + [PTime]) ELSE 0 END, 
     [SlowPages] = CASE WHEN sel.[PGSLPCT] > @PGSLPCT THEN 1 ELSE 0 END, 
     [AESWA] = CASE WHEN [ExclSimul] >=1 THEN 0 ELSE [SlowPages] END 
    FROM (SELECT [PGNPGE], [PGHTTP400PCT], [PGHTTP500PCT], [PGSLPCT], [PGTME] 
      FROM [dbo].[TimeValue] 
      WHERE [ApplicationID] = @AppID 
      AND [Time] BETWEEN @DateFrom AND @DateTo) sel; 

SELECT * FROM @tempTable; 

和錯誤在那裏我添加新值

消息207,級別16,狀態1,59號線
無效列名稱'PViews'。

消息207,級別16,狀態1,行59
無效的列名稱'Http400'。

消息207,級別16,狀態1,行59
無效的列名稱'Http500'。

消息207,級別16,狀態1,行59
無效的列名稱'PTime'。

消息207,級別16,狀態1,行60
無效的列名'PViews'。

消息207,級別16,狀態1,行60
無效的列名稱'Http400'。

Msg 207,Level 16,State 1,Line 60
無效的列名稱'Http500'。

消息207,級別16,狀態1,行60
無效的列名稱'PTime'。

消息207,級別16,狀態1,行60
無效的列名'PViews'。

消息207,級別16,狀態1,行60
無效的列名稱'Http400'。

Msg 207,Level 16,State 1,Line 60
無效的列名稱'Http500'。

消息207,級別16,狀態1,行60
無效的列名稱'PTime'。

消息207,級別16,狀態1,行62
無效的列名稱'ExclSimul'。

消息207,級別16,狀態1,行62
無效的列名'SlowPages'。

+3

你不能指表別名在它的定義相同'select'。如果你喜歡,你可以使用子查詢,或者重複表達式。 – 2014-12-05 14:23:16

+0

我認爲有可能以某種方式避開 – Sanprof 2014-12-05 14:28:27

+0

是的。這只是分配別名的另一種方式。 – 2014-12-05 14:58:25

回答

0

嘗試使用CTE在表變量中插入數據:

declare @PGNPGE float, 
    @PGHTTP400PCT float, 
    @PGHTTP500PCT float, 
    @PGSLPCT float, 
    @PGTME float; 

declare @tempTable TABLE 
(
    PViews int, 
    Http400 int, 
    Http500 int, 
    PTime int, 
    ExclSimul int, 
    DispSimul int, 
    SlowPages int, 
    AESWA int 
); 

IF EXISTS (SELECT COUNT(*) FROM [ApplicationThreshold] WHERE [ApplicationID] = @AppID) 
BEGIN 
    SELECT @PGNPGE = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 6; 
    SELECT @PGHTTP400PCT = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 31; 
    SELECT @PGHTTP500PCT = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 12; 
    SELECT @PGSLPCT = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 49; 
    SELECT @PGTME = Value FROM [ApplicationThreshold] WHERE [ThresholdID] = 72; 
END 
ELSE 
BEGIN 
    SELECT @PGNPGE = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 6; 
    SELECT @PGHTTP400PCT = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 31; 
    SELECT @PGHTTP500PCT = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 12; 
    SELECT @PGSLPCT = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 49; 
    SELECT @PGTME = Value FROM [ApplicationTypeThreshold] WHERE [ThresholdID] = 72; 
END 

;with ThresholdCTE 
as 
(
    SELECT 
     case when sel.[PGNPGE] < @PGNPGE then 1 else 0 end as [PViews], 
     case when sel.[PGHTTP400PCT] > @PGHTTP400PCT then 1 else 0 end as [Http400], 
     case when sel.[PGHTTP500PCT] > @PGHTTP500PCT then 1 else 0 end as [Http500], 
     case when sel.[PGTME] > @PGTME then 1 else 0 end as [PTime], 
     case when ([PViews] + [Http400] + [Http500] + [PTime]) >= 1 then 1 else 0 end as [ExclSimul], 
     case when ([PViews] + [Http400] + [Http500] + [PTime]) > 1 then ([PViews] + [Http400] + [Http500] + [PTime]) else 0 end as [DispSimul], 
     case when sel.[PGSLPCT] > @PGSLPCT then 1 else 0 end as [SlowPages], 
     case when [ExclSimul] >=1 then 0 else [SlowPages] end as [AESWA] 
    from [dbo].[TimeValue] 
    where [ApplicationID] = @AppID 
      and [Time] between @DateFrom and @DateTo 
) 

insert into @tempTable 
(
    PViews, 
    Http400, 
    Http500, 
    PTime, 
    ExclSimul, 
    DispSimul, 
    SlowPages, 
    AESWA 
) 
select 
    PViews, 
    Http400, 
    Http500, 
    PTime, 
    ExclSimul, 
    DispSimul, 
    SlowPages, 
    AESWA 
from [ThresholdCTE] 


SELECT * FROM @tempTable; 
+0

無效的列名'PViews'等 – Sanprof 2014-12-05 14:35:00

相關問題