在sql server存储过程中创建发票号

时间:2014-06-05 04:48:41

标签: sql sql-server stored-procedures

我有这样的数据:

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

我想要自动生成发票编号的构建存储过程,例如:

6月4日,有1个发票号码:201406041(从日期开始20140604起,当天发生的总订单中有1个)。

因此,在6月5日,有2个发票号码:201406051和201406052

我缺少SQL脚本,除了每天计算事务的查询:

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

提前致谢。

3 个答案:

答案 0 :(得分:1)

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 

答案 1 :(得分:1)

试试这个

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 

enter image description here

答案 2 :(得分:0)

希望这可以帮助您使用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;