How to turn on/off IDENTITY_INSERT in Redshift - sequence

I have to turn off the identity on a table in redshift and insert the historical values into that.
If I try to insert values into an identity column I am getting below error
ERROR: 0A000: cannot set an identity column to a value

you can do it by following simple 2 step approach.
unload the historical table data and put it into file #s3
run copy command against this file to load target table by
specifying explicit_ids as parameter in your copy command.

There is no way to switch off identity values in Redshift. If the order of your identity is independent of records being new or historical, you can just insert the historical records and they will be inserted with new values for the identity key.

Related

SQL Server : alter the Identity seed

I am migrating data from one database to another. I have my scripts mostly together already, but I am trying to figure out the best way to make one change to a table in the new database.
I have a Customer table. That table has a customer_id column which is the identity column. I want to change the identity seed/increment from (1,1) to (200,1) without changing the customer_ids for the existing data I will be inserting into the table.
Old data is 101-108. Basically we want to keep the old data the same so it matches up with old records in other systems, but we want the new data to start seeding in at 200.
I tried Googling how to do this, but all my Googling came back with results where people wanted to change what column was the identity column, and not just change the identity seed number. Is there a simple query I can use to accomplish what I want to do?
You can use DBCC CHECKIDENT:
DBCC CHECKIDENT ('dbo.customer', RESEED, 200)
This will change the current seed value of the identity column of the specified table. If you need to insert specific identity values, you can SET IDENTITY_INSERT ON in your insert statement.
IDENTITY_INSERT
What I would do unset the new column as an identity (using alter table), then insert the data from the old table, and then reset the new column as the identity again, with whatever increment you want as per the link
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-2017

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.

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.

SQL Server: "Cannot insert the value NULL into column" - on identity column

I am inserting data into a table with an identity column called unique_id. I am leaving the unique_id column out of the insert, assuming that the value will then be seeded from the identity configuration on the column.
Insert statement:
INSERT INTO table_name (column_1, column_2, column_3, processed_dt)
VALUES ('A', 'B', 'C', GETDATE());
Error:
Cannot insert the value NULL into column 'unique_id', table 'db.dbo.table_name'; column does not allow nulls. INSERT fails.
When I double click on the unique_id column in SQL Server Management Studio, I see that the following values are set:
Identity: true
Identity Seed: 1
Identity Increment: 1
What other configuration may be wrong to cause the identity seed to not be used automatically?
UPDATE:
Based on some of the recommendations I am seeing, I want to add that this schema was converted by our vendor from Oracle into SQL Server. I'm guessing that part of that process must of included converting Oracle Sequences into SQL Server Identity columns. Something being "broken" with the identity column is certainly a possibility.
I'm definitely on the correct database, table, and column. The Identity on this column was not created today, it was created weeks ago.
Is there any configuration the vendor could have put in place that would disable the auto assignment of the seed value and force the developer to "fetch" the next seed value manually?
SQL Server stores the seed/nextID value to be used. I'm wondering if the conversion from Oracle neglected to set that value. Try using the the following command on that table to check what it's seed value is:
DBCC CHECKIDENT ( table_name, NORESEED )
Maybe it actually is null, which is causing the error. Then you can use a variation of the same command to 'reseed' or set the next value to be used.
For more information and options:
http://msdn.microsoft.com/en-us/library/ms176057(v=sql.110).aspx
This is a pretty old question, but I was lead here when one of my developers had this very issue and wanted to share what caused/fixed it for us.
Basically, on another tab in SQL server "Edit Top 200 Rows" was open for the specified table.
Once this tab was closed, the insert worked without issue.
Hope this helps someone!

Enable an identity column that already has data in it?

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