delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (1)


No ,You´re not crazy Sql Server does not have generators.Format this article printer-friendly!Bookmark function is only available for registered users!
How to make a 'Work around' on this limitation.
Product:
Delphi all versions
Category:
Database-SQL
Skill Level:
Scoring:
Last Update:
03/19/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet SQL SERVER ORACLE INTERBASE DBEXPRESS GENERATORS SEQUENCES AUTOINC
Times Scored:
5
Visits:
3798
Uploader: Marcello Dias
Company:
Reference: N/A
 
Question/Problem/Abstract:
Used with Oracle or Interbase.
Just forget Microsoft Sql Server does not have generators.
Answer:



Hi,

If you´re used with Oracle or Interbase and suddenly based on the first
Article of the Murphy law one customer of yours come and say.
I want this system to run in a Microsoft Sql Server.

First of all i expect you not to be using IBX or other architecture that just
Don´t let you make the move(This week i convert a whole system From Interbase
Into SQL Server,for my happiness i´m using DbExpress ,based on the articles of guys
Like Cary Jensen,Bill Tod,Dr.Bob,Eric Harmon and others.

Second if you want to use DbExpress for Oracle or M$ SQL Server,just forget
D7 DbExpress original drivers,They have very annoying bugs at this moment, and
Are much slower than Core labs´ones.

But coming back to the main subject of this article.
Sql server does not have Generators(Interbase) or Sequences(Oracle) instead of that
They have of they call Identity Columns a kind of autoincrement fields.

I myself think them to be totally useless ,specially if you like me is a fan of Optmistic Locking,and use ClientDatasets a lot.

I just can´t image doing a master-detail unit without having generators.
And the method that i´m going to show you is not from my authory and to be
Honest I didn´t like it and I won´t recommend it for you to use it in a bank or in airline company, because you´re problably end up with an enourmous Bottle neck, But untill Microsoft open its yes its the best you can do.
========================================================
THE TABLE.

The first thing is to create this table in the SQL SERVER

Table name-  MYGENERATORS
Fields         -  GEN_NAME        char(20)
              -  ID               integer

THE STORED PROCEDURE
Create procedure gen_id (  @GenName varchar(30))
as
begin
      declare  @ID integer

      SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
      BEGIN TRANSACTION SP_GEN_ID

      select @ID = ID from MYGENERATORS where GEN_NAME =@GenName
      IF @@ERROR <> 0 GOTO LBL_ERROR
      update mygenerators set ID = ID + 1 where GEN_NAME=@GenName
      IF @@ERROR <> 0 GOTO LBL_ERROR

      COMMIT TRANSACTION SP_GEN_ID
      return @ID

LBL_ERROR:
      ROLLBACK TRANSACTION SP_GEN_ID
      return 0
end


GO

THE DELPHI CODE
SpSequencias is a Stored Procedure.
Set the StoreProcName to gen_id
In this example i´m using a client dataset with persistent fields but of course
You have to adapt it to our logic.

begin
     with spsequencias do
      begin {Start of with Stored proc do}
         try    {Start of try...except block}
            ParamByName('GenName').AsString := 'ULTCATEGORIAEST';
            ExecProc;
         DMClient.CLDSCategoriaEstCTG_ID.AsInteger:=ParamByName('Result').AsInteger;
            Close;
         except
            on e: EDatabaseError do
               begin
                  Close;
                  DMClient.CLDSCategoriaEstCTG_ID.AsInteger:=-1;
                  ShowMessage(‘Na error occurred in your database,Try again´);
                  DBE_CTG_NOME.SetFocus;
               end;
         end;  {End of try...except block}
      end; {End of with Stored proc do}


You´re probably find some articles in The Net similar to this one,but with different Contents,I prefer to be hard with MicroSoft since i think they must respect us the Customers and give us GENERATORS,if someone don´t like them don´t use them.
But since many programmers like me want to create Code that runs on several RDMS
by know we have to use this Clipper-times approach because if you don´t do so your code will be Microsoft Sql Server  incompatible

Special thank´s to Kevin Frevert and Philippe who helped me very much.


Regards,
Marcello Dias





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Microsoft wants you locked in
    Jim McKeeth (Apr 30 2003 7:32PM)

I think you hit the nail on the head with that last comment. The reason M$ doesn't give you generators is they want you to create your applications of thier products and then get locked in so you cannot upgrade to Oracle or a better DB later.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)