我有一個存儲過程,我想在執行時獲取reportdate
。執行存儲過程導致錯誤
我傳遞一個參數給存儲過程來執行它。我將它傳遞這樣
exec UserReportData '10-06-2016'
,但我得到一個錯誤:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
這是我的存儲過程:
ALTER PROCEDURE [dbo].[UserReportData]
@As_ONDATE Datetime
AS
BEGIN
DECLARE @REPORTDATE datetime
--DECLARE @OPENING INT
SELECT *
INTO #temptable
FROM
(SELECT
a.CUser_id, b.User_Id, a.U_datetime AS REPORTDATE
FROM
inward_doc_tracking_trl a, user_mst b
WHERE
a.CUser_id = b.mkey
AND CONVERT(varchar(50), a.U_datetime, 103) = @As_ONDATE) AS x
DECLARE Cur_1 CURSOR FOR
SELECT CUser_id, User_Id
FROM #temptable
OPEN Cur_1
DECLARE @CUser_id INT
DECLARE @User_Id INT
FETCH NEXT FROM Cur_1 INTO @CUser_id, @User_Id
WHILE (@@FETCH_STATUS = 0)
BEGIN
SELECT CONVERT(varchar(50), U_datetime, 103)
FROM inward_doc_tracking_trl
WHERE CONVERT(varchar(50), U_datetime, 103) = @As_ONDATE
UPDATE #temptable
SET REPORTDATE = @REPORTDATE
WHERE CUser_id = @CUser_id
AND User_Id = @User_Id
FETCH NEXT FROM Cur_1 INTO @CUser_id, @User_Id
END
CLOSE Cur_1
DEALLOCATE Cur_1
SELECT * FROM #temptable
DROP TABLE #temptable
END
請幫助我知道什麼是錯誤的原因。
http://stackoverflow.com/questions/5466100/conversion-of-a-varchar-data-type-to-a-datetime-data-type-resulted-in-an-out-of – mohan111
@ mohan111:我已經嘗試通過轉換它的鏈接,但仍然執行時它給出了相同的錯誤 – BNN
我會建議確認'inward_doc_tracking_trl.U_datetime'是日期時間。看起來可疑的是,您將'U_datetime'轉換爲'varchar(50)',然後將其與作爲日期時間的@As_ONDATE進行比較。另外,您可以考慮更新您的連接語法以使用更標準的「內部連接」語法。 – user212514