当我尝试将日期时间字符串(yyyy-MM-dd HH:mm TT)转换为Cursor内存储过程中的SQL DateTime时,我面临如下错误。
从字符串转换日期和/或时间时转换失败。
我在下面粘贴了我的商店程序,并在INSERT
语句中的第131行错误抛出,转换了2个变量@var1
& @var2
到DATETIME。
SQL Server版本:
Microsoft SQL Server 2012(SP1) - 11.0.3128.0(X64)2012年12月28日20:23:12版权所有(c)Windows NT 6.2上的Microsoft Corporation Developer Edition(64位)(Build 9200:)
测试变量值:
@Log = "8/6/2015 1:37:00 PM"
@stBreakTimeStart = "1:00 PM"
@stBreakTimeEnd = "2:00 PM"
近似@var1
& @var2
测试值:
@var1 = "2015-08-06 1:00 PM"
@var2 = "2015-08-06 2:00 PM"
我的商店程序:
-- =============================================
-- Author: KG
-- Create date: 15.09.13
-- Description: Insert all excel file data in EmpInOutAuto table
-- EXEC [insertEmpInOutAuto] '<EmpData><AttendanceData><stEmployeeName>Dexterity</stEmployeeName><inStaffNo>25</inStaffNo><stDepartment>Management</stDepartment><inDevNo>1</inDevNo><dtLog>8/6/2015 1:37:00 PM</dtLog><stState></stState><stMode>In</stMode></AttendanceData></EmpData>',1
-- =============================================
ALTER PROCEDURE [dbo].[insertEmpInOutAuto]
(
@ImportXML XML,
@inTrackExcelSheetId INT
)
AS
BEGIN
DECLARE @intErrorCode INT;
BEGIN TRAN
INSERT INTO EmpInOutAuto
(
[stEmployeeName],
[inStaffNo],
[stDepartment],
[inDevNo],
[dtLog],
[stState],
[stMode],
[inTrackExcelSheetId]
)
SELECT
Col.value('stEmployeeName[1]', 'NVARCHAR(200)') AS NAME,
Col.value('inStaffNo[1]', 'INT') AS StaffNo,
Col.value('stDepartment[1]', 'NVARCHAR(50)') AS Dept,
Col.value('inDevNo[1]', 'INT') AS DevNo,
Col.value('dtLog[1]', 'DATETIME') AS DateTimeLog,
Col.value('stState[1]', 'NVARCHAR(200)') AS StateC,
Col.value('stMode[1]', 'NVARCHAR(50)') AS Mode,
@inTrackExcelSheetId
FROM
@ImportXML.nodes('//EmpData/AttendanceData') Tab(Col)
-- Get BreakTime Data
DECLARE @stBreakTimeStart NVARCHAR(50);
DECLARE @stBreakTimeEnd NVARCHAR(50);
DECLARE @stFinalBTStart NVARCHAR(20);
DECLARE @stFinalBTEnd NVARCHAR(20);
SELECT @stBreakTimeStart=stStartTime, @stBreakTimeEnd=stEndTime
FROM BreakTime;
-- End
DECLARE @EmployeeName NVARCHAR(200);
DECLARE @StaffNo INT;
DECLARE @Dept NVARCHAR(50);
DECLARE @DevNo INT;
DECLARE @Log DATETIME;
DECLARE @State NVARCHAR(200);
DECLARE @Mode NVARCHAR(50);
DECLARE MyCursor CURSOR FOR
SELECT [stEmployeeName],
[inStaffNo],
[stDepartment],
[inDevNo],
[dtLog],
[stState],
[stMode]
FROM
[EmpInOutAuto]
WHERE
ISNULL([stMode],'Auto')<>'Auto' AND
[inTrackExcelSheetId]=@inTrackExcelSheetId
ORDER BY stEmployeeName ASC, dtLog ASC
OPEN MyCursor
FETCH NEXT FROM MyCursor INTO @EmployeeName, @StaffNo, @Dept, @DevNo, @Log, @State, @Mode
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @inEmployeeId INT;
DECLARE @dtBTStart DATETIME;
DECLARE @dtBTEnd DATETIME;
SET @inEmployeeId = (SELECT inEmployeeId FROM Employee WHERE stEmployeeName=@EmployeeName);
IF @inEmployeeId IS NULL
BEGIN
INSERT INTO Employee
(
[inEmpRoleId],
[stEmployeeCode],
[stUserName],
[stPassword],
[stEmployeeName],
[inStaffNo],
[stDepartment],
[inDevNo],
[stState],
[inTrackExcelSheetId]
)
VALUES
(
(CASE @Dept
WHEN 'Management' THEN 2
ELSE 3
END),
@StaffNo,
@EmployeeName,
(SELECT dbo.getRandomPassword()),
@EmployeeName,
@StaffNo,
@Dept,
@DevNo,
@State,
@inTrackExcelSheetId
)
SET @inEmployeeId = Scope_Identity();
END
DECLARE @var1 NVARCHAR(50);
SET @var1 = CONVERT(NVARCHAR(10),CONVERT(DATE, @Log)) + ' ' + @stBreakTimeStart;
print(@var1);
DECLARE @var2 NVARCHAR(50);
SET @var2 = CONVERT(NVARCHAR(10),CONVERT(DATE, @Log)) + ' ' + @stBreakTimeEnd;
print(@var2);
INSERT INTO EmpInOut
(
[inEmployeeId],
[dtInOut],
[stMode],
[dtBreakTimeStart],
[dtBreakTimeEnd],
[inNumber],
[inTrackExcelSheetId]
)
VALUES
(
@inEmployeeId,
@Log,
@Mode,
CONVERT(DATETIME,@var1),
CONVERT(DATETIME,@var2),
(SELECT ISNULL(MAX(inNumber),0)+1 FROM EmpInOut WHERE inEmployeeId=@inEmployeeId),
@inTrackExcelSheetId
)
FETCH NEXT FROM MyCursor INTO @EmployeeName, @StaffNo, @Dept, @DevNo, @Log, @State, @Mode
END
CLOSE MyCursor
DEALLOCATE MyCursor
SELECT 'Successfull!'
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
COMMIT TRAN
PROBLEM:
IF (@intErrorCode <> 0) BEGIN
SELECT @intErrorCode + 'Unexpected error occurred!'
ROLLBACK TRAN
END
END