Mega Code Archive

 
Categories / Delphi / ADO Database
 

Ms sql server autoincremental

{ sql server a dbexpress ile baglanirken database in verdigi identity yi dbx cekemiyor. dolayisi ile siz hangi kayda insert yaptiginizi bilemiyorsunuz.. ado hangi kayda insert yapildigini bildigi icin bunu dbx ya da midas vb.. yontemleri kullananlar icin yazdim buraya.. oncelikle asagidaki gibi bir table create edilir... ms sql query analyzer dan } create table IdentityTable (ForTable sysname not null, Value int not null) { daha sonra asagidaki procedure create edilir.. } create proc GetNextIdentity @ForTable sysname, @Value int OUTPUT AS set nocount on begin tran /* if this is the first value generated for this table, start with zero */ if not exists (select * from IdentityTable where ForTable = @ForTable) insert IdentityTable (ForTable, Value) values (@ForTable, 0) /* update must be before select to issue a lock and prevent duplicates */ update IdentityTable set Value = Value + 1 where ForTable = @ForTable select @Value = Value from IdentityTable where ForTable = @ForTable commit tran return @value { sql query analyzer in icinden kullanimi asagidaki sekilde.. delphi den de bir adet stored procedure alinir. tablo adi parametre olarak gecilir.. donen deger database in insert yapacagi identity numarasidir.. declare @MyIdentity int exec @MyIdentity = GetNextIdentity @ForTable = 'SI_SICIL', @Value = 0 select @MyIdentity iyi calismalar.. Kaynak : M$ nin sitesi winlinux@mynet.com 11 mart 2004 02:28 AM }