2014-03-12 14 views
2

下面我想加入股票行情與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
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 
+1

實際上連接條件是SST ON sa.SlipType = sst.SlipType。問題是,外部應用程序工作非常緩慢,並沒有顯示連續劇 –