sql 中的发票创建 - 创建存储过程

Invoice creation in sql - create a stored procedure

我想创建一个程序,其中应显示以下详细信息:- 我创建了这个查询,但没有得到正确的结果。我附上了 table 架构。

我们正在使用的表格:- InvoiceDataCustomerDetailsInvoiceItems

Table 架构 ->
1. InvoiceItems TABLE1
2. CustomerDetails Table2
3. InvoiceDetails enter image description here

发票有 2 个部分:-

在发票的第一部分,应显示以下详细信息。

Invoice Information section

在发票的第二部分,应显示以下详细信息:-

Invoice Items description section

我附上以下查询:-

alter Procedure SaveInvoiceDetails
(
@CustomerId varchar(50),
@InvoiceNumber varchar(50),
@InvoiceDate date,
@InvoiceMonth int,
@FromDate date,
@ToDate date,
@Rate int,
@Quantity int,
@ActualAmount int,
@ZoneId int
)

as
set nocount on;

begin

Declare @TotalRows int
Declare @NumPages int
set @TotalRows = 0




Select ROW_NUMBER() over (order by C.CustomerId) as InvoiceRow,
C.CustomerId, I.InvoiceNumber, I.InvoiceDate, I.FromDate, I.ToDate, 
I.InvoiceMonth, I.Rate, I.ActualAmount, I.Quantity, C.ZoneId, 
C.BillingAmount
into #tempInvoice
from ConsumerMST_LKO C
inner join InvoiceDetails I
on C.CustomerId = I.CustomerId
inner join INVOICEITEMS II
on I.InvoiceNumber = II.INVOICEID
where InvoiceNumber = @InvoiceNumber AND InvoiceDate = @InvoiceDate AND 
InvoiceMonth = @InvoiceMonth
AND FromDate =@FromDate AND ToDate = @ToDate AND ActualAmount = 
@ActualAmount

set @TotalRows = @@ROWCOUNT
If @TotalRows = 0
Begin

set @TotalRows = @TotalRows + 1
Insert #tempInvoice
(
InvoiceNumber,
InvoiceDate,
InvoiceMonth,
ZoneId,
Rate,
Quantity,
BillingAmount,
FromDate,
ToDate
)
VALUES 
(@TotalRows
,   ''
,''
,''
,0
,0
,0
,0
,''
,0)


End
End
SELECT * FROM #tempInvoice ORDER BY InvoiceRow asc
return

所以我希望您从程序中获得 #tempInvoice 中的所有记录。并且仅使用您提供的格式生成发票编号时会遇到问题。

对于每个区域,您已经在您的终端生成了一个特定的代码(我猜)。

#regioncode  ---- this table contain record of zoneid and zoneocde for each area

如果您没有任何 table 地区,那么在继续之前准备一个,因为这需要您在代码部分的任何地方。

最后,您的程序需要将其更新为 return 您需要的结果。

    ; with cte as (
    select row_number() over (partition by ZoneId order by InvoiceDate) as Slno, * from #tempInvoice as t  inner join #regioncode as r on t.zoneid=r.zoneid
    )
    select zonecode + '_' + cast(slno as varchar(10)) as Uniquecode, * from cte