473,396 Members | 2,030 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,396 software developers and data experts.

Locking a Table Within an INSERT

I'm trying to perform this insert in a non-procedural environment:

INSERT INTO table
(column1,
column2)
SELECT
:col1value,
MAX(column2) + :count
FROM table
WHERE column1 = :col1value
;

My problem is that two or more processes might be executing this
statement concurrently. If so, they could get the same value for
MAX(column2). But I want the second process to get the updated value
for that aggregate that is set by the first process, and insert an
incremented value based on THAT.

Is there a way I can write this statement so one instance locks out
the second one? In my environment, one statement is all I'm allowed.

Thanks,
Elliott
Jul 19 '05 #1
3 3941
sh*****@aol.com (Elliott) wrote in message news:<14**************************@posting.google. com>...
I'm trying to perform this insert in a non-procedural environment:

INSERT INTO table
(column1,
column2)
SELECT
:col1value,
MAX(column2) + :count
FROM table
WHERE column1 = :col1value
;

My problem is that two or more processes might be executing this
statement concurrently. If so, they could get the same value for
MAX(column2). But I want the second process to get the updated value
for that aggregate that is set by the first process, and insert an
incremented value based on THAT.

Is there a way I can write this statement so one instance locks out
the second one? In my environment, one statement is all I'm allowed.

Thanks,
Elliott

Use a sequence
replace the horrible expression by
<sequence>.nextval and you are set.

Sybrand Bakker
Senior Oracle DBA
Jul 19 '05 #2
> Use a sequence
replace the horrible expression by
<sequence>.nextval and you are set.

Sybrand Bakker
Senior Oracle DBA


Would that I could. Unfortunately, the business requirements are that
I know the generated values in advance of inserting them.

I've thought of establishing an intermediate table into which I could
insert the values from a sequence, together with other record
identifying information, to be used in a lookup later. But I'm dealing
with, in some cases, a couple of million rows per run, with a dozen
runs a day.

You can write me at es*****@ford.com if you'd care to hear more.
Jul 19 '05 #3


Elliott wrote:
Use a sequence
replace the horrible expression by
<sequence>.nextval and you are set.

Sybrand Bakker
Senior Oracle DBA

Would that I could. Unfortunately, the business requirements are that
I know the generated values in advance of inserting them.

I've thought of establishing an intermediate table into which I could
insert the values from a sequence, together with other record
identifying information, to be used in a lookup later. But I'm dealing
with, in some cases, a couple of million rows per run, with a dozen
runs a day.

You can write me at es*****@ford.com if you'd care to hear more.


<Various Random Ideas>
Have you thought about:

* Accessing a sequence once per run and adding 8 zeros to the end of the
number you get. Then add the number of the record to get a unique
identifer that is derived from a sequence. You could also use a sequence
that increments by 10 million each time. Oracle won't be phased by this
but check the size of the numeric data types used by non-oracle
languages to access the data.

* Using a numeric identifier that has decimal places. The left hand half
identifies the number of the row in the batch and the right hand half is
pulled from a sequence once at the start of the process.

* splitting the PK into two - a batch number (pulled from sequence like
above) and a number within a batch

</Various Random Ideas>

David Rolfe
Orinda Software
Dublin, Ireland

Jul 19 '05 #4

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

Similar topics

12
by: Puvendran | last post by:
Hi, We have encountered deadlock on a table which is used to generate sequential numbers for different categories eg typical entries Category Value TRADE_NO ...
9
by: john smile | last post by:
Hi All, I want to lock 2 tables on 2 servers using TABLOCKX hint. These tables function as semaphores in my application. It means when the tables are locked then other users will not be able to...
2
by: Randall Sell | last post by:
Hello all, Somewhere on these newsgroups I recall reading that SQL Server 6 and prior (when they were married with Sybase) used page locking and not row level locking. Hence you could be locking...
3
by: mahajan.sanjeev | last post by:
Hi All, I am using a SQLTransaction to insert records into a table. At one time, there are 5000 or more records to be inserted one by one. It takes some 20-25 mins for the entire process to run....
13
by: Jeff Davis | last post by:
Right now performance isn't a problem, but this question has me curious: Let's say I have a shopping cart system where there is a "products" table that contains all possible products, and an...
0
by: shakahshakah | last post by:
Just started investigating InnoDB after having used MyISAM tables in the 4.0.12 version, almost immediately ran into a locking issue with INSERTs, DELETEs, and transactions. Given the following...
2
by: qhjghz | last post by:
I have two tables say A_prod & A_staging. Now, I need to insert those rows in A_prod which are present in A_staging but not in A_prod. We can do this in chiefly two ways :- 1. We can simply...
3
by: Elliott | last post by:
I'm trying to perform this insert in a non-procedural environment: INSERT INTO table (column1, column2) SELECT :col1value, MAX(column2) + :count FROM table WHERE column1 = :col1value
3
by: Brad Pears | last post by:
I have a vb.net 2005 app that asks for a job number. After the job number has been entered, I run an SQL 2000 stored procedure that selects the appropriate row from the Jobs table. I do not want to...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.