Enable an identity column that already has data in it? - sql

Someone disabled the identity column of a table in a SQL DB. Is it possible to re-enable that feature of the column even when there is already data in the table? And maintain the existing identity values?
I know I could copy the data to another table and reinsert it after setting Identity_Insert on.

You cannot switch on the IDENTITY on an existing column, that's just not possible in SQL Server right now (at least up to version 2012).
What you need to do is exactly what you describe:
create the new table in the structure you want, with the IDENTITY column
copy the data from the existing table into the new table, with SET IDENTITY_INSERT ON
drop the old table
rename the new table to the old table name
You can "re-enable" the identity specification in the visual table designer in SQL Server Mgmt Studio, but this really only does those above steps in the background, for you.

Just as *marc_s* said you can use
I dont know if there is any other way to do this but you can use
CREATE TABLE tblNewTable
(
//Put the columns and datatypes of the former table
)
INSERT INTO tblNewTable
AS
SELECT * FROM oldTable
Then drop the table usng
DROP TABLE oldTable
Then recreate the new table and add the identity column, then use
INSERT INTO tblNewRecreatedTable (//Columns of the new created table except the column with the identity
AS
SELECT //Columns of the table you copied the data to except the Columned that you defined identity
I hope it helps

Related

SQL set identity and retain values

I have a problem restoring a table.
I wanted to restore data by copying data from the backup to the table.
The problem is, the PK of the table has the Identity-Property set. So when I inserted the lost rows they got new IDs.
I created a new table without identity and put the data in there.
Now I want to turn on Identity on the PK column, which doesn't work.
Any what I can do or if this is possible at all?
You cannot add IDENTITY property to existing column. Either you insert in existing table with IDENTITY column with IDENTITY_INSERT ON' option or you create a newIDENTITY` column in the new table.
This post might help you
This will solve your problem
[https://stackoverflow.com/a/1049305/6652909]
If you have backup of table then TRUNCATE your table then set primary key then make it auto increment in table and then copy your table and make sure your data of table is entered properly, it worked for me may be it will helpful to you also.

DB2: How to add new column between existing columns?

I have an existing DB2 database and a table named
employee with columns
id,e_name,e_mobile_no,e_dob,e_address.
How can I add a new column e_father_name before e_mobile_no?
You should try using the ADMIN_MOVE_TABLE procedure which allows to change the table structure.
The ALTER TABLE only allows adding columns to the end of the table. The reason is that it would change the physical structure of the table, i.e., each row would need to be adapted to the new format. This would be quite expensive.
Using the mentioned procedure ADMIN_MOVE_TABLE you would copy the entire table and during that process change the table structure. It requires a significant amount of space and time.
In DB2 IBM i v7r1 you can do it, try on your DB2 version
alter table yourtable
add column e_father_name varchar(10) before e_mobile_no
I always do the following --
Take a backup/dump of table data and db2look
(If you dump to a CSV file as I do I suggest dumping in the new format so for example put null for the new column in the right place.
Drop table and indexes
Create table with the new colunn
Load data with old values
Recreate all indexes and runstats.
Once you have done it a few times it becomes old hat.

How to reseed identity in Amazon redshift and how to make it auto increment

I am wondering how can we reset/reseed an identity column in Amazon redshift
I also want to make an auto increment column in my table.
You can do something like this while creating the table.
create table test(id bigint identity(1,1))
Adding identity column in existing table is not possible, so you need to re-create a new table and then copy your data from existing table. While re-creating you can reset the identity as column as required.
I too had the similar scenario to rest the Identity column in Redshift especially when the table is truncated. Unfortunately I couldn't find any option to reset the identity column. Using the work around of drop and create the table whenever required to rest but this many not work in all the scenarios.

create table using exsiting table update/link to column

I want to create a new table with one of the columns linked/updated by a table on another database (but on the same server).
so when table A column is updated it will automatically update table b's column with the same information no data will be entered into this column from table b.
I have tried various different ways but can't find a way to do this with out updating column manually or setting up a server agent any help would be great.
if you want to make cross server query, please check sp_addlinkedserver
https://msdn.microsoft.com/en-us/library/ms190479.aspx
Once it is linked, just create a trigger, where you can use
select * from [server].[database].[schema].[table]

How do I create a trigger to insert a value into an ID field that is Max([ID Field])+1 on insert

When I add a new record I want SQL Server to automatically add a fresh ID.
There are already some records which have been migrated over (from Access) and until I finish preparing the server for other required functionality I will be manually migrating further records over (if this affects any possible answers).
What are the simplest ways to implement this.
The simplest way would be to make the column an IDENTITY column. Here is an example of how to do this (it's not as simple as ALTER TABLE).
Make use of the Identity field type. This will automatically create a value for you using the next available number in the sequence.
Here is an example of how to create an Identity column (add a new column) on an existing table
ALTER TABLE MyTable ADD IdColumn INT IDENTITY(1,1)