?
Solved

Alter Column Counter

Posted on 2015-01-27
4
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 2000 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 66

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 48

Author Closing Comment

by:Dale Fye
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 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