Solved

Check the next number and then insert query

Posted on 2016-08-08
10
49 Views
Last Modified: 2016-08-08
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.
0
Comment
Question by:RIAS
  • 4
  • 4
  • 2
10 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41747153
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
 

Author Comment

by:RIAS
ID: 41747154
Cheers!

but have you ever tried an identity column? it will handle this for you?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41747164
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
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41747166
Try this:
DBCC CHECKIDENT ( table1, NORESEED )

Open in new window

0
 

Author Comment

by:RIAS
ID: 41747169
Vitor Montalvão , Thanks.
Anu suggestion on how do I modify my query above?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
ID: 41747170
Even Sequence is available for SQL Server and sometimes is better option than Identity columns.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41747180
but are you using an identity field or not?
0
 

Author Comment

by:RIAS
ID: 41747182
yes, identity field
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 41747186
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
 

Author Closing Comment

by:RIAS
ID: 41747220
Thanks!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now