如何加入表值函数?

时间:2014-03-12 08:18:39

标签: sql sql-server

下面我想加入与udf_SlipSerials的股票交易,它返回股票连续出版物和股票ID;我怎样才能做到这一点?

ALTER VIEW [dbo].[vStockSerials]
AS
SELECT distinct sa.Oid as Id,it.Oid as StockOid, wh.Oid as WarehouseOid, udf.serial,
it.Code as StockCode,sa.ActionPrice,it.Code+' '+it.Title as StockName, it.Title as StockTitle,wh.Title as Warehouse,
sa.SlipDate,sa.IsSlipDeleted,sst.ActionType, 
sa.SlipType 

FROM dbo.StockAction as sa 
  INNER JOIN dbo.Item as it ON sa.Stock=it.Oid
  INNER JOIN dbo.EnterpriseCore as wh ON sa.Warehouse=wh.Oid
  INNER JOIN dbo.Stock as s ON sa.Stock=s.Oid  
  LEFT OUTER JOIN 
  (
    SELECT  CONVERT(uniqueidentifier, st.Oid) as SlipType,
    (case st.StockEffect
        when '1' then        
        'Income' 
        when '2' then
        'Expense'   
     end) as ActionType

    FROM dbo.StockSlipType as st 

    UNION  

  OUTER APPLY  udf_SlipSerials(sa.SerialNos,sa.Stock) as udf  

GO 

1 个答案:

答案 0 :(得分:1)

SELECT distinct 
         sa.Oid as Id
        ,it.Oid as StockOid
        , wh.Oid as WarehouseOid
        , udf.serial
        ,it.Code as StockCode
        ,sa.ActionPrice
        ,it.Code+' '+it.Title as StockName
        , it.Title as StockTitle
        ,wh.Title as Warehouse
        ,sa.SlipDate
        ,sa.IsSlipDeleted
        ,sst.ActionType
        ,sa.SlipType 

FROM dbo.StockAction as sa INNER JOIN dbo.Item as it 
ON sa.Stock=it.Oid
INNER JOIN dbo.EnterpriseCore as wh 
ON sa.Warehouse=wh.Oid
INNER JOIN dbo.Stock as s 
ON sa.Stock=s.Oid  
LEFT OUTER JOIN 
  (
    SELECT  CONVERT(uniqueidentifier, st.Oid) as SlipType,
            case st.StockEffect
                when '1' then 'Income' 
                when '2' then 'Expense'   
             end as ActionType
    FROM dbo.StockSlipType as st 
  )Sub
ON s.Oid   = Sub.SlipType    --<-- Your are missing this join condition here I have only gussed
   OUTER APPLY 
                dbo.udf_SlipSerials(sa.SerialNos,sa.Stock) as udf