2

我无法弄清楚为什么会发生此错误。其他人可以帮我找出错误吗?我正在尝试合并到临时表中,并收到错误

不允许从数据类型 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)
;
4

1 回答 1

1

基于ProviderBibDateAdded输入为整数的猜测并且您正在将日期时间填充到其中,尝试将您的更新语句更改为:

UPDATE
    SET ProviderCustNum      = pt.ProviderCustNum,
        ProviderDivID        = pt.ProviderDivID,
        ProviderBibDateAdded = Cast(pt.ProviderBibDateAdded as Integer),
        ProviderQtyOwned     = pt.ProviderQtyOwned

和你的插入语句:

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)

如果这确实是问题所在,那么您可以改为创建具有显式数据类型的临时表,然后再将任何内容插入其中。

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
于 2014-05-15T18:25:24.400 回答