使用max + 1 entry_sr_no

时间:2016-08-19 06:03:41

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

我有一个SP,它会返回相应文件号的最大条目号。

现在我想要的是,我想在inward_doc_tracking_trl表中插入相同的记录,其中包含max + 1 entry_sr_no

让我们说,

Max_Entry_Srno = {IW / 16/5694的

doc_no为3, 然后在trl表格中插入相同内容时,Max_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 个答案:

答案 0 :(得分:1)

工会全部帮助

---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