从存储过程中删除一个表及其别名

时间:2016-07-13 05:21:18

标签: sql sql-server stored-procedures sql-server-2005

我有一个存储过程,我不想使用inward_doc_tracking_trl及其相关别名的表。

这是代码:

ALTER PROCEDURE [dbo].[GET_INWARD_REMINDER_REPORT]     
AS     
BEGIN     
    Select distinct     
        U.first_name + ' ' + U.last_name UserName,     
        TH.User_ID, TH.doc_no, TH.U_datetime,     
        CASE     
            WHEN TL.U_datetime < DATEADD(d, -5, GETDATE())     
                THEN M.Reporting_To     
           ELSE NULL     
        END Reporting_1 ,     
        --U.Email AS UserEmail,     
        --senior.Email AS RA1_Email,     
        10000 AS UserEmail,    
        100001 AS RA1_Email,                                
         DATEDIFF(DAY,TH.LastAction_DateTime,GETDATE()) - DATEDIFF(WK,TH.LastAction_DateTime,GETDATE())AS No_Of_Days_Opening     
    from 
        inward_doc_tracking_trl TL     
    inner join 
        inward_doc_tracking_hdr TH ON TH.mkey = TL.ref_mkey     
    inner join
        user_mst U ON TH.User_Id = U.mkey     
    inner join
        emp_mst M ON M.mkey = U.employee_mkey     
    outer apply
        (select 
             uss.Email     
         from 
             emp_mst MS     
         inner join
             user_mst uss ON uss.employee_mkey = MS.mkey     
         where
             MS.mkey = M.Reporting_To) senior     
    where 
        TH.Status_flag NOT IN (5,14)     
        and TH.To_user IS NOT NULL  
END

更新

Inward_Doc_Tracking_Hdr表的脚本

            CREATE TABLE [dbo].[Inward_Doc_Tracking_Hdr](
            [Mkey] [numeric](18, 0) NOT NULL,
            [FModule_ID] [char](1) NOT NULL,
            [Comp_Mkey] [numeric](4, 0) NOT NULL,
            [Branch_Mkey] [numeric](4, 0) NOT NULL,
            [Tran_Type] [varchar](6) NOT NULL,
            [Tran_Code] [numeric](8, 0) NOT NULL,
            [Tran_No] [numeric](4, 0) NOT NULL,
            [Department_ID] [varchar](4) NOT NULL,
            [User_Id] [numeric](10, 0) NULL,
            [Doc_Type] [numeric](10, 0) NULL,
            [Doc_No] [varchar](30) NULL,
            [Doc_Date] [datetime] NOT NULL,
            [Dispatch_By] [char](1) NOT NULL,
            [Status_Flag] [numeric](8, 0) NOT NULL,
            [FA_Year] [smallint] NOT NULL,
            [Party_Mkey] [numeric](10, 0) NULL,
            [To_Department] [numeric](10, 0) NULL,
            [To_User] [numeric](10, 0) NULL,
            [Inward_Amt] [numeric](18, 3) NULL,
            [Ref_No] [varchar](40) NULL,
            [Ref_date] [datetime] NULL,
            [U_DateTime] [datetime] NOT NULL,
            [LastAction_DateTime] [datetime] NULL,
            [Remarks] [varchar](255) NULL,
            [Delete_Flag] [char](1) NOT NULL,
            [Outward_Type] [char](1) NULL,
            [Doc_Department] [numeric](10, 0) NULL,
            [Party_Name] [varchar](80) NULL,
            [Delivered_By] [varchar](30) NULL,
            [Doc_Description] [varchar](50) NULL,
            [Last_Department] [numeric](10, 0) NULL,
            [Last_User] [numeric](10, 0) NULL,
            [Approved_Amount] [float] NULL,
            [Chq_No] [varchar](50) NULL,
            [Chq_dated] [datetime] NULL,
            [Chq_Bank] [varchar](40) NULL,
            [Chq_Amount] [float] NULL,
            [Vendor_MKey] [int] NULL,
            [Vendor_Comp_Mkey] [int] NULL,
            [Project_Mkey] [numeric](10, 0) NULL,
            [Program_mkey] [numeric](10, 0) NULL,
            [Payment_MKey] [int] NULL,
            [Due_Date] [datetime] NULL,
            [Updated_Remarks] [varchar](500) NULL,
            [Updated_Bill_no] [varchar](27) NULL,
            [Updated_Bill_Date] [datetime] NULL,
            [Updated_Bill_Amt] [float] NULL,
            [TotalDeductions] [float] NULL,
            [Broker_Mkey] [numeric](10, 0) NULL,
            [Customer_Mkey] [numeric](10, 0) NULL,
            [Payable_Amt] [float] NULL,
            [Balance_Amt] [float] NULL,
            [Req_Bill_Flag] [char](1) NULL,
            [Po_No] [varchar](50) NULL,
            [Receipt_No] [varchar](50) NULL,
            [Bill_No] [varchar](50) NULL,
            [Org_id] [varchar](10) NULL,
            [Site_Id] [varchar](50) NULL,
            [Site_Name] [varchar](150) NULL,
            [Cumulative_Amt] [numeric](18, 2) NULL,
            [Email_Id] [varchar](50) NULL,
            [Emp_Id] [varchar](6) NULL,
            [Disp_through] [varchar](50) NULL,
            [Disp_Through_Name] [varchar](100) NULL,
            [Last_To_User] [numeric](10, 0) NULL
        ) ON [PRIMARY]

2 个答案:

答案 0 :(得分:2)

删除对inward_doc_tracking_trl的所有引用,请参阅注释 - ##

ALTER PROCEDURE [dbo].[GET_INWARD_REMINDER_REPORT]     
AS     
BEGIN     
    Select distinct     
        U.first_name + ' ' + U.last_name UserName,     
        TH.User_ID, TH.doc_no, TH.U_datetime,     
        CASE     
            --## changed TL.U_datetime to TH.U_datetime
            WHEN TH.U_datetime < DATEADD(d, -5, GETDATE())     
                THEN M.Reporting_To     
           ELSE NULL     
        END Reporting_1 ,     
        --U.Email AS UserEmail,     
        --senior.Email AS RA1_Email,     
        10000 AS UserEmail,    
        100001 AS RA1_Email,                                
         DATEDIFF(DAY,TH.LastAction_DateTime,GETDATE()) - DATEDIFF(WK,TH.LastAction_DateTime,GETDATE())AS No_Of_Days_Opening     
    --##from 
    --##    inward_doc_tracking_trl TL     
    --##inner join 
    --##    inward_doc_tracking_hdr TH ON TH.mkey = TL.ref_mkey     
    from  inward_doc_tracking_hdr TH
    inner join
        user_mst U ON TH.User_Id = U.mkey     
    inner join
        emp_mst M ON M.mkey = U.employee_mkey     
    outer apply
        (select 
             uss.Email     
         from 
             emp_mst MS     
         inner join
             user_mst uss ON uss.employee_mkey = MS.mkey     
         where
             MS.mkey = M.Reporting_To) senior     
    where 
        TH.Status_flag NOT IN (5,14)     
        and TH.To_user IS NOT NULL  
END

答案 1 :(得分:0)

我认为删除此代码就足够了:

  

inward_doc_tracking_trl TL
      内连接

并在TL中用TH替换

  

CASE
              当TH.U_datetime&lt; DATEADD(d,-5,GETDATE())
                  然后M.Reporting_To              ELSE NULL

这样的东西应该是存储过程:

ALTER PROCEDURE [dbo].[GET_INWARD_REMINDER_REPORT]     
AS     
BEGIN     
    Select distinct     
        U.first_name + ' ' + U.last_name UserName,     
        TH.User_ID, TH.doc_no, TH.U_datetime,     
        CASE     
            WHEN TH.U_datetime < DATEADD(d, -5, GETDATE())     
                THEN M.Reporting_To     
           ELSE NULL     
        END Reporting_1 ,     
        --U.Email AS UserEmail,     
        --senior.Email AS RA1_Email,     
        10000 AS UserEmail,    
        100001 AS RA1_Email,                                
         DATEDIFF(DAY,TH.LastAction_DateTime,GETDATE()) - DATEDIFF(WK,TH.LastAction_DateTime,GETDATE())AS No_Of_Days_Opening     
    from 
        inward_doc_tracking_hdr TH    
    inner join
        user_mst U ON TH.User_Id = U.mkey     
    inner join
        emp_mst M ON M.mkey = U.employee_mkey     
    outer apply
        (select 
             uss.Email     
         from 
             emp_mst MS     
         inner join
             user_mst uss ON uss.employee_mkey = MS.mkey     
         where
             MS.mkey = M.Reporting_To) senior     
    where 
        TH.Status_flag NOT IN (5,14)     
        and TH.To_user IS NOT NULL  
END