0
我有2個表名爲Circuits and Detections Circuits表包含所有可能電路的記錄。 檢測表包含Circuits表中電路檢測的記錄。 我想創建一個包含電路列表和每個特定電路的每種類型的檢測計數的視圖。 我想出瞭如下代碼,但它不工作動態創建視圖表
CREATE VIEW Result
AS
declare @circuitId INT
declare my_cursor Cursor for
SELECT circuitId from Circuits
OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @circuitId
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @circuitId as CircuitId ,
(SELECT COUNT(*) AS AliveCount FROM dbo.Detections WHERE dbo.Detections.circuitId = @circuitId AND dbo.detections.health = 'Alive') AS Alive,
(SELECT COUNT(*) AS DeadCount FROM dbo.Detections WHERE dbo.Detections.circuitId = @circuitId AND dbo.detections.health = 'Dead') AS Dead
FETCH NEXT FROM my_cursor into @circuitId
END
CLOSE my_cursor
DEALLOCATE my_cursor
我不知道是否有可能在這種情況下使用的視圖。任何建議都非常感謝。謝謝。
工作十分感謝。 – stoney78us 2014-10-03 18:23:39