I have a form with a label that should show an invoice number. The invoice
number should be generated by sql Server using an autoincremented technique.
However, after reading several articles, it seems that I can only retrieve
this value after an INSERT has been done in the database. I want to find out
what this autoincremented before an insertion.--
L. A. Jones 8 9244
Select MAX(ID) FROM MYTABLE
Assuming that ID is the IDENTITY (Autoincrement) column, this will give you
the highest number previously inserted.
--Peter
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"Dave" wrote:
I have a form with a label that should show an invoice number. The invoice
number should be generated by sql Server using an autoincremented technique.
However, after reading several articles, it seems that I can only retrieve
this value after an INSERT has been done in the database. I want to find out
what this autoincremented before an insertion.--
L. A. Jones
A better way would be to use:
select @@identity
After the record is inserted. If you do the "select max" route, and you
are not in a transaction, you run the risk of getting the wrong id. With
@@identity, it will return the last identiy value produced on the
^connection^, and you don't have to worry about other values inserted into
the table.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com wrote in message
news:B8******** *************** ***********@mic rosoft.com...
Select MAX(ID) FROM MYTABLE
Assuming that ID is the IDENTITY (Autoincrement) column, this will give
you
the highest number previously inserted.
--Peter
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"Dave" wrote:
>I have a form with a label that should show an invoice number. The invoice number should be generated by sql Server using an autoincremented technique. However, after reading several articles, it seems that I can only retrieve this value after an INSERT has been done in the database. I want to find out what this autoincremented before an insertion.-- L. A. Jones
Nicholas Paldino [.NET/C# MVP] wrote:
A better way would be to use:
select @@identity
Or:
SELECT SCOPE_IDENTITY( )
After the record is inserted. If you do the "select max" route, and you
are not in a transaction, you run the risk of getting the wrong id. With
@@identity, it will return the last identiy value produced on the
^connection^, and you don't have to worry about other values inserted into
the table.
The SELECT MAX route is a disaster waiting to happen.
And transactions itself are not even enough. Transaction
isolation level needs to be set high too.
Arne
Frankly, that is why I never let the database generate my ids for me. I
would rather produce them and set them myself. The problem just goes away
in that case.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Arne Vajhřj" <ar**@vajhoej.d kwrote in message
news:MyUAg.7914 5$fG3.45410@duk eread09...
Nicholas Paldino [.NET/C# MVP] wrote:
> A better way would be to use:
select @@identity
Or:
SELECT SCOPE_IDENTITY( )
> After the record is inserted. If you do the "select max" route, and you are not in a transaction, you run the risk of getting the wrong id. With @@identity, it will return the last identiy value produced on the ^connection^ , and you don't have to worry about other values inserted into the table.
The SELECT MAX route is a disaster waiting to happen.
And transactions itself are not even enough. Transaction
isolation level needs to be set high too.
Arne
Thank you fellows. I think I'm gonna use "select SCOPE_IDENTITY( )".
--
L. A. Jones
"Arne Vajhøj" wrote:
Nicholas Paldino [.NET/C# MVP] wrote:
A better way would be to use:
select @@identity
Or:
SELECT SCOPE_IDENTITY( )
After the record is inserted. If you do the "select max" route, and you
are not in a transaction, you run the risk of getting the wrong id. With
@@identity, it will return the last identiy value produced on the
^connection^, and you don't have to worry about other values inserted into
the table.
The SELECT MAX route is a disaster waiting to happen.
And transactions itself are not even enough. Transaction
isolation level needs to be set high too.
Arne
Yes, but in your original post you stated that you wanted to get the value
BEFORE an insert. Actually, Nick's last response was the better one. Generate
the ID yourself and the problem goes away.
Peter
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"Dave" wrote:
Thank you fellows. I think I'm gonna use "select SCOPE_IDENTITY( )".
--
L. A. Jones
"Arne Vajhøj" wrote:
Nicholas Paldino [.NET/C# MVP] wrote:
A better way would be to use:
>
select @@identity
Or:
SELECT SCOPE_IDENTITY( )
After the record is inserted. If you do the "select max" route, and you
are not in a transaction, you run the risk of getting the wrong id. With
@@identity, it will return the last identiy value produced on the
^connection^, and you don't have to worry about other values inserted into
the table.
The SELECT MAX route is a disaster waiting to happen.
And transactions itself are not even enough. Transaction
isolation level needs to be set high too.
Arne
I now see the deficiencies, but what if I have several connections? Would
they cause a problem? Also when I use "SELECT IDENTITY_SCOPE FROM Table1", I
get several null values. The amount of null values returned is the same as
the number of rows in the table.
--
L. A. Jones
"Peter Bromberg [C# MVP]" wrote:
Yes, but in your original post you stated that you wanted to get the value
BEFORE an insert. Actually, Nick's last response was the better one. Generate
the ID yourself and the problem goes away.
Peter
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"Dave" wrote:
Thank you fellows. I think I'm gonna use "select SCOPE_IDENTITY( )".
--
L. A. Jones
"Arne Vajhøj" wrote:
Nicholas Paldino [.NET/C# MVP] wrote:
A better way would be to use:
select @@identity
>
Or:
>
SELECT SCOPE_IDENTITY( )
>
After the record is inserted. If you do the "select max" route, and you
are not in a transaction, you run the risk of getting the wrong id. With
@@identity, it will return the last identiy value produced on the
^connection^, and you don't have to worry about other values inserted into
the table.
>
The SELECT MAX route is a disaster waiting to happen.
>
And transactions itself are not even enough. Transaction
isolation level needs to be set high too.
>
Arne
>
Dave,
I'm sorry but it seems you have missed the point.
@@IDENTITY and SCOPE_IDENTITY are only available in the current scope of a
stored procedure, and ONLY after there has been a row inserted.
Once again, if you want to get the maximum value of the last IDENTITY
column, you can use the Select MAX(COLUMNNAME) I showed. If you cannot do
this or need a value BEFORE an insert is made, you need to follow Nick's
advice and consider setting up your table so that you create the value before
executing the insert, and you put the value into the table as one of the
parameters.
You can also use a GUID column (UNIQUEIDENTIFI ER) for this.
Hope that clarifies,
Peter
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"Dave" wrote:
I now see the deficiencies, but what if I have several connections? Would
they cause a problem? Also when I use "SELECT IDENTITY_SCOPE FROM Table1", I
get several null values. The amount of null values returned is the same as
the number of rows in the table.
--
L. A. Jones
"Peter Bromberg [C# MVP]" wrote:
Yes, but in your original post you stated that you wanted to get the value
BEFORE an insert. Actually, Nick's last response was the better one. Generate
the ID yourself and the problem goes away.
Peter
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"Dave" wrote:
Thank you fellows. I think I'm gonna use "select SCOPE_IDENTITY( )".
--
L. A. Jones
>
>
"Arne Vajhøj" wrote:
>
Nicholas Paldino [.NET/C# MVP] wrote:
A better way would be to use:
>
select @@identity
Or:
SELECT SCOPE_IDENTITY( )
After the record is inserted. If you do the "select max" route, and you
are not in a transaction, you run the risk of getting the wrong id. With
@@identity, it will return the last identiy value produced on the
^connection^, and you don't have to worry about other values inserted into
the table.
The SELECT MAX route is a disaster waiting to happen.
And transactions itself are not even enough. Transaction
isolation level needs to be set high too.
Arne
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Justin |
last post by:
I have created a dataset with two tables and an insert command, I need to be
able to retreive the Key Identity after inserting into table "A" for use in
table "B".
Should I use ExecuteScalar() or is there a better solution?
Thanks, Justin.
|
by: Stu Lock |
last post by:
Hi,
I have a stored procedure:
--/ snip /--
CREATE PROCEDURE sp_AddEditUsers
(
@Users_ID int,
@UserName nvarchar(80),
@Password nvarchar(80),
|
by: Rico |
last post by:
Hello,
I am in the midst of converting an Access back end to SQL Server Express.
The front end program (converted to Access 2003) uses DAO throughout. In
Access, when I use recordset.AddNew I can retrieve the autonum value for the
new record. This doesn't occur with SQL Server, which of course causes an
error (or at least in this code it...
|
by: Susanne Klemm |
last post by:
Hello!
I use a procedure to insert a new row into a table with an identity
column. The procedure has an output parameter which gives me the
inserted identity value. This worked well for a long time. Now the
identity value is over 700.000 and I get errors whiles retrieving the
inserted identitiy value. If I delete rows and reset the identity...
|
by: Mark Olbert |
last post by:
I am struggling with trying to retrieve the value of an autoincrement identity field after a DetailsView Insert operation. The
DetailsView is bound to an SqlDataSource control.
So far as I can tell, nowhere in the arguments for either the Inserted event for the DetailsView or the Inserted event for the
SqlDataSource control is the value...
| |
by: gunnar.sigurjonsson |
last post by:
I´m having some problem retrieving identity value from my newly
inserted row into a view.
I have two tables T1 and T2 which I define as following
CREATE TABLE T1
(
id BIGINT GENERATED ALWAYS AS IDENTITY
(
START WITH 1
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
| |
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |