473,508 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SELECT and UPDATE in the same query

I have a table with only one row to get unique ID-numbers. To get the next
ID-number I have to increase the number in the row: first, get the number
from the database, and then do the update.

Some ASP-code:

Set rs = db.execute("SELECT id FROM tbl_id")
id = rs("id")
db.execute("UPDATE tbl_id SET id ="& id + 1 &"")

This is not safe: two different sessions cat get the same ID.

Is it possible to increase the value of id in tbl_id and get the value at
the same time? Or is there any other way?

/matte
Jul 20 '05 #1
6 27091
[posted and mailed, vänligen svara i nys]

Mattias Yngvesson (ma*****@mypix.se) writes:
I have a table with only one row to get unique ID-numbers. To get the next
ID-number I have to increase the number in the row: first, get the number
from the database, and then do the update.

Some ASP-code:

Set rs = db.execute("SELECT id FROM tbl_id")
id = rs("id")
db.execute("UPDATE tbl_id SET id ="& id + 1 &"")

This is not safe: two different sessions cat get the same ID.

Is it possible to increase the value of id in tbl_id and get the value at
the same time? Or is there any other way?


CREATE PROCEDURE get_id @id int OUTPUT AS

BEGIN TRANSACTION

SELECT @id = id FROM tbl_id (UPDLOCK)
UPDATE tbl_id SET id = @id + 1

COMMIT TRANSACTION

Actually this can be shorten to:

UPDATE tbl_id
SET ? = id,
id = id + 1
FROM tbl

Although, I don't know if you can use a parameter placeholder in this way.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Erland Sommarskog wrote:
Actually this can be shorten to:

UPDATE tbl_id
SET ? = id,
id = id + 1
FROM tbl

Although, I don't know if you can use a parameter placeholder in this way.


I'm curious. Although I'm aware of this SQL Server feature I presumed
that the use as above would imply that the id you get back is the one
prior to the increment (following th erule that the right hand side of
teh SET executes to completion before setting the left hand side). Is
that correct?

Cheers
Serge
Jul 20 '05 #3
Can you use stored procedures instead of in-line sql?

Mike
Jul 20 '05 #4
Serge Rielau (sr*****@ca.ibm.com) writes:
I'm curious. Although I'm aware of this SQL Server feature I presumed
that the use as above would imply that the id you get back is the one
prior to the increment (following th erule that the right hand side of
teh SET executes to completion before setting the left hand side). Is
that correct?


I would expect so too. That was also how Mattias' original ASP code worked.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
Well this is syntax that I never really use but it works. I may want to
consider this technique in the future...
declare @vid int
UPDATE tbl_id
SET @vid = id + 1,
id = id + 1
FROM tbl_id
select @vid

Danny
"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn*********************@127.0.0.1...
Serge Rielau (sr*****@ca.ibm.com) writes:
I'm curious. Although I'm aware of this SQL Server feature I presumed
that the use as above would imply that the id you get back is the one
prior to the increment (following th erule that the right hand side of
teh SET executes to completion before setting the left hand side). Is
that correct?


I would expect so too. That was also how Mattias' original ASP code
worked.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 20 '05 #6
Danny,

I would advise against it. Not only is it non-standard proprietary
Microsoft syntax, but if table tbl_id contains more than one distinct
value in column id, then the result in @vid will be undefined. @vid will
then probably hold the value of whatever row happened to be processed
last.

Gert-Jan
Danny wrote:

Well this is syntax that I never really use but it works. I may want to
consider this technique in the future...

declare @vid int
UPDATE tbl_id
SET @vid = id + 1,
id = id + 1
FROM tbl_id
select @vid

Danny

"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn*********************@127.0.0.1...
Serge Rielau (sr*****@ca.ibm.com) writes:
I'm curious. Although I'm aware of this SQL Server feature I presumed
that the use as above would imply that the id you get back is the one
prior to the increment (following th erule that the right hand side of
teh SET executes to completion before setting the left hand side). Is
that correct?


I would expect so too. That was also how Mattias' original ASP code
worked.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 20 '05 #7

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

Similar topics

1
11105
by: avinash | last post by:
hi myself avi i am developing one appliacaion in which i am using vb 6 as front end, adodb as database library and sql sever 7 as backend. i want to update one table for which i required data from...
17
4971
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
6
9307
by: GSteven | last post by:
(as formerly posted to microsoft.public.access.forms with no result) I've created a continuous form which is based on a straightforward table (ex - customers - 100 records). On the form there is...
1
2613
by: Greg Strong | last post by:
Hello All, Any reason why a select query that runs will not run as an update query? What I've done is created a select query that runs successfully. The query has several custom functions to...
5
1831
by: cdtsly | last post by:
Hi i have a table with all value at 4 i select all lines in a fetch i update one with a value of 7 i update all the row in the fetch with a value 5 the result is that all my row are at 5 and...
5
8314
by: parwal.sandeep | last post by:
Hello grp! i'm using INNODB tables which are using frequently . if i fire a SELECT query which fetch major part of table it usually take 10-20 seconds to complete. in mean time if any UPDATE...
3
2660
by: eholz1 | last post by:
Hello PHP programmers. I had a brilliant idea on one of my pages that selects some data from my mysql database. I first set the page up to display some info and an image, just one item, with a...
9
4375
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
3
9107
by: RAG2007 | last post by:
I'm using the QueryDef and Execute method to update a record in my MySQL backend. Problem: When the Passthrough update query is defined using QueryDef, it becomes a select query, and I cannot use...
2
1880
by: DuncanIdaho | last post by:
Hi Apologies if this is similar to a (very) recent post. I was wondering if it is possible to execute an update query that contains a select statement on an MS access 2000 database. I have...
0
7123
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
7326
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
7383
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...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
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
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.