在sql查询中使用Top子句时,将数据类型varchar转换为float时出错

时间:2014-07-23 07:56:05

标签: tsql sql-server-2000

为什么我在查询中使用top子句时会转换错误,但是当我在查询中使用select *时,一切正常。

我使用SQL server 2000

我使用从下面的代码

创建的视图
select sum(g.qty) as qty,g.PartCode,g.PartName,g.arz,g.t,g.OrdNo,max(g.id) as id,g.Val from
        (SELECT sum(iv.FinalQty) AS qty, p.PartCode, p.PartName, CAST(RIGHT(p.PartCode, 4) AS float) AS arz,case when isnumeric(LEFT(RIGHT(p.PartCode, 7), 3))=1 then CAST(LEFT(RIGHT(p.PartCode, 7), 3) AS float) / 100 else CAST(LEFT(RIGHT(p.PartCode, 7), 4) AS float)/100 end AS t, ord.OrdNo, 
                              id.id,ctrl.val
        FROM         INV.InvVchHdr AS hv WITH (nolock) INNER JOIN
                              INV.InvVchItm AS iv WITH (nolock) ON hv.VchHdrID = iv.VchHdrRef INNER JOIN
                              inv.Part p WITH (nolock) ON p.Serial = iv.PartRef INNER JOIN
                              INV.InvRqstItm AS i WITH (nolock) ON i.RqstItmID = iv.RefNum INNER JOIN
                              INV.InvRqstHdr AS h WITH (nolock) ON h.RqstHdrID = i.HdrRef INNER JOIN
                              PRD.PrdOrdPlan AS po WITH (nolock) ON po.OrdPlnId = h.OrdPlnBase INNER JOIN
                              PRD.PrdOrdItem AS oi WITH (nolock) ON oi.OrdItmId = po.OrdItmRef INNER JOIN
                              PRD.PrdOrder AS ord WITH (nolock) ON ord.OrdID = oi.OrderRef JOIN
                              inv.InvVchItmCtrl ctrl WITH (nolock) ON ctrl.VchItmRef = iv.VchItmID
                              join USR.idgen_coil_trace id WITH (nolock) on id.vchitmid=iv.VchItmID
        WHERE     (hv.VchType = 51)  AND (iv.BaseVchType <> 0) and iv.VchItmID>981336159 
        and isnumeric(LEFT(RIGHT(p.PartCode, 7), 3))=1
        AND hv.VchHdrID IN
                                  (SELECT     max(i.vchhdrref)
                                     FROM         inv.InvVchItm i WITH (nolock) JOIN
                                                           inv.InvVchItmCtrl c WITH (nolock) ON c.VchItmRef = i.VchItmID
                                     WHERE     i.VchType = 51
                                     GROUP BY i.PartRef, c.Val)
        GROUP BY p.PartCode, p.PartName, CAST(RIGHT(p.PartCode, 4) AS float)
        , CAST(LEFT(RIGHT(p.PartCode, 7), 3) AS float) / 100
        , ord.OrdNo, ctrl.Val,id.id) g
        --join USR.idgen_coil_trace id on id.vchitmid=g.VchItmID 
        group by g.PartCode, g.PartName, g.arz
        , g.t, g.OrdNo, g.Val


        UNION 
        select sum(g.qty),g.PartCode,g.PartName,g.SizeW,g.SizeH,g.OrdNo,max(g.id),g.Val from(SELECT     sum(iv.FinalQty) AS qty, p.PartCode, p.PartName, cp.SizeW, cp.Sizeh, ord.OrdNo,id.id, ctrl.val
        FROM         INV.InvVchHdr AS hv WITH (nolock) INNER JOIN
                              INV.InvVchItm AS iv WITH (nolock) ON hv.VchHdrID = iv.VchHdrRef INNER JOIN
                              INV.Part AS p WITH (nolock) ON p.Serial = iv.PartRef INNER JOIN
                              INV.InvRqstItm AS i WITH (nolock) ON i.RqstItmID = iv.RefNum INNER JOIN
                              INV.InvRqstHdr AS h WITH (nolock) ON h.RqstHdrID = i.HdrRef INNER JOIN
                              PRD.PrdOrdPlan AS po WITH (nolock) ON po.OrdPlnId = h.OrdPlnBase INNER JOIN
                              PRD.PrdOrdItem AS oi WITH (nolock) ON oi.OrdItmId = po.OrdItmRef INNER JOIN
                              PRD.PrdOrder AS ord WITH (nolock) ON ord.OrdID = oi.OrderRef JOIN
                              inv.InvVchItmCtrl ctrl WITH (nolock) ON ctrl.VchItmRef = iv.VchItmID JOIN
                              inv.PartCmplmnt cp WITH (nolock) ON cp.PartRef = p.Serial
                               join USR.idgen_coil_trace id WITH (nolock) on id.vchitmid=iv.VchItmID
        WHERE     (hv.VchType = 57)  AND (iv.BaseVchType <> 0) and iv.VchItmID>981336159 
         AND PO.PartRef IN (SELECT SERIAL FROM INV.Part P WHERE P.PartName LIKE '%لاف%')
        GROUP BY p.PartCode, p.PartName, cp.SizeW, cp.Sizeh, ord.OrdNo,id.id, ctrl.Val)g
        --join USR.idgen_coil_trace id on id.vchitmid=g.VchItmID 
        group by g.PartCode, g.PartName, g.SizeW
        , g.SizeH, g.OrdNo, g.Val

        UNION  
        --SELECT     *FROM         usr.aghili WITH (nolock)
        --UNION
        --SELECT     *
        --FROM         usr.moradi WITH (nolock)
        SELECT   qty, PartCode, PartName, arz, t, OrdNo, id, val FROM USR.COIL_TRACE_BAK  WITH (nolock)
        union 
        select   qty,PartCode,PartName,arz,t,OrdNo,id,Val from usr.unregister_coil_trace WITH (nolock)

此查询是正确的&gt;从usr.coil_trace

中选择*

此查询不正确&gt;从usr.coil_trace

中选择前100名*

1 个答案:

答案 0 :(得分:0)

Top子句应该在sql server 2000中工作。 虽然我没有跟我一起使用sql 2000,但是this link可以帮到你。

另请查看this stackover flow link

的答案