2016-08-19 321 views
0

我有一個SP返回給我的最大條目號分別爲文檔號插入max + 1的重複記錄entry_sr_no

現在我想的是,我要插入相同的記錄在inward_doc_tracking_trl表MAX + 1 entry_sr_no

讓說,

doc_no = IW /五千六百九十四分之一十六是3 Max_Entry_Srno, 然後在插入相同的trlMax_Entry_Srno應該是4

這裏是我的SP

ALTER procedure set_auto_action 
     as 
     begin   
      select hdr.mkey, hdr.doc_no, MAX(trl.entry_sr_no) as Max_Entry_Srno 
       from inward_doc_tracking_hdr hdr 
       JOIN inward_doc_tracking_trl trl 
      ON(hdr.mkey = trl.ref_mkey) 
       where hdr.status_flag  not in (5,13) 
      and trl.U_Datetime < DATEADD(DAY, -1, GETDATE())  
      GROUP BY hdr.mkey, hdr.doc_no 
     end 

也見截圖由SP返回的結果

Screenshot

inward_doc_tracking_trl表腳本也低於: -

CREATE TABLE [dbo].[Inward_Doc_Tracking_Trl](
[Mkey] [numeric](18, 0) NOT NULL, 
[Entry_Sr_No] [numeric](4, 0) NOT NULL, 
[N_UserMkey] [numeric](10, 0) NULL, 
[N_Department] [numeric](10, 0) NULL, 
[CStatus_Flag] [numeric](8, 0) NOT NULL, 
[Remarks] [varchar](500) NULL, 
[CUser_ID] [numeric](10, 0) NOT NULL, 
[U_Datetime] [datetime] NOT NULL, 
[NStatus_Flag] [numeric](10, 0) NOT NULL, 
[Delete_Flag] [char](1) NULL, 
[CDept_Id] [numeric](10, 0) NOT NULL, 
[Ref_Mkey] [numeric](18, 0) NULL, 
[No_Of_Days] [int] 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, 
[Party_Name] [varchar](80) NULL, 
[Acc_mkey] [numeric](10, 0) NULL, 
[TotalDeductions] [float] NULL, 
[Broker_Mkey] [numeric](10, 0) NULL, 
[Customer_Mkey] [numeric](10, 0) NULL, 
[Payable_Amt] [float] NULL, 
[Balance_Amt] [float] NULL, 
[Receipt_No] [varchar](50) NULL, 
[Po_No] [varchar](50) NULL, 
[Bill_No] [varchar](50) NULL, 
[Disp_through] [varchar](50) NULL, 
[Disp_Through_Name] [varchar](100) NULL, 
[Site_Id] [int] NULL, 
    CONSTRAINT [PK_Inward_Doc_Tracking_Trl_1] PRIMARY KEY CLUSTERED 
    (
    [Mkey] ASC, 
    [Entry_Sr_No] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =  OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

注意:後面加上重複的原因用max + 1 sr_no排是讓用戶知道該用戶收到的文件

回答

1

Wi ll union all help

---this returns dupe 
    select hdr.mkey, hdr.doc_no, MAX(trl.entry_sr_no) as Max_Entry_Srno 
        from inward_doc_tracking_hdr hdr 
        JOIN inward_doc_tracking_trl trl 
       ON(hdr.mkey = trl.ref_mkey) 
        where hdr.status_flag  not in (5,13) 
       and trl.U_Datetime < DATEADD(DAY, -1, GETDATE())  
       GROUP BY hdr.mkey, hdr.doc_no 

    union all 
---dupe+1 
    select hdr.mkey, hdr.doc_no, MAX(trl.entry_sr_no)+1 as Max_Entry_Srno 
        from inward_doc_tracking_hdr hdr 
        JOIN inward_doc_tracking_trl trl 
       ON(hdr.mkey = trl.ref_mkey) 
        where hdr.status_flag  not in (5,13) 
       and trl.U_Datetime < DATEADD(DAY, -1, GETDATE())  
       GROUP BY hdr.mkey, hdr.doc_no 
+0

它會將記錄插入'inward_doc_tracking_trl'中的max + 1'sr_no' ?? – BNN

+0

是的,它插入max + 1 – TheGameiswar

+0

如果我想檢查trl表,那麼如何檢查? – BNN