Click here to Skip to main content
15,896,290 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
In a database table we have a varchar() type field which has to maintain identical data to int type primary key for a specific purpose. And varchar fields data value to be calculated before saving new records.

We used Select Max(primary key column) to retrieve maximum value of primary field and add 1 to it for this purpose. But on deleting last record, newly added record's varchar() field stores deleted record's value and primary key field value becomes +1 to it.
Posted
Comments
walterhevedeich 28-Apr-11 0:12am    
And what is the question? :)
sk saini 28-Apr-11 1:33am    
inconsistency between both field values to whom we want to keet identical due to deletion of last record.
sk saini 28-Apr-11 1:36am    
How to remove inconsistency in values of both fields whose values to be kept identical.
Sandeep Mewara 28-Apr-11 0:42am    
Issue?
sk saini 28-Apr-11 1:37am    
How to remove inconsistency in values of both fields whose values to be kept identical.

1 solution

It seems you are keeping track of the number of rows that were inserted in the table. It does not matter whether they were deleted later on. I think you should be going for Audit tables. Whenever Insert/Update/Delete happens, a trigger can change this table. Later you can use this for any reporting job that you need. Although as people say that triggers are evil, there are to ways of doing this:

Change the datatype for the varchar column. It should be integer. And make a autoicnrement column. This should serve the purpose. Alhtough you will have to check the effect of row deletion on the incremental value. I believe it should be same as sequence in Oracle so it should change.
 
Share this answer
 
Comments
sk saini 28-Apr-11 1:38am    
But its requires to maintain varchar datatype here.
dan!sh 28-Apr-11 8:21am    
It is plain wrong. You are using varhcar for saving integers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900