Cara Membuat Autonumber di SQLServer 2008

format yang diinginkan adalah CTC00001, untuk membuat autonumber di SQLServer2008 bisa dicoba alternatif kode berikut:

declare @houseno varchar (20); 

SELECT @houseno = 'CTC' + substring(convert(varchar, cast(max(substring(HOUSE_NO,4,5))+1 as float)/100000), 3,7)
FROM CONTOHTABEL;

Karena contoh di atas menggunakan store procedure, maka untuk input datanya seperti:

INSERT INTO CONTOHTABEL (HOUSE_NO) 
	VALUES (@houseno);     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s