|
嗨,我在SQL Server以下内容存储在 2005中proc:
5 l( r8 `7 Q! k) P: W$ FALTER PROCEDURE [dbo].[slot_sp_EngineerTimeslots_Group] @PROPREF VARCHAR(50) @PRIORITYCODE VARCHAR(3), @JOBLENGTH INT = 0, @TIMESLOT VARCHAR(3)ASSET NOCOUNT ON DECLARE @TOTALDAYS INT DECLARE @TOTALDAYSTARGET INT DECLARE @COUNTER INT DECLARE @STARTTIME DATETIME DECLARE @MIDDAYTIME DATETIME DECLARE @ENDTIME DATETIME DECLARE @STARTDATE DATETIME DECLARE @iSTARTDATE DATETIME DECLARE @CONTRACT VARCHAR(10) SET @iSTARTDATE = GETDATE() SET @STARTDATE = CONVERT(DATETIME,CONVERT(VARCHAR(10),@iSTARTDATE,120) ' 00:00:00',120) SELECT @CONTRACT = CONTRACT FROM [tbl_property] WHERE [PROPREF] = @PROPREF-- Get the contract Start/MidDay/End timesSelect @STARTTIME = CONVERT(VARCHAR(10),@STARTDATE,120) CONVERT(VARCHAR(5),ContractStartTime,108), @MIDDAYTIME = CONVERT(VARCHAR(10),@STARTDATE,120) CONVERT(VARCHAR(5),ContractMiddayTime, @ENDTIME = CONVERT(VARCHAR(10),@STARTDATE,120) CONVERT(VARCHAR(5),ContractEndTime,108) From [tbl_contract]WHERE [tbl_contract].[CONTRACT]=@CONTRACT-- Get Priority TimesSelect @TOTALDAYS = CASE WHEN ROUND(NEARCOMPLETEDURATION/24,0) = @STARTDATE)--Get Details of Free time form schedule table INSERT #TempEngineer SELECT slot_tbl_Schedule.Engineer AS Engineer, slot_tbl_Schedule.BookedDate AS BookDate, slot_tbl_Schedule.FromTime AS FromTime, ISNULL(slot_tbl_Schedule.ToTime,CONVERT(VARCHAR(10),@STARTDATE,120) CONVERT(VARCHAR(5),ENDTIME,108)) AS ToTime, @TOTALDAYS AS TotalDays, NULL AS JobMins, NULL AS MinPMTime, NULL AS BTime FROM SELECT Engineer, BookedDate, ToTime AS FromTime, (SELECT MIN(fromTime) FROM slot_tbl_Schedule x WHERE x.engineer = s1.engineer AND x.bookedDate = s1.bookedDate AND x.fromTime >= s1.ToTime) AS ToTime FROM slot_tbl_Schedule s WHERE CONVERT(DATETIME,CONVERT(VARCHAR(10),bookedDate,120),120) = @STARTDATE UNION ALL SELECT e2.Engineer,s2.BookedDate, CONVERT(VARCHAR(10),@STARTDATE,120) CONVERT(VARCHAR(5),e2.StartTime,114) AS FromTime,MIN(s2.FromTime) AS ToTime FROM tbl_engineer e2 INNER JOIN slot_tbl_Schedule s2 ON s2.engineer = e2.engineer WHERE CONVERT(DATETIME,CONVERT(VARCHAR(10),s2.bookedDate,120),120) = @STARTDATE GROUP BY e2.Engineer,s2.BookedDate,e2.StartTime HAVING MIN(s2.FromTime) > e2.StartTime slot_tbl_Schedule INNER JOIN dbo.tbl_engineer engineer ON engineer.ENGINEER = slot_tbl_Schedule.Engineer Select @COUNTER = @COUNTER 1 Select @STARTDATE = DATEADD(Day,1,@STARTDATE) END -- While EndEND -- Temp Table End--Data from above select statements which is in the temp table --CREATE TABLE #TempEngineerGroup AppointmentType VARCHAR(50), BookedDate DateTime, BookedFromTime DateTime, BookedToTime DateTime, TotalDays INT, JobMins INT )IF @TIMESLOT = 'AM INSERT INTO [#TempEngineerGroup] SELECT 'FreeTime' AS [AppointmentType],BookedDate,BookedFromTime,BookedToTime,TotalDays,DATEDIFF(mi,BookedFromTime,BookedToTime) AS [JobMins] FROM #TempEngineer WHERE (DATEDIFF(mi,BookedFromTime,BookedToTime) >= @JOBLENGTH) AND (DATEDIFF(mi,BookedFromTime,BookedToTime) = @JOBLENGTH) AND (DATEDIFF(mi,BookedFromTime,BookedToTime) = CONVERT(DATETIME,CONVERT(VARCHAR(10),BookedFromTime,120) CONVERT(VARCHAR(5),@MIDDAYTIME,114),120) Then BookedFromTime WHEN BookedFromTime = 0 THEN CONVERT(DATETIME,CONVERT(VARCHAR(10),BookedFromTime,120) CONVERT(VARCHAR(5),@MIDDAYTIME,114),120) WHEN BookedFromTime > CONVERT(DATETIME,CONVERT(VARCHAR(10),BookedFromTime,120) CONVERT(VARCHAR(5),@MIDDAYTIME,114),120) AND IsNull(PMAppointments,0) >= 0 THEN BookedFromTime END) AS MinPMTime FROM SELECT TempEngineer,BookedDate,BookedFromTime,BookedToTime,TotalDays,DATEDIFF(mi,BookedFromTime,BookedToTime) AS JobMins, CASE WHEN convert(d" T* `! q0 K8 F' @( s, Q
atetime,convert(varchar(5),BookedFromTime,108),120) >= convert(datetime,convert(varchar(5),@ENDTIME,108),120) THEN DATEDIFF(mi,convert(datetime,convert(varchar(5),BookedFromTime,108),120),convert(datetime,convert(varchar(5),@ENDTIME,108),120))/@JOBLENGTH WHEN convert(datetime,convert(varchar(5),BookedToTime,108),120) >= convert(datetime,convert(varchar(5),@MIDDAYTIME,108),120) THEN DATEDIFF(mi,convert(datetime,convert(varchar(5),@MIDDAYTIME,108),120),convert(datetime,convert(varchar(5),BookedToTime,108),120))/@JOBLENGTH END As PMAppointments FROM #TempEngineer INSIDEQUERY_A GROUP BY TempEngineer,BookedDate,BookedFromTime,BookedToTime,TotalDays,JobMins INSIDEQUERY_B GROUP BY TempEngineer,BookedDate,BookedFromTime,BookedToTime,TotalDays,JobMins,MinPMTime OUTSIDEQUERY WHERE (DATEDIFF(mi,BookedFromTime,BookedToTime) >= @JOBLENGTH) AND (DATEDIFF(mi,BookedFromTime,BookedToTime) = @WC_BookedFromTime BEGIN -- Change Start Time SET @WC_BookedFromTime = @C1_BookedFromTime SET @Saved = 0 END ELSE IF @C1_BookedDate = @WC_BookedDate AND @C1_AppointmentType = @WC_AppointmentType AND @C1_BookedFromTime = @WC_BookedFromTime BEGIN -- Change End Time SET @WC_BookedToTime = @C1_BookedToTime SET @Saved = 0 END ELSE IF @C1_BookedDate = @WC_BookedDate AND @C1_AppointmentType = @WC_AppointmentType AND @C1_BookedFromTime @WC_BookedToTime BEGIN -- New INSERT INTO [#TempEngineerGroupMain] ([AppointmentType],[BookedDate],[BookedFromTime],[BookedToTime],[TotalDays],[JobMins]) VALUES ( @WC_AppointmentType,@WC_BookedDate,@WC_BookedFromTime,@WC_BookedToTime,@WC_TotalDays,DATEDIFF(mi,@WC_BookedFromTime,@WC_BookedToTime)) SET @WC_AppointmentType = @C1_AppointmentType SET @WC_BookedDate = @C1_BookedDate SET @WC_BookedFromTime = @C1_BookedFromTime SET @WC_BookedToTime = @C1_BookedToTime SET @WC_TotalDays = @C1_TotalDays SET @WC_JobMins = @C1_JobMins SET @Saved = END END FETCH NEXT FROM c1 INTO @C1_AppointmentType,@C1_BookedDate,@C1_BookedFromTime,@C1_BookedToTime,@C1_TotalDays,@C1_JobMinsENDIF @Saved = 0 INSERT INTO [#TempEngineerGroupMain] ([AppointmentType],[BookedDate],[BookedFromTime],[BookedToTime],[TotalDays],[JobMins]) VALUES ( @WC_AppointmentType,@WC_BookedDate,@WC_BookedFromTime,@WC_BookedToTime,@WC_TotalDays,DATEDIFF(mi,@WC_BookedFromTime,@WC_BookedToTime))CLOSE c1DEALLOCATE c1SELECT *FROM [#TempEngineerGroupMain]问题是,虽然应该从linq的temp表中返回行,但返回行int,有没有理由解释为什么会发生这种情况?1 u) O5 \7 @: |8 I- w
$ w) v; i2 x4 m3 p; N
解决方案:
1 H2 D, j6 {" @' Y# I 一个常见的原因是存储过程中的元数据非常差,特别是对于带分支的复杂代码。外部代码很难真正理解正在选择/返回的内容。
; f4 x5 l% t* ~3 b& e m* Y* B! {一个常见的技巧是用麻木的简单东西(即简单返回所需模式的任何样本数据)替换存储过程,然后交换回实际代码。此外,如果可能,请考虑交换UDF-
3 A; R: s2 u0 Z0 k& b4 a因为UDF架构元数据更强。
+ H' v* h6 S2 O! R当然,您也可以手动编辑可以正常工作的类似内容DBML。
$ [. w. G6 V$ Y/ Z' k+ V! P顺便说一句-现有代码可能有很多问题,包括(但不限于)DML /
8 R0 {/ ?9 T9 ?$ [- JDDL转换(强制重新编译),多次更新同一表(强制选择性重新编译)-更不用说游标用法了(可能没必要)。(@table而不是#table)也可能有用。我不打算重写。注意你可能想看的东西。 |
|