Solved

Alter Column Counter

Posted on 2015-01-27
4
197 Views
Last Modified: 2015-01-27
Trying to alter a table to make a column a counter using TSQL.

Then, is there a way to set the start value of that counter also in TSQL
0
Comment
Question by:Dale Fye (Access MVP)
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40572670
A counter? Do you mean an IDENTITY column (similar to AutoNumber in Access)?

If so, you can't change an existing field to it. You would have to drop that column then recreate it using IDENTITY()

You asked how you could set the start value. The syntax of IDENTITY is IDENTITY(startvalue,increment) - see https://msdn.microsoft.com/en-us/library/ms186775.aspx for more information.

If dropping an existing column is not an option, then you can use a Sequence, and set that to be the DEFAULT for that column. Here's the syntax for that: https://msdn.microsoft.com/en-gb/library/ff878091.aspx
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40572676
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40572680
>Trying to alter a table to make a column a counter using TSQL.
Nope, you can't alter a column to be an identity column.   Essentially this is the same dilemma as Access changing an int to be an AutoNumber.  Your only options are:
Add a column as an identity
Drop the column, re-add it as an identity
Keep the column as an int (?) but add a trigger that gets the max() value, and adds one more for inserts

>Then, is there a way to set the start value of that counter also in TSQL
Yes, the seed value can be changed, say from id(1,1) to id(1000,1)
0
 
LVL 47

Author Closing Comment

by:Dale Fye (Access MVP)
ID: 40572702
Thanks, Phillip.

Dropping the column was an option, so I did that, then added it again as an Identity column.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

932 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

11 Experts available now in Live!

Get 1:1 Help Now