防止发票号码重复针对特定 ID

发布于 2024-08-08 06:15:28 字数 425 浏览 5 评论 0原文

我对如何防止针对 CompanyId 重复 InvoiceNo 感到困惑。我刚刚准备了一个发票项目,其中包含 CompanyIdInvNo 等字段。两者都没有唯一键,因为 Company ID 和 InvoiceNo 都必须重复。如下所示

CompanyID   InvNo
1           1
2           1
1           2
3           1
4           1
1           3

现在我想针对特定 CompanyId 对重复的 InvoiceNo 触发 raiserror。我该如何实现这个。重要提示:如果我创建唯一索引,则不允许重复记录,并且重要的是允许除特定 CompanyId 之外的记录

I have confusion on how to prevent duplicate InvoiceNo against CompanyId. I just have prepare an Invoice project that has field like CompanyId and InvNo. Both do not have Unique Keys because Company ID and InvoiceNo both have to repeated. as per below

CompanyID   InvNo
1           1
2           1
1           2
3           1
4           1
1           3

Now I want to fire a raiserror on duplicate InvoiceNo against a particular CompanyId. How do I implement this. Important: if i create a unique index then duplicate records will not be allowed and it is important to allow except against particular CompanyId

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱娆 2024-08-15 06:15:28

您正在寻找的是由 CompanyId 和 InvNo 组成的唯一约束。这将让您只为 CompanyId = 1 创建一张 InvoiceNo = 1,并且如果您尝试插入重复项,将自动引发 RaisError。它还可以让您为 CompanyId = 2 插入 InvoiceNo = 1 从而(希望)满足您的要求

这就是我在 SQL Server 中执行此操作的方式

ALTER TABLE YourTableName
ADD UNIQUE CONSTRAINT InvoiceIdMustBeUniqePerCompany (CompanyId, InvNo)

What you are looking for is a Unique Constraint composed of both CompanyId and InvNo. This will let you create only one InvoiceNo = 1 for CompanyId = 1 and will automatically RaisError if you try to insert a duplicate. It will also let you insert InvoiceNo = 1 for CompanyId = 2 thereby (hopefully) satisfying your requiements

This is how I would do it in SQL Server

ALTER TABLE YourTableName
ADD UNIQUE CONSTRAINT InvoiceIdMustBeUniqePerCompany (CompanyId, InvNo)
暖树树初阳… 2024-08-15 06:15:28

你的问题并不是那么直接,但假设你问的是我认为你在问的问题,它是这样的......

你需要一条名为 NextInvoiceNumber 的电缆,由 CompanyID 和 NextInvoiceNo 组成。创建一个新公司应该创建一个新的 NextInvoiceNumber - 因此也许可以在您的 Companies 表上使用插入触发器...

编写一个函数来获取特定公司的下一个发票 ID,然后递增 NextInvoiceNumber 表中的值(所有在公共事务中)。

因此,在伪代码中,类似

    function GetNextInvoiceNo(CompanyIDCode){

begin transaction;
result = Select NextInvoiceNo from NextInvoiceNumber where CompanyID = CompanyIDCode;
update NextInvoiceNo set NextInvoiceNo = NextInvoiceNo + 1 where CompanyID = CompanyIDCode;
commit transaction;
return result;
}

This function 的内容理想地属于数据库服务器上的 UDF。

Your question is not all that straight forward, but assuming you're asking what I think you're asking, it goes something like this...

You need a cable called NextInvoiceNumber, comprising of CompanyID and NextInvoiceNo. Creating a new Company should create a new NextInvoiceNumber - so perhaps use an insert trigger on your Companies table for that...

Write a function to get the next Invoice id for a specific company, and then incremenent the value in the NextInvoiceNumber table (all inside a common transaction).

So, in pseudo code, something like

    function GetNextInvoiceNo(CompanyIDCode){

begin transaction;
result = Select NextInvoiceNo from NextInvoiceNumber where CompanyID = CompanyIDCode;
update NextInvoiceNo set NextInvoiceNo = NextInvoiceNo + 1 where CompanyID = CompanyIDCode;
commit transaction;
return result;
}

This function ideally belongs on your database server as a UDF.

说好的呢 2024-08-15 06:15:28

从示例数据来看,CompanyID + InvNo 的组合是唯一的。如果确实如此,您可以在这两个字段上创建一个键,并且在尝试插入已用于特定 CompanyID 的重复 InvNo 时,将引发错误。

create table Invoice
(
CompanyID int,
InvNo int,
Primary Key(CompanyID, InvNo)
)

From the sample data it appears that the combination of CompanyID + InvNo is unique. If that's true you can create a key on those two fields and upon an attempt to insert a duplicate InvNo that has already been used for a particular CompanyID an error would be thrown.

create table Invoice
(
CompanyID int,
InvNo int,
Primary Key(CompanyID, InvNo)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文