I have a table which is having a column name ID field of integer type. It was declared IDENTITY. And it has data according to IDENTITY. But recently I removed IDENTITY column from that table. Now I want to change that to IDENTITY again. But this query says incorrect syntax
Alter table FuleConsumptions alter column TransactionID INT IDENTITY(1,1);
But I can perform the same task using SQL server designer in properties of the table.
What am I doing wrong here?
I think The identity column will hold the sequence of number thats why it thrown error
better you drop column then create it again and set IDENTITY
Alter Table FuleConsumptions Drop Column TransactionID
Go
ALTER TABLE FuleConsumptions
ADD TransactionID int;
go
Alter table FuleConsumptions alter column TransactionID INT IDENTITY(1,1);
Related
Here is my table and Id is unique constraint...Now I want to add Identity Column in ID .Can anyone help me how to do it with TSQL.
This is existing table.
You can only have 1 Identity Column per Table. So if you don't have one already, Just alter the table and add the Column. Like this
ALTER TABLE YourTableName
ADD IdCol INT IDENTITY(1,1)
You can rename your previous ID column to keep those values if you need them in your application
EXEC sp_rename 'TableName.id', 'oldid', 'COLUMN';
Then add new ID column with Unique constraint as follows
Alter table TableName Add id int identity(1,1) unique not null
I created a new table, without auto increment of the primary key and inserted set of predefined data's into the table. Now I need to set the auto increment for the primary key, which I'm not able to set. please suggest the mistake i have done in the query below.
ALTER TABLE SYNCBASE.AUTO_LOAD_DATASOURCES
ALTER COLUMN ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 62, INCREMENT BY 1)
Till 61 rows i have inserted the records, it has to increment from 62 onwards. but i;m getting error as
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=BIGINT;RCES
ALTER COLUMN ID;SET, DRIVER=3.53.95
SQLState: 42601
ErrorCode: -104
Thanks in advance
The issue might be coming from the fact that you already have data in your database and modifying the primary key would violate the data integrity. Or you may check whether your DBMS allows you to make changes to a table after creating it.
first of all... we need to drop the identity and then alter the column, only then it works.
alter table SYNCBASE.AUTO_LOAD_DATASOURCES alter column ID drop identity;
alter table SYNCBASE.AUTO_LOAD_DATASOURCES alter column ID set generated always as identity (start with 62, INCREMENT BY 1);
There is a syntax error in the DDL statement. The column definition doesn't need to be repeated. This statement will work:
ALTER TABLE SYNCBASE.AUTO_LOAD_DATASOURCES
ALTER COLUMN ID SET GENERATED ALWAYS AS IDENTITY (START WITH 62);
I have old SQL sever table with 5000 rows. It has a column called OrderID which has the data type int. But this table doesn't have a primary key and OrderID is not on the sorted order. Can you please tell me how can I make this OrderID column the primary key and make it auto increment
You can't add identity to an existing column.
Your best option is to create a new table with the same structure and an identity column, set identity_insert on and then copy your records from the old one into the new one.
Check out this answer from the MS SQL Forum
You can't add identity to existing column. Create a new column "new_OderId" , copy data from "OderId" column paste in "new_OderId" column.
#add new column to Order_table
alter table Order_table add new_OderId int
#copy data from OrderId to new_OrderId
update Order_table set new_OrderId=OderId
#drop OderId column
alter table Order_table drop column OrderId
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I add auto_increment to a column in SQL Server 2008
Hey all i created the table in gui however i forgot to set the primary key to autoincrement! name of the table is Emp_CV.
Something like this:
alter table Emp_CV alter column Applicant_ID NOT NULL int AUTO_INCREMENT
how can i accomplish this?
Am using SQL Server 2008!
The short answer is...you can't. Here's what you have to do:
Ensure nobody/nothing is using your table.
Add a new integer column that will be the new identity column. It must allow nulls and must have the appropriate identity value.
execute set identity_insert dbo.foo on -- must specify your table name.
Seed it with the values of the existing identity column:
update dbo.foo set new_id = id
Drop any keys/constraints in which the existing identity column participates.
execute set identity_insert dbo.foo off
Drop the existing column.
Alter the new column, changing its nullity to not null.
Execute the command dbcc checkident( {your-table-name-here} , reseed ).
Execute sp_rename and give the new column the same name as the old column.
Recreate the keys/constraints dropped in step #4.
Might want to run sp_recompile on any stored procedures referencing this table.
Easy!
I have a table set up that currently has no primary key. All I need to do is add a primary key, no null, auto_increment.
I'm working with a Microsoft SQL Server database. I understand that it can't be done in a single command but every command I try keeps returning syntax errors.
edit ---------------
I have created the primary key and even set it as not null. However, I can't set up the auto_increment.
I've tried:
ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment
ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment
I'm using NVARCHAR because it wouldn't let me set NOT NULL under int
It can be done in a single command. You need to set the IDENTITY property for "auto number":
ALTER TABLE MyTable ADD mytableID int NOT NULL IDENTITY (1,1) PRIMARY KEY
More precisely, to set a named table level constraint:
ALTER TABLE MyTable
ADD MytableID int NOT NULL IDENTITY (1,1),
CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (MyTableID)
See ALTER TABLE and IDENTITY on MSDN
If the table already contains data and you want to change one of the columns to identity:
First create a new table that has the same columns and specify the primary key-kolumn:
create table TempTable
(
Id int not null identity(1, 1) primary key
--, Other columns...
)
Then copy all rows from the original table to the new table using a standard insert-statement.
Then drop the original table.
And finally rename TempTable to whatever you want using sp_rename:
http://msdn.microsoft.com/en-us/library/ms188351.aspx
You can also perform this action via SQL Server Management Studio.
Right click on your selected table -> Modify
Right click on the field you want to set as PK --> Set Primary Key
Under Column Properties set "Identity Specification" to Yes, then specify the starting value and increment value.
Then in the future if you want to be able to just script this kind of thing out you can right click on the table you just modified and select
"SCRIPT TABLE AS" --> CREATE TO
so that you can see for yourself the correct syntax to perform this action.
If you have the column it's very easy.
Using the designer, you could set the column as an identity (1,1): right click on the table → design → in part left (right click) → properties → in identity columns, select #column.
Properties:
Identity column:
In SQL Server 2008:
Right click on table
Go to design
Select numeric datatype
Add Name to the new column
Make Identity Specification to 'YES'
you can try this...
ALTER TABLE Your_Table
ADD table_ID int NOT NULL PRIMARY KEY auto_increment;