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
 General SQL Server Forums
 New to SQL Server Programming
 Need some Help

Author  Topic 

krittim
Starting Member

6 Posts

Posted - 2006-07-25 : 06:46:12
I have a column name GoodsID (Datatype is varchar 50). This is the primary (primary key creates index). It is the combination of 3 characters and 6 numeric values, i.e. MAC100000. Every time the user entering new record in this table I am calculating the max number extracting the primary key-

Syntax is:



SELECT MAX(CONVERT(numeric, RIGHT(RTRIM(GoodsID), LEN(GoodsID)-3))) AS MaxID FROM GoodsMaster


Now my question is if I have a table with 1,00,000 transaction per month, is it the best solution to create the primary key?

I'm using indexing as well .

nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-25 : 07:17:56
Why not have two columns
GoodsID
Seq

Then maybe Seq could be an identity or calculated as you are now.

==========================================
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

krittim
Starting Member

6 Posts

Posted - 2006-07-25 : 07:41:31
thanks NR. Can u please explain a little bit more? I didn't get u properly.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-25 : 08:06:02
Your GoodsID is actually the combination of two fields. It is probably better to model them as such so the table structure would be

GoodsID char(3) ,
seq decimal(6,0) -- or int

Then to get the max seq
select max(seq) from AS MaxID FROM GoodsMaster

If seq is indexed then it won't have to scan the values like it will as you have it.
As you have it the index on GoodsID won't be very useful as it would have to scan all the values to get the max of the numeric part.
If you are just doing this to calculate the next value then it could be an identity if you don't mind gaps or calculated and set in a trigger.



==========================================
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

krittim
Starting Member

6 Posts

Posted - 2006-07-25 : 08:26:42
Thanks a lot NR.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-26 : 10:49:53
Also refer
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=57069

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -