ALTER PROCEDURE [dbo].[TRN_Hold_GetData]
@XMLSearch XML = '<DocumentElement><TRN_Hold_GetData_Custom><HoldId>0</HoldId><FromDate>17-3-2017</FromDate><ToDate>17-2-2017</ToDate></TRN_Hold_GetData_Custom></DocumentElement>'
AS
BEGIN
DECLARE @HoldId INT = 0,
@HoldNo VARCHAR(50) = '',
@Party VARCHAR(100) = '',
@StoneNo VARCHAR(50) = '',
@FromDate VARCHAR(50) = '',
@ToDate VARCHAR(50) = '',
@HoldStatus VARCHAR(20) = '',
@FilterQry NVARCHAR(MAX) = '',
@FinalQry NVARCHAR(MAX) = ''
SELECT
@HoldNo = doc.col.value('HoldNo[1]', 'VARCHAR(50)'),
@Party = doc.col.value('Party[1]', 'VARCHAR(MAX)'),
@StoneNo = doc.col.value('StoneNo[1]', 'VARCHAR(MAX)'),
@FromDate = doc.col.value('FromDate[1]', 'VARCHAR(11)'),
@ToDate = doc.col.value('ToDate[1]', 'VARCHAR(11)'),
@HoldStatus = doc.col.value('HoldStatus[1]', 'VARCHAR(20)')
FROM
@XMLSearch.nodes('/DocumentElement/TRN_Hold_GetData_Custom') doc(col)
SELECT
convert(varchar(11), cast(OnHoldDate as datetime), 103),
ISNULL(TH.HoldId, '') AS HoldId,
ISNULL(TH.HoldNo, '') AS HoldNo,
ISNULL(TH.PartyId, '') AS PartyId,
ISNULL(MP.Party, '') AS Party,
ISNULL(TH.OnHoldDate, '') AS HoldDate,
ISNULL(TH.ExpReleaseDays, '') AS ExpReleaseDays,
ISNULL(TH.ExpReleaseDate, '') AS ExpReleaseDate,
ISNULL(TH.TotalPkt, '') AS TotalPkt,
ISNULL(TH.TotalCts, '') AS TotalCts,
HOLDSTONE, RELEASESTONE,
ISNULL(TH.TotalAmount, '') AS TotalAmount,
ISNULL(TH.HoldById, '') AS HoldById,
ISNULL(TH.Remark, '') AS Remark,
ISNULL(MEmp.firstname, '') + ' ' + ISNULL(MEmp.middlename, '') + ' ' + ISNULL(MEmp.lastname, '') AS HoldUser,
ISNULL(MEmp.firstname, '') + ' ' + ISNULL(MEmp.middlename, '') + ' ' + ISNULL(MEmp.lastname, '') AS UnHoldUser
FROM
dbo.TRN_Hold TH WITH (NOLOCK)
INNER JOIN
(SELECT SUM(CASE WHEN HoldStatus = 0 THEN 1
ELSE 0
END) AS HOLDSTONE, SUM(CASE WHEN HoldStatus = 1 THEN 1
ELSE 0
END) AS RELEASESTONE, HoldId
FROM TRN_Hold_Detail
GROUP BY HoldId) AS THD ON TH.HoldId = THD.HoldId
INNER JOIN TRN_Hold_Detail AS HDD WITH (NOLOCK) ON HDD.HoldId = THD.HoldId
INNER JOIN mst_Stone AS MS WITH (NOLOCK) ON HDD.StoneId = MS.Stone_id
LEFT JOIN MST_Party MP ON TH.PartyId = MP.party_id
LEFT JOIN MST_Employee MEmp ON TH.HoldById = MEmp.employee_id
WHERE HoldNo = (
CASE WHEN @HoldNo = '' or @HoldNo is null THEN HoldNo ELSE @HoldNo END) AND
convert(varchar(11),cast(OnHoldDate as datetime),101)
between
CASE WHEN @FromDate= ''
THEN OnHoldDate
ELSE convert(varchar(11),@FromDate,101)
END
AND
CASE WHEN @ToDate = ''
THEN OnHoldDate
ELSE convert(varchar(11),@ToDate,101)
END
print @FromDate
print @ToDate
--????????????????????????????????????????????????????????????????
--AND (CASE WHEN convert(varchar(50),cast(@FromDate as datetime),103)= ''
-- THEN convert(varchar(50),cast(@FromDate as datetime),103)
-- ELSE OnHoldDate
-- END) >= convert(varchar(50),cast(@FromDate as datetime),103)
--AND (CASE WHEN convert(varchar(50),cast(@ToDate as datetime),103) = ''
-- THEN convert(varchar(50),cast(@ToDate as datetime),103)
-- ELSE OnHoldDate
-- END) <= convert(varchar(50),cast(@ToDate as datetime),103)
--????????????????????????????????????????????????????????????????
--AND (@StoneNo = '' OR ms.Stone_no = @stoneNo)
-- --realse
--AND ((@HoldStatus = 'RELEASE' AND ISNULL(THD.HOLDSTONE, 0) = 0 AND ISNULL(THD.RELEASESTONE, 0) <> 0)
-- --Hold
--OR ( @HoldStatus = 'HOLD' AND ISNULL(THD.HOLDSTONE, 0) <> 0 AND ISNULL(THD.RELEASESTONE, 0) = 0)
-- --Partial
--OR (@HoldStatus = 'PARTIAL' AND ISNULL(THD.HOLDSTONE, 0) <> 0 AND ISNULL(THD.RELEASESTONE, 0) <> 0)
--OR @HoldStatus = 'ALL'
--)
-- print @FromDate
-- print convert(nvarchar(20), @FromDate ,106)
--print convert(nvarchar(20), @ToDate ,103)
--OnHoldDate between (case when @FromDate ='' then OnHoldDate else @FromDate end)
--and (case when @ToDate = '' then OnHoldDate else @ToDate end)
END
這是我存儲的過程;當我執行它,我得到以下錯誤給出此SQL錯誤的解釋「將varchar數據類型轉換爲導致超出範圍值的日期時間數據類型」
消息242,級別16,狀態3過程TRN_Hold_GetData,第45行
varchar數據類型爲datetime數據類型的轉換導致的失 - 範圍值
「OnHoldDate」的價值是什麼? –
@mayurgnu我認爲複製粘貼HTML解析失敗了你。正在傳入的XMLSearch變量的表面值看起來不是有效的XML,因此不太可能是可測試的。或者添加一個SQL小提琴並在問題中引用它。 –
***爲什麼***你是否聲明像'@ FromDate'這樣的變量,它們是**明顯的**日期相關的字符串?你應該**總是**使用**最合適的**數據類型 - 在這裏,你應該使用'DATE'(如果它只是一個'DATE'),或者'DATETIME2(3)'如果它是一個日期和時間 –