Adding Auto Increment column in destination SSIS? - sql

I am creating a package in SSIS and this will insert records based on the look up operation as like below .
Source : View
Destination : Table
NOw I have an issue while inserting record i need to insert into a destination column which is not in the source and that column needs to be autoincremented by 1. How to do it here.
Any suggestion is welcome ?
Regards

Let us say the column name is "Id". If column "Id" already has an identity to it, then you're good. If not, then in your destination table you will have to create a new identity column, remove the "Id" column and then rename new column to "Id".
ALTER TABLE (yourTable) ADD NewColumn INT IDENTITY(1,1)
ALTER TABLE (yourTable) DROP COLUMN Id
EXEC sp_rename 'yourTable.NewColumn', 'Id', 'COLUMN'
After this in your SSIS destination do not map any column from the source to Id column and the Id column in database will start from value 1 and automatically increment by a value of 1. Hope it helps.

You could add to target table column defined as IDENTITY/AUTO_INCREMENT/DEFAULT sequence_name.NEXTVAL.
In your SSIS package you need to skip that column to allow DB to populate that column.

Related

How to add SUPER column to existing AWS Redshift table?

GOAL
I would like to add a Redshift SUPER column to and existing redshift table.
I need this to store JSON data there
CODE
This is how Normally I would add a new column.
ALTER TABLE products
ADD COLUMN created_at NOT NULL;
1. Tried
CREATE TABLE temp_test_persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
ALTER TABLE temp_test_persons ADD COLUMN newSuperColumn SUPER NOT NULL;
Error running query: ALTER TABLE ADD COLUMN defined as NOT NULL must have a non-null default expression
Reviewed Solutions
Alter column data type in Amazon Redshift
AWS Redshift - Add IDENTITY column to existing table
Adding column to existing tables
Add dynamic column to existing MySQL table?
SQLAlchemy: How to add column to existing table?
adding columns to an existing table
add column to existing table postgres
How to add not null unique column to existing table
Adding columns to existing csv file using super-csv
UCanAccess: Add column to existing table
Adding columns to existing redshift table
How to add column to existing table in laravel?
Add column to existing table in rds
oracle add column to existing table
The solution is to set a default value for the column. Then you can define the column as NotNull.
Like this
ALTER TABLE temp_test_persons
ADD COLUMN newSuperColumn SUPER NOT NULL
DEFAULT '';
ALTER TABLE temp_test_persons
ADD COLUMN newSuperColumn SUPER;

Add new column after existing column in sqlite

I need a SQL query which adds a new column after an existing column, so the column will be added in a specific order.
ALTER TABLE table_name ADD COLUMN MiddleName varchar(50) AFTER Gender.
You have two options. First, you could simply add a new column with the following:
ALTER TABLE {tableName} ADD COLUMN COLNew {type};
Second, and more complicatedly, but would actually put the column where you want it, would be to rename the table:
ALTER TABLE {tableName} RENAME TO TempOldTable;
Hope it helped.

add auto_increment to an existing db

I'm new to SQL. I created a table and filled it with some data. Now I want to change the properties of my primary key, so it will auto increment every single time by itself when adding data.
I know how to do it when creating a table, but where do I add auto_increment now, when I already have a created table with filled data?
Update
Try
ALTER TABLE [TableName] DROP COLUMN ID
ALTER TABLE [TableName] ADD ID INT IDENTITY(1,1)
If you had entered data to your table review this link that have an answer to you question SQL Server, How to set auto increment after creating a table without data loss?
You can't change logical structure column when you have some filled data in column. That's impossible. You must deleting data from column and use ALTER TABLE to change properites column

SQL sever, Make a existing column primary and set to auto increment

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

How to add unique column in sybase?

I have a sybase db table in which i need to add a new column.
The conditions: The column must not allow nulls and be unique.
what is the alter table sql to achieve this?
EDIT:
It is a varchar type column.Yes the table as of now is empty, but when filled it is ensured that unique values would be filled in.
I tired executing
alter table Test add Name varchar not null unique
i get error saying default value must be specified as not null is given.
but i want to add unique constraint so do i really need to specify default?
thanks
Unique values are specified as part of an index on the column, not in the column definition itself.
Try:
alter table Test add Name varchar not null
create unique index index_name_unique on Test (Name)
The ASE reference manual can help with more detail.
Once a table has been created Sybase ASE does not allow addition of NOT NULL column directly unless a default is specified for the column. However, if the table is still empty you can do the following -
First add the new column as a NULL column to the table using alter table command -
alter table Test add Name varchar(100) null
Once this has been done, try modifying the same column Name in the table Test using the alter table script -
alter table Test modify Name varchar(100) NOT NULL
and you will see that you are able to modify the Name column to a NOT NULL column using these steps. This is because at this time Sybase server checks as there is no data in the table hence the NOT NULL constraint is not checked and the column is made NOT NULL. Hence, we are able to skip the default constraint.
In case there would have been some data already present in the table Test, then we need to add one more step in between steps 1 and 2 which will add default values to the existing rows in the table. This can be done via a script for previous data and then following the step 2.
To make the column only allow unique values for the column you need to add a unique key constraint using the following syntax -
alter table Test add constraint UK1 unique(Name)