我无法弄清楚为什么会发生此错误。其他人可以帮我找出错误吗?我正在尝试合并到临时表中,并收到错误
不允许从数据类型 datetime 到 int 的隐式转换。使用 CONVERT 函数运行此查询。该错误被识别为发生在 MERGE 开始的行上。
询问:
SELECT
BookID AS KTSID,
ResourceID AS FLRID,
ls.ID AS SiteShelfID,
ls.CustomerNumber AS SiteCustNum,
ls.DivisionID AS SiteDivID,
st.DateCreated AS SiteBibDateAdded,
st.TotalCopies AS SiteQtyOwned,
ls.ParentLibraryShelfID AS ProviderShelfID,
NULL AS ProviderCustNum,
NULL AS ProviderDivID,
NULL AS ProviderBibDateAdded,
NULL AS ProviderQtyOwned
INTO #TitleData
FROM dapadmin.ShelfTitle st
JOIN dapadmin.LibraryShelf ls ON st.LibraryShelfID = ls.ID
JOIN #TitlesToInclude tti ON st.ID = tti.ShelfTitleId
MERGE #TitleData WITH(HOLDLOCK) AS td
USING (SELECT
st.BookID AS KTSID,
st.ResourceID AS FLRID,
st.LibraryShelfID AS SiteShelfID,
ls2.CustomerNumber AS SiteCustNum,
ls2.DivisionID AS SiteDivID,
s.ProviderShelfID,
ls.CustomerNumber AS ProviderCustNum,
ls.DivisionID AS ProviderDivID,
st.DateCreated AS ProviderBibDateAdded,
st.TotalCopies AS ProviderQtyOwned
FROM @ShelfIdsToSearch s
JOIN dapadmin.ShelfTitle st
ON s.ProviderShelfId = st.LibraryShelfID
JOIN #TitleData td
ON td.KTSID = st.BookID
AND td.FLRID = st.ResourceID
AND td.SiteShelfId = s.LibraryShelfId
AND td.ProviderShelfID = st.LibraryShelfID
JOIN #TitlesToInclude tti
ON st.ID = tti.ShelfTitleId
OR s.ProviderHasChanged = 1
JOIN dapadmin.LibraryShelf ls
ON s.ProviderShelfId = ls.ID
JOIN dapadmin.LibraryShelf ls2
ON s.LibraryShelfId = ls.ID
) AS pt
ON td.KTSID = pt.KTSID
AND td.FLRID = pt.FLRID
AND pt.SiteShelfID = td.SiteShelfID
WHEN MATCHED THEN
UPDATE
SET ProviderCustNum = pt.ProviderCustNum,
ProviderDivID = pt.ProviderDivID,
ProviderBibDateAdded = pt.ProviderBibDateAdded,
ProviderQtyOwned = pt.ProviderQtyOwned
WHEN NOT MATCHED THEN
INSERT (KTSID, FLRID, SiteShelfID, SiteCustNum, SiteDivID,
SiteBibDateAdded, SiteQtyOwned, ProviderShelfID,
ProviderCustNum, ProviderDivID,
ProviderBibDateAdded, ProviderQtyOwned)
VALUES (pt.KTSID, pt.FLRID, pt.SiteShelfID, pt.SiteCustNum, pt.SiteDivID,
NULL, NULL, pt.ProviderShelfID,
pt.ProviderCustNum, pt.ProviderDivID,
pt.ProviderBibDateAdded, pt.ProviderQtyOwned)
;