|
-
Apr 8th, 2003, 08:15 PM
#1
Thread Starter
Member
Generating New ID
I am new to SQL server.
In MySQL when inserting a new record, im locking the table first,
use max(ID) + 1 to get a new ID and then release lock after
succesfully insert a record.
How you do this in MS SQL ?
I try to use subquery like
VB Code:
Insert Into StockGroup Values((SELECT max(id) FROM stockgroup) +1, "test")
Sub query not allowed.
Please help.
Also how get it work in Store Procudure.
Thanks.
-
Apr 9th, 2003, 03:44 AM
#2
Hyperactive Member
VB Code:
INSERT INTO StockGroup (id, otherfield, and_so_on) SELECT MAX(id) + 1, "otherfield", "and_so_on" FROM StockGroup
where 'otherfield' and 'and_so_on' are strings.
This is the right syntaxis.
-
Apr 9th, 2003, 04:48 AM
#3
You could also consider using two queries, one for determining the max(id) and the other for the insertion.
Another (crude) method is to use SQL Server's own version of autonumber. In the design view of the table, allow for increment and seed of 1.
-
Apr 9th, 2003, 05:11 AM
#4
Thread Starter
Member
Originally posted by mendhak
You could also consider using two queries, one for determining the max(id) and the other for the insertion.
I wonder how to prevent other user from getting max(ID) when
there are still another user did the same.
As I told before I did lock entire table in mySQL to complete this
task. Table is locked when retrieving max(ID) and inserting a new
record.
Autonumber still the best ?
-
Apr 9th, 2003, 05:18 AM
#5
Hyperactive Member
In my opinion autonumber is the best.
-
Apr 9th, 2003, 06:22 AM
#6
Lively Member
Another (crude) method is to use SQL Server's own version of autonumber.
Why crude??
Normally this is all you need! And you don't have to worry about multiple users getting the same ID, you don't have to lock the table...
-
Apr 9th, 2003, 01:43 PM
#7
Thread Starter
Member
How do you reset autonumber ?
-
Apr 9th, 2003, 04:03 PM
#8
Fanatic Member
hmm
create a new table just like the one you had??
do a search, this question has been asked many times.
It Never Fails. Everytime I try to make a program idiot proof, the world makes a better idiot.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|