September 23, 2015 at 1:02 pm
I am trying to setup a constraint on a 2012 database the allows only one "LineitemNumber" for each InvoiceID. I am not certain how to do this. Both of these columns exist in the same table.
I know the starting code would look like: ALTER TABLE dbo.InvoiceLineItemBase WITH NOCHECK
ADD CONSTRAINT CK_LineItemNumber
I appreciate any and all help.
Thanks
September 23, 2015 at 1:10 pm
I would use a unique index on InvoiceID filtered where LineitemNumber is not null;
This way you can have as many Null LineitemNumber rows as you like for each InvoiceID, but only one NON-NULL value
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
September 23, 2015 at 1:14 pm
You can also define the columns as the Primary Key.
ALTER TABLE OrderDetails
ADD CONSTRAINT PK_OrderDetails PRIMARY KEY (InvoiceID, LineitemNumber);
September 23, 2015 at 1:33 pm
Luis Cazares (9/23/2015)
You can also define the columns as the Primary Key.
ALTER TABLE OrderDetails
ADD CONSTRAINT PK_OrderDetails PRIMARY KEY (InvoiceID, LineitemNumber);
Yes, I read the question as multiple rows per InvoiceID, with only one allowed to have a LineItemNumber value, but if the intention is to have multiple rows per InvoiceID with multiple different LineItemNumbers, then this is the way to go.
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
September 23, 2015 at 3:11 pm
I am sorry I re-Read my original post and i was unclear.
I want individual LineitemNumbers per each InvoiceId.
so I cna have Invoice#97865 with LineItemNumbers 1,2,3.
right now I am getting Invoice#97865 with LineItemNumbers of 1,2,2,3,4,4,5 etc.
September 23, 2015 at 3:26 pm
jameslauf (9/23/2015)
I am sorry I re-Read my original post and i was unclear.I want individual LineitemNumbers per each InvoiceId.
so I cna have Invoice#97865 with LineItemNumbers 1,2,3.
right now I am getting Invoice#97865 with LineItemNumbers of 1,2,2,3,4,4,5 etc.
Well, you can enforce the uniqueness with a primary key like Luis suggested, but if you are already seeing duplicates, then adding the PK will cause errors to bubble up in the application that is inserting the rows, so be prepared...
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
September 23, 2015 at 3:50 pm
If you've already got a primary key on this table, you can also use a unique index.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 23, 2015 at 5:57 pm
jameslauf (9/23/2015)
I am sorry I re-Read my original post and i was unclear.I want individual LineitemNumbers per each InvoiceId.
so I cna have Invoice#97865 with LineItemNumbers 1,2,3.
right now I am getting Invoice#97865 with LineItemNumbers of 1,2,2,3,4,4,5 etc.
That's actually a display function, IMHO. What would such a sequence provide other than just for display purposes?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply