473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SCOPE_IDENTITY with ASP

I originally asked this on comp.databases.ms-sqlserver, but they've
referred me here.

I am seeing a problem with an ASP application, where I have 2 tables.
In the first table, the ASP inserts just 1 row and retrieves the
primary key of the new row using SCOPE_IDENTITY. It then uses that
primary key in the column of a second table (foreign key) to insert
many rows.

What I'm seeing is an intermittent problem where the foreign key in the
second table is not what it should be. I think the problem may be due
to the fact that the insert into the first table and the calling of
SCOPE_IDENTITY are done in 2 separate ASP statements with some ASP code
in between.

Is it possible that 2 users may be calling my ASP page at the same time
and causing a concurrency problem due to the INSERT and the
SCOPE_IDENTITY being done in 2 different SQL statements? I read that
SCOPE_IDENTITY always returns the last identity value generated from
"the current connection", so I thought that would mean that it wouldn't
get messed up by another ASP request. But now I'm thinking that
perhaps ASP uses connection pooling which could mean that 2 users could
be sharing the same connection which would cause this concurrency
issue.

Does anyone know if my theory of what's wrong is plausible?

Jul 22 '05 #1
2 6000
You haven't posted any code, so it is hard to make any
informed comments.

If you are not encapsulating your SQL logic in stored
procedure, consider doing so that way you have a degree
of separation amongst tasks.

Take a look at this link and see if it opens anything up
for you!

http://www.aspfaq.com/show.asp?id=2174
Jul 22 '05 #2
Larry wrote:
I originally asked this on comp.databases.ms-sqlserver, but they've
referred me here.

I am seeing a problem with an ASP application, where I have 2 tables.
In the first table, the ASP inserts just 1 row and retrieves the
primary key of the new row using SCOPE_IDENTITY. It then uses that
primary key in the column of a second table (foreign key) to insert
many rows.

What I'm seeing is an intermittent problem where the foreign key in
the second table is not what it should be. I think the problem may
be due to the fact that the insert into the first table and the
calling of SCOPE_IDENTITY are done in 2 separate ASP statements with
some ASP code in between.

Is it possible that 2 users may be calling my ASP page at the same
time and causing a concurrency problem due to the INSERT and the
SCOPE_IDENTITY being done in 2 different SQL statements? I read that
SCOPE_IDENTITY always returns the last identity value generated from
"the current connection", so I thought that would mean that it
wouldn't get messed up by another ASP request. But now I'm thinking
that perhaps ASP uses connection pooling which could mean that 2
users could be sharing the same connection which would cause this
concurrency issue.

Does anyone know if my theory of what's wrong is plausible?


As Adam said, we can't answer without seeing the code. My suggestion (which
again echoes Adam) would be to encapsulate the inserts into a single stored
procedure.

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Scrappy | last post by:
I have an ASP front end on SQL 2000 database. I have a form that submits to an insert query. The entry field is an "identity" and the primary key. I have used scope_identity() to display the...
1
by: Lauren Quantrell | last post by:
I'm using an Access2K front end on a SQL Server2K backend. I use Scope_Identity() in a lot of stored procedures to pass the newwly inserted record's unique ID to the next select statement in the...
5
by: Larry | last post by:
I am seeing a problem with an ASP application, where I have 2 tables. In the first table, the ASP inserts just 1 row and retrieves the primary key of the new row using SCOPE_IDENTITY. It then uses...
6
by: Hardy Wang | last post by:
Hi all, I have the following codes, but SCOPE_IDENTITY() just returns NULL to me. If I comment out SCOPE_IDENTITY() line and run @@IDENTITY line, it works fine!! Since I have a trigger on the...
4
by: Mr Newbie | last post by:
I was using this fine sqlIDQuery = "SELECT SectionID FROM SECTIONS WHERE (SectionID=SCOPE_IDENTITY());" UNTIL. . . . I added a parameter, now it fails, it doesent matter if I use the...
4
by: Mr Newbie | last post by:
The following code is comprises a function to create a section of a documemt. Each section is stored in a table. This successfully inserts a record in the Sections table, but the scope_identity()...
1
by: Justyn | last post by:
Hi everyone, I hope someone can help me out! I have added a trigger that gets fired on INSERT and which itself inserts data into another table WITHOUT an identity I used to get back the...
2
by: needin4mation | last post by:
Hi, I have generated my queries and selected the refresh the table option. This creates a select after the insert to get the scope_identity() so that I can use that key value in another table,...
8
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal =...
10
by: Mike | last post by:
Sql Server, Scope_Identity(), Ado.NET: Which is better? Using an output parameter to return Scope_Identity through ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.