這個查詢給了我想要的結果,但我無法每次都運行這個查詢。這2個循環耗費了我所有的費用。所以我需要實現類似view.But的東西,但是邏輯中包含臨時表那麼,還有沒有其他的方式來存儲這個結果或者改變查詢,這樣會使我花費更少。如何調整以下查詢?
DECLARE @Temp TABLE (
[SiteID] VARCHAR(100)
,[StructureID] INT
,[row] DECIMAL(4, 2)
,[col] DECIMAL(4, 2)
)
DECLARE @siteID VARCHAR(100)
,@structureID INT
,@struct_row INT
,@struct_col INT
,@rows_count INT
,@cols_count INT
,@row INT
,@col INT
DECLARE structure_cursor CURSOR
FOR
SELECT StructureID
,SiteID
,Cols/8.5 AS Cols
,Rows/11 AS Rows
FROM Structure
WHERE SellerID = 658 --AND StructureID = 55
OPEN structure_cursor
FETCH NEXT
FROM structure_cursor
INTO @structureID
,@siteID
,@struct_col
,@struct_row
SELECT @rows_count = 1
,@cols_count = 1
,@row = 1
,@col = 1
WHILE @@FETCH_STATUS = 0
BEGIN
WHILE @row <= @struct_row
BEGIN
WHILE @col <= @struct_col
BEGIN
--PRINT 'MEssage';
INSERT INTO @Temp (
SiteID
,StructureID
,row
,col
)
VALUES (
@siteID
,@structureID
,@rows_count
,@cols_count
)
SET @cols_count = @cols_count + 1;
SET @col = @col + 1;
END
SET @cols_count = 1;
SET @col = 1;
SET @rows_count = @rows_count + 1;
SET @row = @row + 1;
END
SET @row = 1;
SET @col = 1;
SET @rows_count = 1;
FETCH NEXT
FROM structure_cursor
INTO @structureID
,@siteID
,@struct_col
,@struct_row
END
CLOSE structure_cursor;
DEALLOCATE structure_cursor;
SELECT * FROM @Temp
請張貼樣本數據和預期的輸出。 –
我實際上將表1轉換爲上表中的圖2.因此,我可以在我的查詢中進一步使用第二個表來加入並獲得輸出 –