因此,我有一些包含數據的表,我需要根據舊數據獲取具有新數據的新表,然後將它們插入臨時表以供下一次處理。從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'。
你不能指表別名在它的定義相同'select'。如果你喜歡,你可以使用子查詢,或者重複表達式。 – 2014-12-05 14:23:16
我認爲有可能以某種方式避開 – Sanprof 2014-12-05 14:28:27
是的。這只是分配別名的另一種方式。 – 2014-12-05 14:58:25