I can't use a identity field.
Because I need to know the value before I store it in the DB.
Main Topics
Browse All TopicsHi,
The following I need to accomplish:
In table "Systemsettings" an Integer field called "NextID"
When a user (application) wants to use an ID then it must be increased (for the next user).
Because we're in a multi-user environment this field is updated by several users.
Example 1:
NextID = 100
A user want to use 1 ID
NextID := NextID + 1
Returnvalue = 101
Example 2:
NextID = 101
A user want to use 10 ID's
NextID := NextID + 10
Returnvalue = 111
I need SQL Server to give me the actual value immediately, because otherwise there might be a timing issue. So I was thinking of using a StoredProcedure which returns the value after the update is done.
Can anybody give me some detailed info (sample code) on how to set this up.
1. Create the StoredProcedure in SQL Server 2005
2a. Call the SP from within Delphi
2b. Supply the SP with the increament value (1 or more)
3. Capture the returned value in Delphi
Regards, Stef
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
exactly.
the problem is this:
say user 1 starts the process to add... procedure called, returns 101 (but the actualy "max" remains 100 so far)
before the session of user 1 calls/committed the second phase (the insert of that value), user 2 starts the same process... and will get back 101 also...
do yourself a favor, and do it the right way, please.
Oracle has an SQL command for locking a table :
LOCK TABLE <Table_Name> IN EXCLUSIVE MODE;
The lock is released at the end of the transaction (Meaning, COMMIT or ROLLBACK).
You can create a Query and set the SQL property for the above command.
For SQL Server:
http://www.mssqlcity.com/A
You will have to do some reading..... :-)
Learn about record locking in SQL Server. Covers optimistic, and pessimistic locking.
Pessimistic and Optimistic Concurrency
In a multi-user environment, there are two models for updating data in a database: optimistic concurrency and pessimistic concurrency.
Pessimistic concurrency involves locking the data at the database when you read it. You essentially lock the database record and don't allow anyone to touch it until you are done modifying and saving it back to the database. Here you have 100% assurance that nobody will modify the record and while you check it have it checked out, out. aAnother person will have to wait until you have made the your changes.
By default, SQL Server controls lock escalation, but you can control it yourself by using lock optimizer hints. Here are some lock escalation hints you may want to consider:
· ROWLOCK This hint guides tells SQL Server to use row-level locking instead of page locks for INSERTS. By default, SQL Server may perform as a page-level lock instead of a less intrusive row-level lock when inserting data. By using this hint, you can guide tell SQL Server to always start out using row-level locking. But, this hint does not prevent lock escalation if the number of locks exceeds SQL Server's lock threshold.
· SERIALIZABLE (equivalent to HOLDLOCK) applies only to the table specified and only for the duration of the transaction, and it will hold a shared lock for this duration instead of releasing it as soon as the required table, data page, row or data is no longer required.
· TABLOCK specifies that a table lock to be used instead of a page or row level lock. This lock will be remained held until the end of the statement.
· TABLOCKX specifies that an exclusive lock will be applied held on the table until the end of the statement or transaction, and will prevent others from reading or updating the table.
· UPDLOCK specifies that update locks will be used instead of shared locks, and will hold the locks until the end of the statement or transaction.
· XLOCK specifies that an exclusive lock be used and kept activated held until the end of the end of the transaction on all data being processed by the statement. The granularity of XLOCK will be adjusted if it is used with the PAGLOCK or TABLOCK hints.
SQL Server 7.0 and SQL Server 2000 Lock Escalation Options
You can override how SQL Server performs locking on a table by using the SP_INDEXOPTION command. Below is an example of code you can run to guide tell SQL Server to use page locking, not row locks, for a specific table:
SP_INDEXOPTION 'INDEX_NAME', 'ALLOWPAGELOCKS', TRUE
When FALSE, page locks are not used. Access to the specified indexes is obtained using row- and table-level locks only.
SP_INDEXOPTION 'INDEX_NAME', 'ALLOWROWLOCKS', TRUE
When FALSE, row locks are not used, . aAccess to the specified indexes is obtained using page- and table-level locks only.
SQL Server 2000 Lock Escalation Options
SP_INDEXOPTION 'INDEX_NAME', 'DISALLOWPAGELOCKS', TRUE
When TRUE, page locks are not used,. aAccess to the specified indexes is obtained using row- and table-level locks, only.
SP_INDEXOPTION 'INDEX_NAME', 'ALLOWROWLOCKS', TRUE
When TRUE, row locks are not used., aAccess to the specified indexes is obtained using page- and table-level locks, only.
When these commands are used, they affect all queries that eaffect these indexes. This command should not be used unless you know, positively, that they always produce the results you desire.
Important
The SQL Server query optimizer automatically makes the correct determination. It is recommended that you do not override the choices the optimizer makes. Disallowing a locking level can affect the concurrency for a table or index adversely. For example, specifying only table-level locks on a large table accessed heavily by many users can affect performance significantly. Users must wait for the table-level lock to be released before accessing the table.
Optimistic concurrency means you read the database record, but don't lock it. Anyone can read and modify the record at anytime and you will take your chances that the record is not modified by someone else before you have a chance to modify and save it. As a developer, the burden is on you to check for changes in the original data (collisions) and act accordingly based on any errors that may occur during the updating e process..
With optimistic concurrency, the application has to check for changes to the original record to avoid overwriting changes. There is no guarantee that the original record has not been changed, because no lock has been placed on that data at its source - the database. Hence, there is the real possibility of losing changes made by another person.
Optimistic Concurrency Strategies
If you are in a performance state-of-mind, there are chances of your chosingchances are you will go with optimistic concurrency. Optimistic concurrency frees up database resources as quickly as possible so that other users and processes can act upon that data as soon as possible.
There are four popular strategies to deal ing with optimistic concurrency:
1. Do Nothing.
2. Check for changes to all fields during update.
3. Check for changes tof modified fields during update.
4. Check for changes to timestamp (row version) during update.
All of these strategies have to deal with the shaping of the Update T-SQL Command sent to the database during the updating of the data. The examples below are not very detailed on purpose and assume a basic understanding of ADO.NET.
Optimistic Concurrency on Update Strategy #1 - Do Nothing
The simplest strategy for dealing with concurrency issues during the updating of data is to do nothing.
The update command will not check for any changes in the data, only specify the primary key of the record to be changed. If someone else changed the data, those changes will more than likely be overwritten:
Update Product
Set
Name = @Name
Where
ID = @ID
One would hope that this means either 1) the application is a single-user application, or 2) the chance of multi-user update collisions is very unlikely and the repercussions of overwriting data is negligible.
Optimistic Concurrency on Update Strategy #2 - Check All Fields
With this strategy, the update command will check that all fields in the row (usually minus BLOB fields) are equal to their original values when performing the update to assure no changes have been made to the original record. A check of the return value of the ExecuteNonQuery Command will indicatetell you if the update actually took place. The return value of the ExecuteNonQuery Command is typically the number of rows affected by the query.
Update Product
Set
Name = @Name,
Where
ID = @ID
AND
Name = @OriginalName
AND
Price = @OriginalPrice
This is essentially what CommandBuilder creates when using DataSets and is a strategy that doesn't want to see any changes to the data.
Optimistic Concurrency on Update Strategy #3 - Check Only Changed Fields
Rather than checking all fields in the row to make sure they match their original value, this strategy checks only those fields whichthat are being updated in the command.
Update Product
Set
Name = @Name
Where
ID = @ID
AND
Name = @OriginalName
This strategy only cares that it is not overwriting any data and could care less thant other fields in the record may have been changed. This could create an interesting combination of data in the row.
Optimistic Concurrency on Update Strategy #4 - Implement Timestamp
SQL Server has a timestamp ( alias rowversion ) field that is modified every time a change is made to a record that contains such a field. Therefore, if you add such a field to a table you only have to verify the timestamp record contains the same original value to be assured none of the fields have been changed in the record.
Update Product
Set
Name = @Name
Where
ID = @ID
AND
TimestampID = @TimestampID
This is the same as Strategy #2 above without the need for checking all fields.
Optimistic concurrency has a performance component to it that suggests a higher performing ASP.NET website.
There are other methods of achieving optimistic concurrency, but I think the ones above are the most popular. A developer needs to look at the application itself to determine which strategy makes sense. The DataSet, Command Builder, and DataAdapter typically handle this stuff for you using Strategy #2. However, if you work with objects instead of DataSets, you need to handle concurrency issues yourself.
Let's try to do it the other way around.
When I store the rows in the cross-reference table first.
Then use the autoinc recordID from the first entry and save that in a separate field that will be used to link these records to the main table.
Crossref table:
ID LinkedID
1 1
2 1
3 1
Main table:
LinkedID
1
They only problem is that I need to retrieve the ID (autoinc) from the first record that is created.
Does anybody have a solution for that?
that function returns the last generated id, by an insert in that same session/db connection.
which is exactly what I was referring to:
you should have 1 table with that identity field, and insert there first, and retrieve the id value generated using the
SELECT SCOPE_IDENTITY()
query. forget about @@identity (unless you use sql 7, where scope_identity does not exist)
> UPDATE AgendaRelaties SET Linkednr = @linked_id WHERE ID = @linked_id
how can you have that value of ID = @linked_id when that value has been generated just above?
> What remains is the question how to execute this from within Delphi?
see here for example(s), using the adodb.tlb_command:
http://www.experts-exchang
They only problem is that I need to retrieve the ID (autoinc) from the first record that is created.
Does anybody have a solution for that?
All you need to do is to write your query with an output variable. See the basic format in the snippet. Basically, you can do the insert and get back the Identity value in order to go on to create teh FK's for other related tables.
locking should be done by the database itself
not by the developer
you should start a transaction
change table 1
change table 2
change table 3
end transaction
if all this is to prevent holes in your column then put that idea aside
holes will happen, no matter how you program
you could use two columns, one a autogenerated id and the other your next id
@DelphiWizard,
Yes, but then it was suppost that the ID came from the main table. Which isn't available at that time.
If you have already inserted the main table's record and retrieved the Identity value (and, again, I would suggest using the OUTPUT technique rather than the SCOPE_IDENTITY() or, heaven forbid, @@IDENTITY), then you should have that value available to you as you are inserting the additional data in other tables.
As Geert pointed out, if you are concerned about needing to ensure that all or none of the data is committed to the tables, then wrap your process in a Transaction rahter than trying to selectively lock tables yourself.
There was a recent article about there being a bug in the SCOPE_IDENTITY() function which can under certain circumstances return the wrong value. The OUTPUT technique does NOT suffer from this problem.
The customer can be in that crossref table multiple times (related to other main-records).
This shouldn't be a problem as long as you have the unique index set up so that it includes the main record ID and the customer ID (although, I would probably set up 2 unique indexes, one with main record then customer IDS and the other with Customer then main record IDs if you will be searching by either combination or just the main record or customer ID).
By definition, using the OUTPUT technique gives you the Identity that was just created for the record that was just inserted . . . kind of like a little built in trigger that hands it back to you. ;-)
Two people executing the SP will each a) insert their own data which will b) have its own Identity value which will then be c) returned to the respective SP executions.
Pretty cool, huh? ;-)
@angelIII,
I can confirm that scope_identity() is multi-user safe!
There was a recent article with confirmation from Microsoft that under certain circumstances both the @@Identity and the SCOPE_IDENTITY can return the wrong value in SQL Server 200 and 2005. Supposedly the problem is fixed in SQL Server 2008.
no, because the session that generated the identity will pass exactly that value via scope_identity to the variable.
as from there, it will go through the output to the calling code, it cannot go to any other session.
That was the "official party line" until recently when it was discovered that there are, indeed, circumstances where that design fails. However, Microsoft has not made a big deal out of it (other than to say, "Yes, we have confirmed it but we told people to use the OUTPUT approach. Anyway, it's fixed in SS2008") mostly, I suspect, because they believe it is now fixed in SS2008 and they are moving on to SS2010.
@Delphiwizard,
I assume that the number of users doesn't influence this behaviour, otherwise it's of no use. Would be nice to get that confirmed by you though.
The number of users doesn't impact the OUTPUT technique. As for confirmation, I am looking for the article I mentioned in the above point to angelIII. However, since there is, in effect, no delay between the insert and the obtaining of the Identity vlaue when using the OUTPUT technique, unlike the execution of the INSERT statement and then executing a statement to set a variable to the SCOPE_IIDENTITY value (which, admittedly is a very small delay), the danger should be intuitively less. However, the particular circumstances under which the SCOPE_IDENTITY failed (which Microsoft was actually able to reproduce) did not involve more than 2 or 3 processes wexecuting at the same time.
All I am saying is that @@IDENTITY was known to have some issues and that is why SCOPE_IDENTITY came about but SCOPE_IDENTITY (in SS2000 and 2005) is now also known to have issues (although, admittedly, not nearly to the extent that @@IDENTITY does ;-).
DelphiWizard,
>>aspAgendaRela
>sorry, but I don't know how/why that works in Delphi...
You have to assign the value of the returned parameter to a variable in Delphi, kind of like how you assigned the value of a variable to the parameters in order to call the SP except swapping the two two sides of the assignment. ;-)
8080 diver:
You have to assign the value of the returned parameter to a variable in Delphi, kind of like how you assigned the value of a variable to the parameters in order to call the SP except swapping the two two sides of the assignment. ;-)
I would very much appreciate it if you would give my an example for:
1. Correct StoredProcedure using the OUTPUT-way. Returning the ID-field of table "AgendaRelaties".
2. How to handle this in Delphi?
I would very much appreciate it if you would give my an example for:
1. Correct StoredProcedure using the OUTPUT-way. Returning the ID-field of table "AgendaRelaties".
2. How to handle this in Delphi?
Well, I have already posted a description of the answer to #2 and, as to #1, I posted my previous response with that example in a hurry, so I think the following is probably going to work better. (Although, given that you have already assigned the points, you do realize that having left part of the problem as "an exercise for the student" is not to be unexpected. ;-)
By the way, I tend to prefer using the RETURN value to indicate an error. ;-)
I've started a new question to get the remainder of this one corrected.
http://www.exper
Business Accounts
Answer for Membership
by: angelIIIPosted on 2009-11-04 at 00:27:15ID: 25737360
what about using a IDENTITY field... that way, sql server will generate the value on the INSERT of the record, and after the insert, you use
SELECT SCOPE_IDENTITY() to get "your" id value generated.