Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a table for UserId, UserName. my doubt is how can i increment the Userid. that is no need of entering always the Userid while inserting into the Database table.


it is resolved thanks for all ur answers
Posted
Updated 11-Feb-14 0:14am
v3

Use IDENTITY (Property)[^] for userID column. This will auto increment the userID automatically when an user is inserted into the user table.
 
Share this answer
 
v2
While making the SQL Table, you can make the required column Autoincrement(Identity=true), the column must be of int datatype. Then when ever the data is inserted into the table. The column automatically increment the set value.

try to create this table.

SQL
CREATE TABLE [dbo].[testtable](
	[userid] [int] IDENTITY(1,1) NOT NULL,
	[username] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


Thanks
 
Share this answer
 
You can set an auto-increment option while designing the table.
This will automatically increment the value everytime a row is inserted.

Try
http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/[^]
http://www.sitepoint.com/forums/showthread.php?41676-Auto-Increment-primary-key-in-MS-SQL-server[^]
 
Share this answer
 
this is demo but but it is useful for Auto ID


SQL
create table #tblTest(UserId  int,UserName varchar(50))

insert into #tblTest(UserId,UserName)
select isnull(max(UserId),0)+1,'Your Use Name' from #tblTest

select * from  #tblTest
drop table #tblTest
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900