Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Alter Column Counter

Posted on 2015-01-27
4
Medium Priority
?
313 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

618 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