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 2000 Forums
 Transact-SQL (2000)
 Identity

Author  Topic 

dewacorp.alliances

452 Posts

Posted - 2002-12-09 : 02:42:05
HI there

I have this following table called tblAccount

1. AccountCode (varchar 4 .. not INT)
2. RegionID (varchar 2)
3. LocationID (varchar2)

I want to make the AccountCode becoming primary key which the value comes from combination of RegionID and LocationID. So when the regionid and locationid get entered (10 and AB for instance) .. the value for AccountCOde will be 10AB

Therefore:
AccountCode; RegionID; LocationID
10AB; 10; AB
20AB; 20; AB

Is there anyaway that you can do this? Can I use trigger? Any example that I can follow?

Thanks
Val


rihardh
Constraint Violating Yak Guru

307 Posts

Posted - 2002-12-09 : 04:19:56
A trigger will fail, because it will fire AFTER RegionID and LocationID are inserted (AccountCode = null).
Because the column AccountCode is a primary key, a value is required at the time of insertion.

I presume that the values for RegionID and LocationID are inserted from some sort of GUI. If so, combine the values there and insert the combined value, but remember, it has to be UNIQUE!

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-12-09 : 09:15:50
Your primary key of your table should be RegionID and LocationID. AccountCode is just a formula you can derive from RegionID and LocationID whenever you need to, or do it in a view so you don't have to worry about it.

People seem to think primary keys can or should be only 1 field, but they should be whatever the true indicator of uniqueness is for that particular table.



- Jeff
Go to Top of Page
   

- Advertisement -