Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 AutoIncrement Unique column (not Primary Identity)

Author  Topic 

arraysys
Starting Member

12 Posts

Posted - 2010-10-30 : 07:31:18
Hi can anyone assist - working in VS2008, SQL 2008 & vb.net

I need to increment an itemCode for duplicate product names that is unique but is not the Primary key for an inventory table.

EXAMPLE
ProdID = int, identity column
Name = ProductA, string
ItemCode = unique, string

ItemCode NEEDED = ProdA-01; ProdA-02; ProductB-01; Productb-02...




Array

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-10-30 : 08:42:00
Could use a trigger.

Why are you repeating the pruduct name?
try
product
prodid int indentity
prodname string

item
prodid int
itemid int

then the problem resolves to incrementing the itemid - the itemcode is obtained from combining product name and item id (you can add that to the table if you wish). Constraint is on prodid, itemid.

To increment the itemid could get the max from the table for the prodid or maintain another table with the last itemid for the product - depends on the locking and performance issues that you have.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

arraysys
Starting Member

12 Posts

Posted - 2010-10-30 : 10:25:07
thanks for reply:
I need duplicate names as in example: 3 hammers: ham1, ham2, ham3
codes ham-01, ham-02, ham-03

The solution is as you mention - To increment the itemid could get the max from the table for the prodid or maintain another table with the last itemid for the product - so what I need is to get the max and update the itemId ...

...but getting the max is a group-by select query and insert or update has to be done in separate query...is there a solution for passing the max value from select to the update in one query/proc or trigger?


quote:
Originally posted by nigelrivett

Could use a trigger.

Why are you repeating the pruduct name?
try
product
prodid int indentity
prodname string

item
prodid int
itemid int

then the problem resolves to incrementing the itemid - the itemcode is obtained from combining product name and item id (you can add that to the table if you wish). Constraint is on prodid, itemid.

To increment the itemid could get the max from the table for the prodid or maintain another table with the last itemid for the product - depends on the locking and performance issues that you have.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.



Array
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-10-30 : 10:34:59
insert tbl (prodid, itemid)
select @prodid, (select max(itemid)+1 from tbl where prodid = @prodid)

if you have the table as you suggest then it becomes something like
insert tbl (prodid, name, itemcode)
select @prodid, a.name
(select a.name + '_' + right('00' + convert(varchar(2),convert(int,max(right(itemcode,2))+1),2) from tbl where prodid = @prodid)
from (select top 1 name from tbl where prodid = @prodid) a

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -