简体   繁体   English

在SQL Server存储过程中创建发票编号

[英]Create invoice number in sql server stored procedure

I have a data like this : 我有这样的数据:

idOrder |  transactionDate    
   31   | 04/06/2014 7:58:38  
   32   | 05/06/2014 8:00:08  
   33   | 05/06/2014 14:23:51 

I want build stored procedure that produce invoice number automatically, eg : 我想建立自动生成发票编号的存储过程,例如:

In 4 June, there are 1 invoice number : 201406041 (20140604 from date and 1 at last from total order happened that day). 6月4日,发票编号为1:201406041(日期为20140604,当天总订单额为最后1)。

So, in 5 June, there are 2 invoice number : 201406051 and 201406052 因此,在6月5日,发票号为2:201406051和201406052

I am lack on SQL scripting except this query to count transaction per day: 我缺少SQL脚本编制,除了此查询每天要统计交易量:

SELECT 
   count(idOrder) as total 
FROM 
   TR_Order 
GROUP BY 
   Convert(char(8), transactionDate, 111);

Thanks in advance. 提前致谢。

select convert(VARCHAR(8), transactionDate, 112) 
    + CONVERT(VARCHAR(100), RANK() 
        over (partition by convert(date, transactionDate) order by idorder))
    as 'invoice no'
    , * 
from TR_Order 

Try this 尝试这个

select * from test  

select 
    idorder, transactiondate,  
    REPLACE(CAST(transactionDate as DATE),'-','') + 
            CAST(ROW_NUMBER() OVER(PARTITION BY CAST(transactionDate AS DATE) 
                                   ORDER BY idorder) AS varchar(8)) AS InvoiceNumber  
from test 

在此处输入图片说明

Hoping this may help you to generate invoice number by using system-date. 希望这可以帮助您使用system-date生成发票编号。

CREATE PROCEDURE INVOICE(IN INPUT VARCHAR(255) CHARSET utf8)

RETURNS VARCHAR(255) CHARSET utf8

DETERMINISTIC

BEGIN

DECLARE TG VARCHAR(255);

DECLARE TH VARCHAR(255);

DECLARE TI VARCHAR(255);

DECLARE TJ VARCHAR(255);

SET TG = ((SELECT count(idOrder) as total FROM TR_Order GROUP BY Convert(char(8), transactionDate, 111) + 1);

SET TH = SUBSTRING(curdate(),1,4);

SET TI = SUBSTRING(curdate(),6,2);

SET TJ = SUBSTRING(curdate(),9,2);

SET INPUT = CONCAT(TH,TI,TJ,TG);

RETURN INPUT;

END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM