I can't figure out why this error is occurring. Can anyone else help me spot the error? I'm trying to merge into a temp table, and getting the error
Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query. The error is identified as occurring on the line where the MERGE starts.
Query:
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)
;
Based on a guess that ProviderBibDateAdded
is typed as integer and you are tying to stuff a datetime into it, Try changing your update statement to this:
UPDATE
SET ProviderCustNum = pt.ProviderCustNum,
ProviderDivID = pt.ProviderDivID,
ProviderBibDateAdded = Cast(pt.ProviderBibDateAdded as Integer),
ProviderQtyOwned = pt.ProviderQtyOwned
and your insert statement to this:
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,
cast(pt.ProviderBibDateAdded as Integer),
pt.ProviderQtyOwned)
If this is indeed the issue, then you might instead create your temp table with explicit data types to start off with, before you insert anything into it.
Declare @TitleData (
KTSID integer not null,
FLRID integer not null,
SiteShelfID integer not null,
SiteCustNum varChar(??),
SiteDivID integer not null,
SiteBibDateAdded Datetime not null,
SiteQtyOwned smallInt,
ProviderShelfID integer not null,
ProviderCustNum varChar(??) Null,
ProviderDivID Integer null,
ProviderBibDateAdded Datetime null,
ProviderQtyOwned null)
Insert @TitleData (KTSID, FLRID, SiteShelfID,
SiteCustNum, SiteDivID, SiteBibDateAdded,
SiteQtyOwned, ProviderShelfID)
SELECT BookID, ResourceID, ls.ID,
ls.CustomerNumber, ls.DivisionID,
st.DateCreated, st.TotalCopies,
ls.ParentLibraryShelfID
From dapadmin.ShelfTitle st
JOIN dapadmin.LibraryShelf ls
ON st.LibraryShelfID = ls.ID
JOIN #TitlesToInclude tti
ON st.ID = tti.ShelfTitleId