2012-11-14 94 views
0

我有一個存儲過程,每個時隙都會返回一天中的時隙。SQL Server存儲過程產生日曆

ALTER PROCEDURE [dbo].[TimeSlots_Sel] 
    ( 
     @dateReqd date, 
     @typeReqd varchar(5) = 'ATI' 
    ) 
    AS 
    BEGIN 
     SET NOCOUNT ON; 
     DECLARE @timeNow time(0) = CONVERT(time(0),GETDATE()) 

     IF @dateReqd >= CONVERT(date, GETDATE()) 
       BEGIN 
         DECLARE @tblWIP AS dbo.tvpWIP 

         INSERT INTO @tblWIP 
           SELECT [Type], [status_id], Scheduled_Call 
           FROM dbo.WIP 
           WHERE status_id = 70 
             AND Scheduled_Call >= @dateReqd 
             AND Scheduled_Call < DATEADD(dd,1,@dateReqd) 

         SELECT ts.SlotTime, 
           dbo.funCapacity(@tblWIP, @dateReqd, ts.SlotTime, @typeReqd) AS [Capacity] 
         FROM dbo.TimeSlots ts 
         WHERE NOT EXISTS 
         ( 
           SELECT * 
           FROM dbo.Downtime dt 
           WHERE dbo.funStrToDateTime(YEAR(@dateReqd),MONTH(@dateReqd),DAY(@dateReqd), DATEPART(HOUR, ts.SlotTime),DATEPART(MINUTE, ts.SlotTime)) 
             BETWEEN 
               dbo.funStrToDateTime(YEAR(@dateReqd),dt.StartMonth, dt.StartDay, dt.StartHour, dt.StartMin) 
             AND dbo.funStrToDateTime(YEAR(@dateReqd),dt.EndMonth, dt.EndDay, dt.EndHour, dt.EndMin) 
         ) 
         AND dbo.funCapacity(@tblWIP, @dateReqd, ts.SlotTime, @typeReqd) > 0 
         AND 
         ( 
           @dateReqd > GETDATE() 
           OR 
           ts.SlotTime >= @timeNow 
         ) 
         ORDER BY SlotTime 
       END 
    END 

所以我得到這樣

SlotTime Capacity 
-------------------- 
09:00:00 3 
09:30:00 3 
10:00:00 3 
10:30:00 3 
11:00:00 3 
11:30:00 2 
12:00:00 2 
12:30:00 2 

信息,但現在我需要顯示出未來7天日期容量的網格。任何想法如何,我可以修改存儲過程返回這樣的事情

SlotTime Sun 14/11/2010 Mon 15/11/2010 Tue 16/11/2010 Wed 17/11/2010 Thu 18/11/2010 Fri 19/11/2010 Sat 20/11/2010 
09:00:00 3    3    3    1    3    1    1 
09:30:00 3    3    3    3    3    3    3 
10:00:00 3    3    3    1    3    3    3 
10:30:00 3    3    3    1    3    1    2 
11:00:00 3    3    3    3    3    3    3 
11:30:00 3    3    3    1    3    3    3 

我有一個類似的程序創建日曆該做類似的事情,但我發現它很難發揮我想要的東西。下面是其他存儲的過程:

ALTER PROCEDURE [dbo].[Calendar_Sel] (@StartDate DATE) 
AS 
BEGIN 
     SET NOCOUNT ON 

     DECLARE @EndDate DATE = DATEADD(Day,6,@StartDate) 
     DECLARE @dates VARCHAR(MAX) 
     DECLARE @datevals VARCHAR(MAX) 
     DECLARE @sql NVARCHAR(MAX) 
     DECLARE @ParmDefinition NVARCHAR(100) 

     ;WITH cte AS 
     ( 
       SELECT @StartDate AS DateColumn UNION ALL 
       SELECT DATEADD(Day,1,DateColumn) FROM cte WHERE DateColumn < @EndDate 
     ) 

     SELECT DateColumn INTO #temp FROM cte 

     SET @dates = STUFF((SELECT ',[' + CONVERT(VARCHAR,DateColumn,112) + ']' FROM #temp FOR XML PATH('')),1,1,'') 
     SET @datevals = STUFF((SELECT ',COALESCE([' + CONVERT(VARCHAR,DateColumn,112) + '],'''') AS [' + CONVERT(VARCHAR,DateColumn,112) + ']' FROM #temp FOR XML PATH('')),1,1,'') 

     SET @sql = ' 
       DECLARE @EndDate DATE = DATEADD(Day,6,@StartDate) 
       ;WITH cte AS 
       ( 
         SELECT @StartDate AS DateColumn UNION ALL 
         SELECT DATEADD(Day,1,DateColumn) FROM cte WHERE DateColumn < @EndDate 
       ) 
       ,cte1 AS 
       ( 
       SELECT DISTINCT r.RoomID, CONVERT(VARCHAR,DateColumn,112) AS DateColumn, 
         CAST(CASE  WHEN EXISTS(SELECT * FROM dbo.Payments WHERE BookingID = b.BookingID) THEN 1 ELSE 0  END AS CHAR(1)) 
         + CAST(b.BookingID AS CHAR(15)) + c.Surname AS CompoundKey 
       FROM dbo.Bookings b 
       JOIN dbo.BookingsDetails bd 
         ON b.BookingID = bd.BookingID 
       JOIN dbo.Customers c 
         ON c.CustomerId = b.CustomerId 
       JOIN dbo.Rooms r 
         ON r.RoomId = bd.RoomId 
       JOIN cte 
         ON cte.DateColumn BETWEEN b.DateArrive AND DATEADD(Day,-1,b.DateLeave) 
       ) 
       SELECT Room, 
         Price1 = dbo.funStayPrice(@StartDate,DATEADD(Day,1,@StartDate),RoomTypeID,0), 
         Price2 = dbo.funStayPrice(DATEADD(Day,1,@StartDate),DATEADD(Day,2,@StartDate),RoomTypeID,0), 
         Price3 = dbo.funStayPrice(DATEADD(Day,2,@StartDate),DATEADD(Day,3,@StartDate),RoomTypeID,0), 
         Price4 = dbo.funStayPrice(DATEADD(Day,3,@StartDate),DATEADD(Day,4,@StartDate),RoomTypeID,0), 
         Price5 = dbo.funStayPrice(DATEADD(Day,4,@StartDate),DATEADD(Day,5,@StartDate),RoomTypeID,0), 
         Price6 = dbo.funStayPrice(DATEADD(Day,5,@StartDate),DATEADD(Day,6,@StartDate),RoomTypeID,0), 
         Price7 = dbo.funStayPrice(DATEADD(Day,6,@StartDate),DATEADD(Day,7,@StartDate),RoomTypeID,0), 
         SUBSTRING([' + CONVERT(VARCHAR,@StartDate,112) + '],16,12) AS Entry1, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,1,@StartDate),112) + '],16,12) AS Entry2, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,2,@StartDate),112) + '],16,12) AS Entry3, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,3,@StartDate),112) + '],16,12) AS Entry4, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,4,@StartDate),112) + '],16,12) AS Entry5, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,5,@StartDate),112) + '],16,12) AS Entry6, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,6,@StartDate),112) + '],16,12) AS Entry7, 
         SUBSTRING([' + CONVERT(VARCHAR,@StartDate,112) + '],2,15) AS ID1, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,1,@StartDate),112) + '],2,15) AS ID2, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,2,@StartDate),112) + '],2,15) AS ID3, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,3,@StartDate),112) + '],2,15) AS ID4, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,4,@StartDate),112) + '],2,15) AS ID5, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,5,@StartDate),112) + '],2,15) AS ID6, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,6,@StartDate),112) + '],2,15) AS ID7, 
         SUBSTRING([' + CONVERT(VARCHAR,@StartDate,112) + '],1,1) AS Status1, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,1,@StartDate),112) + '],1,1) AS Status2, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,2,@StartDate),112) + '],1,1) AS Status3, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,3,@StartDate),112) + '],1,1) AS Status4, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,4,@StartDate),112) + '],1,1) AS Status5, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,5,@StartDate),112) + '],1,1) AS Status6, 
         SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,6,@StartDate),112) + '],1,1) AS Status7 
       FROM 
       ( 
         SELECT r.RoomName, r.RoomName + '' ['' + rt.TypeDescription + '']'' AS Room, rt.RoomTypeID, '+ @datevals + ' 
         FROM cte1 
         PIVOT (MAX(CompoundKey) FOR DateColumn IN (' + @dates + ')) AS p 
         RIGHT OUTER JOIN dbo.Rooms r 
           ON r.RoomID = p.RoomID 
         JOIN dbo.RoomTypes rt 
           ON rt.RoomTypeID = r.RoomTypeID 
       ) x 
       ORDER BY CASE WHEN ISNUMERIC(RoomName) = 1 THEN CAST(RoomName AS INT) ELSE 0 END, RoomName'; 

     SET @ParmDefinition = N'@StartDate DATE'; 

     EXEC sp_executesql @sql, @ParmDefinition, 
       @StartDate = @StartDate; 
END 

如果有人可以幫助我回到容量表我將非常感激。我期望在GridView中顯示這些信息,並且已經有列標題,所以我只需要正確的存儲過程和數據。

非常感謝您的幫助。任何建議,我相信會是一個很大的幫助。

回答

2

我試圖簡化您的要求下來了一下,想出這個解決方案。基本上,我們得到一個日期和時間列表交叉加入一個函數來獲得容量(我在這裏使用了一個隨機數字),然後在日期上旋轉。由於這些列是動態的,因此我們需要使用動態查詢來獲取列標題。

希望,你可以使用這個應用到自己的查詢。

CREATE TABLE #TimeSlots (SlotTime TIME) 

INSERT INTO #TimeSlots VALUES ('09:00:00') 
INSERT INTO #TimeSlots VALUES ('09:30:00') 
INSERT INTO #TimeSlots VALUES ('10:00:00') 
INSERT INTO #TimeSlots VALUES ('10:30:00') 
INSERT INTO #TimeSlots VALUES ('11:00:00') 
INSERT INTO #TimeSlots VALUES ('11:30:00') 

CREATE TABLE #DateSlots (Dates DATE) 

INSERT INTO #DateSlots VALUES (GETDATE()) 
INSERT INTO #DateSlots VALUES (DATEADD(DAY, 1, GETDATE())) 
INSERT INTO #DateSlots VALUES (DATEADD(DAY, 2, GETDATE())) 
INSERT INTO #DateSlots VALUES (DATEADD(DAY, 3, GETDATE())) 
INSERT INTO #DateSlots VALUES (DATEADD(DAY, 4, GETDATE())) 
INSERT INTO #DateSlots VALUES (DATEADD(DAY, 5, GETDATE())) 
INSERT INTO #DateSlots VALUES (DATEADD(DAY, 6, GETDATE())) 
INSERT INTO #DateSlots VALUES (DATEADD(DAY, 7, GETDATE())) 


DECLARE @PivotColumnHeaders VARCHAR(MAX) 
SELECT @PivotColumnHeaders = COALESCE(@PivotColumnHeaders + ',[' + CAST(Dates AS VARCHAR) + ']', '[' + CAST(Dates AS VARCHAR) + ']') 
FROM #DateSlots 
WHERE Dates BETWEEN GETDATE() AND DATEADD(DAY, 7, GETDATE()) 

DECLARE @PivotTableSQL NVARCHAR(MAX) 
SET @PivotTableSQL = N' 
    SELECT * 
    FROM (
     SELECT SlotTime, cast(Dates as varchar) Dates, Abs(Checksum(NewId())) % 4 AS Capacity 
     FROM #TimeSlots 
     CROSS JOIN #DateSlots 
     WHERE Dates BETWEEN GETDATE() AND DATEADD(DAY, 7, GETDATE()) 
    ) AS Source 
    PIVOT 
    (
     MAX(Capacity) 
     FOR Dates IN (
      ' + @PivotColumnHeaders + ' 
     ) 
    ) AS PivotTable 
' 

EXECUTE(@PivotTableSQL) 

DROP TABLE #TimeSlots 
DROP TABLE #DateSlots 
+0

這是一個輝煌的幫助貝諾。你真的很棒。非常非常非常感謝你。它似乎工作,所以我只需要顯示網格:) – user1825173