SQL Server: Change Integer to AutoInc from within Delphi

Posted on 2008-11-10
1 Ratings
Last Modified: 2013-11-23

How can I change a field of type Integer into field of type AutoInc in SQL Server from within Delphi?
The data in the field is unique.

Thanks, Stef
Question by:Delphiwizard
    LVL 32

    Expert Comment

    by:Daniel Wilson
    From Delphi (or anywhere else you can execute SQL), execute the following SQL.

    This assumes that the actual values in the ID column are not important.  If they are ... not sure ...

    Also, execute each statement separately.

    Alter Table EEQ Add ID2 int not null identity(1,1);
    Alter Table EEQ Drop Column ID;
    exec sp_rename 'EEQ.ID2', 'ID', 'Column';

    Open in new window


    Author Comment

    The actual values in the current Integer-field are important to be kept in the AutoInc-field.
    So your approach probably want work for that.
    LVL 32

    Expert Comment

    by:Daniel Wilson
    OK, you're going to need to:
    • Create a new table that uses IDENTITY in the appropriate field
    • turn Identity_Insert on for that table
    • Select into that table from your live one
    • drop your live table
    • Rename the new table to the live name
    Need code for that?

    Author Comment

    Yes I would like to have some code for that.
    Bye the way, I have to migrate my MS Access database to SQL Server. Maybe this can be done at the same time?
    LVL 32

    Expert Comment

    by:Daniel Wilson
    You might do that migration now ...

    To create the table, use SSMS to script the existing table, but tweak that column's script to say INT NOT NULL IDENTITY (1,1) instead of just INT.  Also come up w/ a new name for the CREATE TABLE statement.

    Set Identity_Insert NewTableName ON
    Insert Into NewTableName (field1, field2, etc.) (Select Field1, field2, etc.) from OriginalTable
    Drop Table OriginalTable
    sp_rename 'NetTableName', 'OriginalTable', 'TABLE'

    Author Comment

    Here is the situation:
    I upsized the MS Access table to SQL Server.
    All AutoInc fields are currently changed to Integer fields in SQL Server.

    I will distribute an empty database (no records) to existing customers. The whole data conversion will be done from within my Delphi application. The customer should just be able to run the exe and all is done automatically.
    My idea was to insert the AutoInc-value from MS Access database into an Integer-field in SQL server and then change the fieldtype in SQL Server.

    Can you supply me with some details on how to proceed next?

    Author Comment

    Can I somehow set Indentity on an existing Integer-field?

    ALTER Table EEQ ALTER ID2 int not null identity(1,1);

    And how can I do that from within Delphi?
    LVL 32

    Accepted Solution

    >>Can I somehow set Indentity on an existing Integer-field?

    that would be really nice ... but, no you can't.

    The code I posted a couple days ago is a series of SQL statements.  Execute each in turn and you should have what you need:

    Set Identity_Insert NewTableName ON;
    Insert Into NewTableName (field1, field2, etc.) (Select Field1, field2, etc.) from OriginalTable;
    Drop Table OriginalTable;
    sp_rename 'NetTableName', 'OriginalTable', 'TABLE';

    Open in new window


    Author Comment

    Could you give me an example on how to convert an MS Access database to SQL Server?
    The code below is the one I currently use, but this will not work when with AutoInc fields and the values from MS Access will be re-numbered during conversion in MS SQL Server.
    procedure TDM.ConverteerDB;
      sl: TStringlist;
      i, j: Integer;
      veld: TField;
      veldnaam: String;
      sl := TStringlist.Create;
      ADOConnectionAccess.GetTableNames(sl, False);
      for i := 0 to Pred(sl.Count) do
        ADOTableAccessConversie.TableName := sl[i];
        ADOTableSQLConversie.TableName := sl[i];
        while (not ADOTableAccessConversie.Eof) do
          for j := 0 to Pred(ADOTableAccessConversie.FieldCount) do
            Veld := ADOTableAccessConversie.FieldList.Fields[j];
            Veldnaam := veld.FieldName;
              if ((Veld.DataType = ftWideString) OR (Veld.DataType = ftMemo) OR (Veld.DataType = ftWideMemo)) then
                ADOTableSQLConversie.FieldByName(Veldnaam).AsString := ADOTableAccessConversie.FieldByName(veldnaam).AsString
              else if ((Veld.DataType = ftInteger) OR (Veld.DataType = ftAutoinc)) then
                ADOTableSQLConversie.FieldByName(veldnaam).AsInteger := ADOTableAccessConversie.FieldByName(veldnaam).AsInteger
              else if (Veld.DataType = ftBoolean) then
                ADOTableSQLConversie.FieldByName(veldnaam).AsBoolean := ADOTableAccessConversie.FieldByName(veldnaam).AsBoolean
              else if (Veld.DataType = ftDateTime) then
                ADOTableSQLConversie.FieldByName(veldnaam).AsDateTime := ADOTableAccessConversie.FieldByName(veldnaam).AsDateTime
              else if (Veld.DataType = ftFloat) then
                ADOTableSQLConversie.FieldByName(veldnaam).AsFloat := ADOTableAccessConversie.FieldByName(veldnaam).AsFloat
              else if (Veld.DataType = ftBlob) then
                ADOTableSQLConversie.FieldByName(veldnaam).asVariant := ADOTableAccessConversie.FieldByName(veldnaam).AsVariant;
              ShowMessage('Erro with field: ' + veldnaam + ' in tabel: ' + sl[i]);

    Open in new window

    LVL 32

    Expert Comment

    by:Daniel Wilson
    The usually-recommended method is to use MS' Access Upsize Wizard.

    Not sure how to do it in code ...

    Author Comment

    Does anybody have an solution for this?

    Woulld something like this be allowed? Works with MS Access on a integer field.
    ALTER Tabel MyTable ALTER Column ID Counter
    ALTER Tabel MyTable ALTER Column ID int not null identity(1,1);
    Maybe the starting ID must be set to the MAX(ID) + 1

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now