转换因UNION而失败

时间:2020-05-30 19:19:24

标签: sql sql-server

我正在使用SQL Server Management Studio来构建报告问题的查询。

单独使用这些查询可以正常工作,但是一旦通过使用联合添加它们,我就会收到以下错误消息:

第245层状态1的状态1行
将varchar值'OFC'转换为数据类型smallint时转换失败

我尝试在CastConvert列上使用w.wpagep.ppage。我尝试将它们直接用于两个子查询的case语句,这些子查询的结果各不相同,但始终是:

转换时转换失败...

错误。

任何帮助或建议,将不胜感激。

SELECT 
    a.Article,
    a.ARTICLENAME,
    a.Offer,
    a.EBD,
    q.Excl,
    a.OfferYear,
    b.SeasonID,
    CASE 
       WHEN CONCAT(catid,packnum) <> CONCAT(a.offer, a.article) 
          THEN 'Add' 
       WHEN a.EBD = 'B' AND q.Excl = 'BE' 
          THEN 'True' 
       ELSE 'Exclusive'
    END AS Action,
    CASE 
       WHEN CONCAT(a.offer, a.article) = CONCAT(d.ToCat, d.article) 
          THEN 'Y' 
          ELSE 'N'
    END AS [On CN]
FROM 
    Lago_CID_Master a 
INNER JOIN 
    CatalogInfo b ON (a.Offer = b.Catalog) AND (a.OfferYear = b.MailYear)
LEFT JOIN 
    ChangeNoticeCopyFile d ON (a.ARTICLE = d.article) 
                           AND (a.Offer = d.ToCat) AND (a.OfferYear = d.ToYear)
LEFT JOIN
    (SELECT 
         c.Packnum, c.CatID, c.Excl
     FROM 
         PIC704Current c 
     INNER JOIN 
         cataloginfo d ON (c.CatID = d.catalog) AND (c.Year = d.MailYear)
     WHERE 
         d.MailYear = '2020' 
         AND d.SeasonID = 'F20' 
         AND d.MediaId IN ('CAT Catalog', 'SCAT Sale Catalog', 'SSTF Sale Statement Stuff', 'STUF Statement Stuffer', 'PKG Package Insert', 'SPKG Sale Pkg Insert') 
         AND d.Brand = 'Seventh Avenue') q ON CONCAT(q.catid,q.packnum) = CONCAT(a.offer, a.article)
WHERE 
    q.CatID IS NULL
    AND (CASE WHEN Retail < '1' THEN 'Yes' ELSE 'No' END) = 'No'
    AND b.MailYear = '2020' 
    AND b.SeasonID = 'F20' 
    AND b.MediaId = 'Cat Catalog' 
    AND b.Brand = 'Seventh Avenue'

--Pages
UNION

SELECT DISTINCT 
    a.PackNum, a.Description, a.CatID,
    c.SeasonID, a.Page as MFPage, w.WPage, p.Ppage,
    CASE 
       WHEN a.page <> w.wpage 
          THEN 'Update Page' 
          ELSE '' 
    END AS action,
    CASE 
       WHEN CONCAT(a.CatID, a.PackNum) = CONCAT(d.offer, d.article) 
          THEN 'Y' 
          ELSE 'N'
    END AS [On CN]
FROM 
    pic704current a 
INNER JOIN 
    Lago_CID_Master b ON (a.CatID = b.Offer)  
                      AND (a.year = b.OfferYear) AND (a.PackNum = b.ARTICLE)
JOIN 
    CatalogInfo c ON (a.catid = c.catalog) AND (a.year = c.mailyear)
FULL OUTER JOIN 
    ChangeNoticeChanges d ON (b.ARTICLE = d.article) 
                          AND (a.CatID = d.offer) AND (a.Year = d.offeryear)
LEFT JOIN
    --Working Page
    (SELECT 
         e.packnum, e.catid,
         CASE 
            WHEN CAST(f.wpfrom AS int) >= 7000 THEN 'EOC'
            WHEN CAST(f.wpfrom AS int) BETWEEN 700 AND 800 
                 AND f.spreadname LIKE '%CHECK%' THEN 'CHK'
            WHEN f.spreadname LIKE '%ORDER FORM%' 
                 OR f.spreadname LIKE '%CTOB%' THEN 'COF'
            WHEN f.spreadname LIKE '%OFC%' THEN 'OFC'
            WHEN f.spreadname LIKE '%EXOBC%' THEN 'WR' +  
                 CASE WHEN SUBSTRING(f.spreadname, CHARINDEX(e.catid, f.spreadname) + 2, 1) = ' ' 
                           OR SUBSTRING(f.spreadname, CHARINDEX(f.offer, f.spreadname) + 2, 1) = '' THEN '1' 
                           ELSE SUBSTRING(f.spreadname, CHARINDEX(f.offer, f.spreadname) + 2, 1) 
                 END
            WHEN f.spreadname LIKE '%IBC%' THEN 'IB' + 
                 CASE WHEN substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = ' ' or substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = '' then '1' 
ELSE substring(spreadname,charindex(f.offer, f.spreadname)+2,1) End
WHEN Cast(f.wpfrom as int) = 2 then 'IF' + case when charindex(f.offer, f.spreadname) = 0  then '1' when substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = '' then '1' 
ELSE substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) End   
WHEN f.spreadname like '%OBC%' then 'OB' +  case when substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = ' ' or substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = '' then '1' 
ELSE substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) End
ELSE right('00' + f.wpfrom,3) END AS WPage

FROM pic704current e INNER JOIN Lago_CID_Master f ON (e.CatID = f.Offer) and (e.year = f.OfferYear) and (e.PackNum = f.ARTICLE)
JOIN CatalogInfo g on (e.catid = g.catalog) and (e.year = g.mailyear)

WHERE
e.year = '2020'
and g.SeasonID = 'F20' 
and g.brand = 'Seventh Avenue'
and g.MediaId in('CAT Catalog','SCAT Sale Catalog','SSTF Sale Statement Stuff','STUF Statement Stuffer','PKG Package Insert','SPKG Sale Pkg Insert')) w
ON CONCAT(a.catid,a.packnum) = CONCAT(w.CatID, w.PackNum)

LEFT JOIN

--Paginated Page
(SELECT I.packnum, I.catid
,CASE WHEN Cast(J.PPFROM as int) >=7000 then 'EOC'
WHEN Cast(J.PPFROM as int) between 700 and 800 and J.spreadname like '%CHECK%' then 'CHK'
WHEN J.spreadname like '%ORDER FORM%'  or J.spreadname like '%CTOB%' then 'COF'
WHEN J.spreadname like '%OFC%' then 'OFC'
WHEN J.spreadname like '%EXOBC%' then 'WR' +  case when substring(J.spreadname,charindex(I.catid, J.spreadname)+2,1) = ' ' or substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = '' then '1' 
ELSE substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) End
WHEN J.spreadname like '%IBC%' then 'IB' + case when substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = ' ' or substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = '' then '1' 
ELSE substring(spreadname,charindex(J.offer, J.spreadname)+2,1) End
WHEN Cast(J.PPFROM as int) = 2 then 'IF' + case when charindex(J.offer, J.spreadname) = 0  then '1' when substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = '' then '1' 
ELSE substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) End   
WHEN J.spreadname like '%OBC%' then 'OB' +  case when substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = ' ' or substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = '' then '1' 
ELSE substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) End
ELSE right('00' + J.PPFROM,3) END AS PPage

FROM pic704current I INNER JOIN Lago_CID_Master J ON (I.CatID = J.Offer) and (I.year = J.OfferYear) and (I.PackNum = J.ARTICLE)
JOIN CatalogInfo K on (I.catid = K.catalog) and (I.year = K.mailyear)

WHERE
I.year = '2020' 
and K.SeasonID = 'F20' 
and K.brand = 'Seventh Avenue'
and K.MediaId in('CAT Catalog','SCAT Sale Catalog','SSTF Sale Statement Stuff','STUF Statement Stuffer','PKG Package Insert','SPKG Sale Pkg Insert'))P
ON CONCAT(a.catid,a.packnum) = CONCAT(p.CatID, p.PackNum)

WHERE
(CASE WHEN wpage <> a.page and ppage = '000' then 'true' else 'false' end) = 'true'
and
(CASE WHEN ppage > '0' and ppage <> a.page then 'true' else 'false' end) = 'true'
and a.year = '2020' 
and c.SeasonID = 'F20' 
and c.brand = 'Seventh Avenue'
and c.MediaId in('CAT Catalog','SCAT Sale Catalog','SSTF Sale Statement Stuff','STUF Statement Stuffer','PKG Package Insert','SPKG Sale Pkg Insert')

ORDER BY Action

1 个答案:

答案 0 :(得分:1)

由于您的问题开始于添加联合运算符,因此看来您列中的数据类型不匹配。字符串“ OFC”不能转换为整数,因此将始终失败。在查看列的说明时,我很可能会猜测PackNum是整数,Article是字符串。

要查找有问题的列,可以做的是在FROM部分之前使用“ INTO #temp”语句独立运行查询。然后检查您创建的两个临时表的数据类型。我敢打赌,您会在一个中找到一个int,在另一个中找到一个字符串。

然后根据您的要求扩展查询或将整数列转换为varchar。