SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 AutoIncrement Unique column (not Primary Identity)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arraysys
Starting Member

12 Posts

Posted - 10/30/2010 :  07:31:18  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 10/30/2010 :  08:42:00  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
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 - 10/30/2010 :  10:25:07  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 10/30/2010 :  10:34:59  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000