[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Server: Change Integer to AutoInc from within Delphi

Posted on 2008-11-10
11
Medium Priority
?
655 Views
1 Endorsement
Last Modified: 2013-11-23
Hi,

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
1
Comment
Question by:Stef Merlijn
  • 6
  • 5
11 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22931637
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

0
 

Author Comment

by:Stef Merlijn
ID: 22931827
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.
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22931939
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?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:Stef Merlijn
ID: 22934017
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?
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22934413
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'
0
 

Author Comment

by:Stef Merlijn
ID: 22934544
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?
0
 

Author Comment

by:Stef Merlijn
ID: 22952482
Can I somehow set Indentity on an existing Integer-field?

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

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

Accepted Solution

by:
Daniel Wilson earned 2000 total points
ID: 22952619
>>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

0
 

Author Comment

by:Stef Merlijn
ID: 22952942
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;
var
  sl: TStringlist;
  i, j: Integer;
  veld: TField;
  veldnaam: String;
begin
  sl := TStringlist.Create;
  ADOConnectionAccess.GetTableNames(sl, False);
  for i := 0 to Pred(sl.Count) do
  begin
    ADOTableAccessConversie.TableName := sl[i];
    ADOTableAccessConversie.Open;
    ADOTableAccessConversie.First;
    ADOTableSQLConversie.TableName := sl[i];
    ADOTableSQLConversie.Open;
    while (not ADOTableAccessConversie.Eof) do
    begin
      ADOTableSQLConversie.Insert;
      for j := 0 to Pred(ADOTableAccessConversie.FieldCount) do
      begin
        Veld := ADOTableAccessConversie.FieldList.Fields[j];
        Veldnaam := veld.FieldName;
        try
          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;
        except
          ShowMessage('Erro with field: ' + veldnaam + ' in tabel: ' + sl[i]);
        end;
      end;
      ADOTableSQLConversie.Post;
      ADOTableAccessConversie.Next;
    end;
    ADOTableSQLConversie.Close;
    ADOTableAccessConversie.Close;
  end;
end;

Open in new window

0
 
LVL 32

Expert Comment

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

Not sure how to do it in code ...
0
 

Author Comment

by:Stef Merlijn
ID: 23039881
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
like
ALTER Tabel MyTable ALTER Column ID int not null identity(1,1);
Maybe the starting ID must be set to the MAX(ID) + 1
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

864 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