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

时间:2019-07-18 07:06:25

标签: sql sql-server stored-procedures invoice

我想创建一个过程,其中应显示以下详细信息: 我创建了此查询,但没有得到正确的结果。我已经附上了表模式。

我们正在使用的表:-InvoiceDataCustomerDetailsInvoiceItems

表架构->
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

1 个答案:

答案 0 :(得分:0)

因此,我希望您从过程中获得#tempInvoice中的所有记录。而且,仅以您提供的格式生成发票编号会遇到问题。

对于每个区域,您已经在末端生成了一个特定的代码(我想是这样)。

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

如果您没有任何用于区域的表,请在继续之前准备一个表,因为这需要您在代码部分中的任何地方。

在pf末尾,您的过程需要更新,以返回所需的结果。

    ; 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