---
title: "Auto Increment id by Using Stored Procedure"  
description: "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"  
author: "Anonymous User"  
published: 2011-03-15  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/133/auto-increment-id-by-using-stored-procedure  
category: "database"  
tags: ["database"]  
reading_time: 1 minute  

---

# Auto Increment id by Using Stored Procedure

In this blog I am going to [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) that how to create a procedure which automatically [increment](https://www.mindstick.com/forum/173/auto-increment-id) 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](https://www.mindstick.com/forum/34651/how-can-reset-identity-seeding-in-sql-on-id-column) is automatically inserted by procedure and value in name column is passed by [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) 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](https://www.mindstick.com/articles/803/using-stored-procedure-in-asp-dot-net) which [insert value in table](https://www.mindstick.com/forum/501/error-while-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)asdeclare @Id bigintselect @Id=count(Id)+1 from AutoIncrementTablebegin     insert into AutoIncrementTable values(@Id,@name)end
```

\

After creating stored procedure now [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) 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.

\
\

---

Original Source: https://www.mindstick.com/blog/133/auto-increment-id-by-using-stored-procedure

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
