如何为这个SCENARIO写SP?

时间:2011-06-01 15:09:42

标签: asp.net sql-server-2005

我在数据库中有这样的表

SlNo
Driver_Id
Sun_StartTime
Sun_EndTime
Mon_StartTime
Mon_EndTime
Tue_StartTime
Tue_EndTime 
Wed_StartTime
Wed_EndTime
Thur_StartTime
Thur_EndTime
Fri_StartTime
Fri_EndTime
Sat_StartTime
Sat_EndTime

以上是我数据库中的字段

但是在重新开始的时候我必须像那样重新开始

Driver_Id | Sun_StartTime  | Sun_EndTime
Driver_Id | Mon_StartTime  | Mon_EndTime
Driver_Id | Tue_StartTime  | Tue_EndTime
Driver_Id | Wed_StartTime  | Wed_EndTime
Driver_Id | Thur_StartTime | Thur_EndTime
Driver_Id | Fri_StartTime  | Fri_EndTime
Driver_Id | Sat_StartTime  | Sat_EndTime

这是我必须将一条记录分成7条记录 我怎么能用存储过程做到这一点。 请帮我 提前谢谢

3 个答案:

答案 0 :(得分:0)

您可以使用Union Query获取此类结果。但你没有提到where子句。

Create Proc testing
AS

Select
driverID, Mon_StartTime,Mon_EndTime From YourTableName

Union

Select
    driverID, Tue_EndTime, Tue_EndTime From YourTableName

Union

Select
    driverID, Wed_StartTime, Wed_EndTime From YourTableName

Union

Select
    driverID, Thur_StartTime, Thur_EndTime From YourTableName

Union

Select
    driverID, Fri_StartTime, Fri_EndTime From YourTableName

Union

Select
    driverID, Sat_StartTime, Sat_EndTime From YourTableName

Union 

    Select
    driverID, Sun_StartTime, Sun_EndTime From YourTableName

答案 1 :(得分:0)

这样的事情可以让你开始朝着正确的方向前进。

SELECT Driver_Id, Sun_StartTime AS StartTime, Sun_EndTime as EndTime, 1 AS DayOfWeek
    FROM YourTable
    WHERE Driver_Id = @YourDriverId
UNION ALL
SELECT Driver_Id, Mon_StartTime AS StartTime, Mon_EndTime as EndTime, 2 AS DayOfWeek
    FROM YourTable
    WHERE Driver_Id = @YourDriverId
UNION ALL
...
SELECT Driver_Id, Sat_StartTime AS StartTime, Sat_EndTime as EndTime, 7 AS DayOfWeek
    FROM YourTable
    WHERE Driver_Id = @YourDriverId
ORDER BY DayOfWeek

答案 2 :(得分:0)