By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,949 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,949 IT Pros & Developers. It's quick & easy.

how to done auto increment with varchar datatype...

P: 2
Session_id varchar(5)primary key
Name varchar(30)
Price int
in this mysql database I want to increment the session Id as
S0001 so how can I do with auto increment....
pls suggest me,,,

pls quick reply...
Sep 15 '12 #1
Share this Question
Share on Google+
2 Replies

P: 134
You should be able to set auto increment on the field within the database when you are creating the fields. And then when putting data in the database the auto increment should do his work automatically.
Sep 15 '12 #2

ariful alam
P: 185
create a procedure that has a variable name @last_id

put the last session_id in this variable @last_id
Expand|Select|Wrap|Line Numbers
  1. if exists(select * from <table_name>)
  2. begin
  3.      select top 1 @last_id=session_id from <table_name> order by session_id desc
  4.      --use substring() method to cut out the 'S' and add 1 to 
  5.      --the existing value in another variable. this will cut 
  6.      --out pre zero's. so you have to test length to add pre 
  7.      --zero's if needed. and then use the last created id to 
  8.      --insert a new record.
  9. end
  10. else
  11. begin
  12.      set @last_id = 'S0001'
  13.      insert <table_name> values (@last_id[,<other_field>])
  14. end

hope, helps. :)
Sep 16 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.