Home > DeveloperSection > Blogs > Auto Increment id by Using Stored Procedure

Auto Increment id by Using Stored Procedure

Database Database 
4 Comment(s)
 25122  View(s)
Rate this:

Auto Increment id by Using Stored Procedure

In this blog I am going to explain that how to create a procedure which automatically increment id of table. For implementing this task I had created a table which has two columns such as id and name. Value in id column
is automatically inserted by procedure and value in name column is passed by variable in procedure.

Creating a table in SQLSERVER

create table AutoIncrementTable


     id bigint,

     name varchar(50)


After creating table now I am going to create a paramentrized stored procedure which insert value in table and
accept name as parameter. This procedure insert id in table automatically and increment id by 1.

Creating parametrized stored procedure

alter proc p_auto_increment_table_proc

@name varchar(50)


declare @Id bigint

select @Id=count(Id)+1 from AutoIncrementTable


     insert into AutoIncrementTable values(@Id,@name)


After creating stored procedure now execute it and see the result.

Executing stored procedure

p_auto_increment_table_proc 'Alok'

This will insert record in table and each time id is automatically incremented.

Auto increment id by using stored procedure

By Amit Singh on   6 years ago
This is the helpful article

Good Stuff

By mohan kumar on   5 years ago
Good Stuff..

Auto increment id in asp.net c#

By Shailesh Kumar on   4 years ago
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class JobSeeker_Registration_JobSeekerRegistration : System.Web.UI.Page
    SqlConnection con = new SqlConnection("Data Source=LENOVO-PC\\SQLEXPRESS;Initial Catalog=student;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
        SqlCommand cmd = new SqlCommand("select max(jobid) from Jobseekerlogin", con);
        int count = Convert.ToInt16(cmd.ExecuteScalar()) + 1;
        Label1.Text = count.ToString();

Database: Jobseekerlogin

jobid   int   (pk)
username   varchar
password   varchar

another way to increment max id

By Atul Pathrikar on   7 months ago
Create proc SP_MaxId
select isnull(max(Demo_ID),0)+1 from Tbl_Demo

Don't want to miss updates? Please click the below button!

Follow MindStick