从过程到查看

时间:2011-12-21 18:26:54

标签: sql sql-server-2008 tsql

以下代码与程序完美配合。 如何转换或更改下面的相同代码,以便它可以在View或View代码或View中工作?

我可以放弃查看吗?

ALTER proc [dbo].[NewOne_1]
as
begin
set nocount on

if  object_ID('TEMPDB..#TableA') <> ''
drop table # TableA
select 
    *
into # TableA
from vd_View  po   

if object_ID('TEMPDB..#FirstSDate') is not null  
   drop table #FirstSDate
select 
    Pt_id = Pat_Id,
    Date_of_First_Shipment = min(DeliveryDate)
into #FSDate    
from # TableA  po
group by Pat_Id        

if object_ID('TEMPDB..#LastSDate') is not null   
   drop table #LastShipDate
select 
    Pt_id = Pat_Id,
    Date_of_Last_Shipment = max(DeliveryDate)
into #LastShipDate  
from # TableA  po
group by Pat_Id                                  

SELECT PtData.Pat_No                Progress_Pat_ID
    ,C_S = case when  dbo.fn_GetBusinessDays(firstship.Date_of_First_Shipment,LastShip.Date_of_Last_Shipment) > 80 then 'Yes'  
                                         when  dbo.fn_GetBusinessDays(firstship.Date_of_First_Shipment,LastShip.Date_of_Last_Shipment) <= 80 then 'No' 
                                        else ''
                                        end
    ,P_Last_Name = PtData.P_LName
    ,PtData.DReg        Reg
    FROM dbo.tbld_PatSum PtData
    inner join vd_PSum ps
        on PtData.P = ps.P_ID
    inner join S_M.dbo.Pat__c ps1 
        on PtData.Pt_ID = ps1.Id              
        left join #FirstShipDate firstship    
        on PtData.Pt_ID = firstship.Pt_Id     
        left join #LastShipDate LastShip      
        on PtData.Pt_ID = LastShip.Pt_Id      
        WHERE PtData.Pat_No IS NOT NULL 
        AND PtData.ActiveStatus<>'Gen Info'

    set nocount off
end

2 个答案:

答案 0 :(得分:1)

我猜你想要用它来加入别的东西。你不能把它放到一个视图中。但是你可以把它放在一个表值函数中,它可以让你完成我认为你想要的东西。

答案 1 :(得分:0)

- 刚刚接受了你的代码(它非常混乱),dbo.fn_GetBusinessDays函数必须是确定性的才能在视图中工作

     create view test as        

SELECT 
   PtData.Pat_No                Progress_Pat_ID
    ,C_S = case when  dbo.fn_GetBusinessDays(firstship.Date_of_First_Shipment,LastShip.Date_of_Last_Shipment) > 80 then 'Yes'  
                                         when  dbo.fn_GetBusinessDays(firstship.Date_of_First_Shipment,LastShip.Date_of_Last_Shipment) <= 80 then 'No' 
                                        else ''
                                        end
    ,P_Last_Name = PtData.P_LName
    ,PtData.DReg        Reg
    FROM dbo.tbld_PatSum PtData
    inner join vd_PSum ps
        on PtData.P = ps.P_ID
    inner join S_M.dbo.Pat__c ps1 
        on PtData.Pt_ID = ps1.Id              
        left join (
            select 
            Pt_id = Pat_Id,
            Date_of_First_Shipment = min(DeliveryDate)
            from vd_View  po
            group by Pat_Id  ) firstship    
        on PtData.Pt_ID = firstship.Pt_Id     
        left join (select 
    Pt_id = Pat_Id,
    Date_of_Last_Shipment = max(DeliveryDate) 
from vd_View  po
group by Pat_Id  ) LastShip      
        on PtData.Pt_ID = LastShip.Pt_Id      
        WHERE PtData.Pat_No IS NOT NULL 
        AND PtData.ActiveStatus<>'Gen Info'