473,513 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Server, how to the next value of identity(1,1) before insert

24 New Member
Folks,

How to the next value of identity(1,1) before insert?

SELECT IDENT_CURRENT('ExampleTable') + IDENT_INCR('ExampleTable');
--does not work when the table is empty!

USE [master]
GO

IF EXISTS (SELECT name FROM sys.databases WHERE name = 'ExampleDatabase')
DROP DATABASE [ExampleDatabase];
GO

CREATE DATABASE [ExampleDatabase];
GO

USE [ExampleDatabase];
GO

IF EXISTS (SELECT NAME FROM SYS.TABLES WHERE NAME = 'ExampleTable')
DROP TABLE dbo.ExampleTable;
GO

CREATE TABLE
dbo.ExampleTable
(
ID INT IDENTITY(1,1) NOT NULL,
Name NVARCHAR(50) NULL
);
GO

SELECT * FROM dbo.ExampleTable;
GO

--FAILS IN THIS CASE:
SELECT IDENT_CURRENT('ExampleTable') + IDENT_INCR('ExampleTable');
GO

INSERT INTO ExampleTable
VALUES
(
'Bill'
);
GO

SELECT * FROM dbo.ExampleTable;
GO

--WORKS IN THIS CASE:
SELECT IDENT_CURRENT('ExampleTable') + IDENT_INCR('ExampleTable');
GO

INSERT INTO ExampleTable
VALUES
(
'Steve'
);
GO

SELECT * FROM dbo.ExampleTable;
GO

--WORKS IN THIS CASE:
SELECT IDENT_CURRENT('ExampleTable') + IDENT_INCR('ExampleTable');
GO
Nov 5 '09 #1
4 7883
ck9663
2,878 Recognized Expert Specialist
Identity auto-increment itself. Why do you need to add one? Anyway, you might want to do a
Expand|Select|Wrap|Line Numbers
  1. isnull(IDENT_CURRENT('ExampleTable'),0)
  2.  
Good luck!!!

--- CK
Nov 5 '09 #2
qwedster
24 New Member
Hi!

It doesn't help, because I want to query from C# code. Every time when I want add a row, I call this method to get the next identity value like:

private int GetNextRowID()
{
int NextRowID =0;

try
{

using (SqlConnection ConnectionSql = new SqlConnection(ConnectionString))
{
StringBuilder SqlQuery = new StringBuilder();

SqlQuery.Append("USE [ExampleDatabase]; ");

// Returns 1 so works for the first insert when the table is empty before any insert:
// Returns 1 again for the second insert, so it FAILS:
SqlQuery.Append("SELECT ISNULL(IDENT_CURRENT('ExampleTable'), 0) ;");

SqlCommand CommandSql = new SqlCommand(SqlQuery.ToString(), ConnectionSql);

ConnectionSql.Open();

SqlDataReader DataReaderSql = CommandSql.ExecuteReader();

while (DataReaderSql.Read())
{
NextRowID = int.Parse(DataReaderSql.GetValue(0).ToString());
}

ConnectionSql.Close();

MessageBox.Show("Add Row.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return NextRowID;
}

Thanks


@ck9663
Nov 6 '09 #3
ck9663
2,878 Recognized Expert Specialist
You might want to reconsider your technique. If this is for a multi-user environment, you'll have a problem when you access your app and assign the next ID to you and someone else access it, the app will assign the same ID to him. So better reconsider everything.

Good luck!!!

--- CK
Nov 6 '09 #4
nbiswas
149 New Member
Try with @@Identity or Scope_Identity()
Nov 8 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
4117
by: Joe | last post by:
For the drop down box, how do we get the server side value For example, the following won't work because it produce <select name="streetdirectional" value="E">. But we need <OPTION VALUE="E"...
2
3833
by: Eugene | last post by:
Hi, Problem: I need to get the value of auto-incremented field from just inserted record In Oracle this is INSERT .. RETURNING command. In SQL Server there are @@IDENTITY, IDENT_CURRENT,...
3
2845
by: Stewart Allen | last post by:
I'm trying to find the next value in a table that a student needs to achieve once he has already passed one grade. *tblStudents* StudentID (PK) FirstName *tblGradingDates* DateID ...
4
59700
by: Diego | last post by:
Hi everybody! I wonder if there is an sql command that increases a sequence value by one and returns such a value. I would like to manage an id inside a program that connects to the database...
1
3232
by: dotnetguys | last post by:
Hi there, I want to pass textbox server control value through anchor tag to some other form how do i do Can somebody help me for this. Thanks
2
2146
by: Rajeesh123 | last post by:
I have a dom tree(same kind of treeview in .net). when user clicks on it , I pass the id to a hidden field and update it with ajax. The main page has one if clause to get the condition of hidden...
1
2319
by: aaron1234nz | last post by:
I am trying to write a stored procedure add a piece of data from table2 into another table1. I thought the query was going to be easy, but I was misteken. All I am trying to achieve is: for...
1
3014
by: skanemupp | last post by:
in this program when using the "c"-button it deletes the last token entered. i want to delete the token after the mousecursor. lets say the string is: 12*(81**.5+12) and i put the cursor between...
8
1587
by: Shila | last post by:
i am doing online meter project......there is table in my html page...in that table meter show continuesly diff values............. without resfrshing page table can take next value..... that is my...
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7166
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
7386
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
5689
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,...
1
5094
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...
0
4749
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
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
459
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.