【问题标题】:Invoice creation in sql - create a stored procedure在sql中创建发票-创建存储过程
【发布时间】:2019-07-18 07:06:25
【问题描述】:

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

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

表架构 ->
1.InvoiceItemsTABLE1
2.CustomerDetailsTable2
3.InvoiceDetailsenter 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

【问题讨论】:

  • 您的输入和预期输出是什么。请以文本格式发布。
  • CustomerName、InvoiceNumber、InvoiceDate、Email、Mobile、Bill DATE、Bill Month、Due Date -- 这些字段应在发票中填写所有值。 @DarkRob
  • 再次检查您给定的代码,您确定这就是全部。而且我们不知道您的InvoiceDataCustomerDetails 的表架构,您在这些表中还有什么?请查看其他示例来发布您的问题。
  • @DarkRob 我已附上表格结构和发票中需要的内容。另外,我也重写了我提到的存储过程。
  • 请帮我解决这个问题。

标签: sql sql-server stored-procedures invoice


【解决方案1】:

所以我期待您从您的程序中获得#tempInvoice 中的所有记录。并面临仅使用您提供的格式生成发票编号的问题。

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

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

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

最后,您的程序需要更新它以返回您所需的结果。

    ; 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

【讨论】:

  • 我已经尝试过了,但执行时出现错误。无论如何。谢谢你的帮助! @DarkRob
  • 显然您的记录中没有#regioncode 表,在继续之前创建一个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 2017-12-21
  • 2013-07-18
  • 1970-01-01
相关资源
最近更新 更多