Check the next number and then insert query

Hello,

I have  an insert query

 INSERT INTO table1 (
                ID
            ,AccountName
            , IsActive
            , IsPowerUser
         
        ) SELECT
                  @ID
            , UPPER(@AccountName)

How can I get the next id (datatype int) and then insert the values.
RIASAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
if you are using an identity field, you don't have to provide the value, it will be filled automatically.

 INSERT INTO table1 (AccountName, IsActive, IsPowerUser)
SELECT UPPER(@AccountName), ...

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
that can be something like this:
declare @id int
set @id = isnull((select max(id) from table1), 0)

Open in new window

but have you ever tried an identity column? it will handle this for you
0
 
RIASAuthor Commented:
Cheers!

but have you ever tried an identity column? it will handle this for you?
0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
Éric MoreauSenior .Net ConsultantCommented:
do you have a question? I just see a copy of my comment!

For info on identity column, check http://www.sqlteam.com/article/understanding-identity-columns
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Try this:
DBCC CHECKIDENT ( table1, NORESEED )

Open in new window

0
 
RIASAuthor Commented:
Vitor Montalvão , Thanks.
Anu suggestion on how do I modify my query above?
0
 
Vitor MontalvãoConnect With a Mentor MSSQL Senior EngineerCommented:
Even Sequence is available for SQL Server and sometimes is better option than Identity columns.
0
 
Éric MoreauSenior .Net ConsultantCommented:
but are you using an identity field or not?
0
 
RIASAuthor Commented:
yes, identity field
0
 
RIASAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.