INSERT a new column into an existing SQL table - sql

I have a "source data" table with columns A,B,C,D,E,F
I use this table to populate a live table by using
INSERT INTO LIVETABLE
SELECT *
FROM SOURCEDATATABLE
Recently, a new column (C1) was added to the LIVETABLE
All I want to do is insert a C1 column into my SOURCEDATATABLE between C and D so that it now is A,B,C,C1,D,E,F. There is no need to populate with data as the LIVETABLE accepts NULLs
Is there any easy solution?
EDIT - MISSING INFORMATION
This table is one of many and my approach to using the INSERT INTO is due to having to use dynamic SQL (for various other reasons) so I cannot specify the column names

There is a reason for the Mantra "I shall not use SELECT *" and you ran straight into it. Add the column to SOURCEDATATABLE (if necessary) and enumerate the columns in the SELECT clause using NULL for the new one.

The only way to insert a new column between two columns is to create a new table with the columns in the order you want, copy the data into it, drop the old table and rename the new table with the old name. Make sure you remove primary key identities to maintain the identity column.

Related

I want to add two columns names to an existing table in Impala Query

I am writing the following query to add a column at a specified position but getting the below error:
alter table quantum_raw_dev.rpt_backup_allocation
change upt_type upt_type STRING after tray_size;
You can add one or more columns to the end of the column list using:
ALTER TABLE <table_name> ADD COLUMNS (col_name col_type, ...);
[note: there is NO comma between column name and type]
Adding or Removing Columns
You can add one or more columns to the end of the column list using ADD COLUMNS,
or (with Impala only) you can delete columns using DROP COLUMN.
The general syntax is
ALTER TABLE tablename ADD COLUMNS (col1 TYPE1,col2 TYPE2,… );
ALTER TABLE tablename DROP COLUMN colname;
For example, you can add a bonus integer column to the employees table:
ALTER TABLE employees ADD COLUMNS (bonus INT);
Or you can drop the office_id column from the employees table:
ALTER TABLE employees DROP COLUMN office_id;
Notes
DROP COLUMN is not available in Hive, only in Impala. However, see “Replacing All Columns” below.
You can only drop one column at a time.
To drop multiple columns, use multiple statements or use the method to replace columns (see below).
You cannot add a column in the middle of the list rather than at the end.
You can, however, add the column then change the order (see above) or use the method to replace columns (see below).
As with changing the column order, these do not change the data files.
If the table definition agrees with the data files before you drop any column other than the last one,
you will need to recreate the data files without the dropped column's values.
If you drop the last column, the data will still exist but it will be ignored when a query is issued.
If you add columns for which no data exists, those columns will be NULL in each row.
Replacing All Columns
You can also completely replace all the columns with a new column list.
This is helpful for dropping multiple columns,
<h1>or if you need to add columns in the middle of the list<h1>
<h2>(like your use case)<h2>
The general syntax is
ALTER TABLE tablename REPLACE COLUMNS (col1 TYPE1,col2 TYPE2,… );
This completely removes the existing list of columns and replaces it with the new list.
Only the columns you specify in the ALTER TABLE statement will exist, and they will be in the order you provide.
Note
Again, this does not change the data files, only the metadata for the table,
so you'll either want the new list to match the data files or need to recreate the data files to match the new list.
I do not think you can add columns in between columns in Impala like above.
You can backup the data, drop the and recreate with new structure, and load the table from backup. Also if you have HIVE in your system you can try to do below steps -
Add column first and then use below commands to move columns around.
ALTER TABLE tab ADD COLUMNS (id BIGINT);
This moves id column to the beginning.
ALTER TABLE tab CHANGE COLUMN id id BIGINT first;
This moves existing_col after id.
ALTER TABLE tab CHANGE COLUMN existing_col existing_col string AFTER id;
Please refresh/invalidate metadata after applying all DDLs.
You cannot add column in between. Best way is to archive the data in another table. Drop the impala old table and create a fresh table with new columns as per the desired location and then reinsert the data.

Oracle: selfcopying data from Oracle tables

Application has different versions. Each version has it's own set of values in each table. I need to provide functionality to copy data from one version to another. Problem :
By inserting data I am trying to insert Ids which has already been in use in this table. So, I need to change ids of components which I want to insert but I must save relationship between those components. How cat I do that?
Create a master table which has a surrogate key as your primary key. A numeric value of type NUMBER(9) works well. You can create a sequence and trigger to automatically insert this.
The rest of the table is the column of your current table plus a column to indicate which version the row is for.
For simplicity you may wish to create views on top of the table along the lines of
select * from master_table where version_id = ####;
To copy the data from one version to another this will work:
Insert into master_table seq_master_table.nextval, new version_id,.....
from master_table
where version_id = ####;

How can i add values to a existing table column

I have a table where i have a column called student id and in that column i want to insert like SN + userid.But i know how to create a new column and do this but i want to add this to an existing column whenever a user is inserted.
ALTER TABLE [dbo].[Profile_Master]
Add [new] as ('SN'+CONVERT([varchar] (10),[UserId],(0)))
In SQL Server you can use triggers to do this:
http://msdn.microsoft.com/en-us/library/aa258254%28v=sql.80%29.aspx
Your solution is already compliant to your requirements: you are creating a computed column.
Thus, it will work for existing records and for new ones created in the future.

SELECT INTO the same table

I have a requirement where I want to rename a column. In in this case, it should happen like:
1.) I need to create a new column with the existing column structure.
2.) Migrate the data to the new column.
3.) Retain the old column.
I don't have the new column structure and I have to take it from the old column.
TIA.
You don't need to select into the same table. Perform the following steps:
Create the new column as nullable.
Use an update statement to fill the data into the new column based on the values from the old column.
Add a non-nullable constraint, if appropriate.
Update all queries and code to use the values in the new column instead of the old column.
Remove the old column.
What version of Oracle? You can rename the column since 9i R2:
ALTER TABLE your_table
RENAME COLUMN old_name to new_name;
If you're set on recreating the table so you can use SELECT INTO, then you'd want to use the following steps:
Drop constraints related to the table(s)
Drop indexes related to the table(s)
Lock the original table:
LOCK TABLE old_table IN EXCLUSIVE MODE;
Rename the table:
RENAME old_table TO old_table_orig
Create the new table with the correct/updated columns
Run your SELECT INTO script(s) to populate the new table, handling data type changes if there are any
Add indexes
Add constraints
Drop the old_table_orig

Add a column to a DB2/400 table with a specific ordinal position

Is there an SQL command on the AS400/iSeries/System-i/whatever to add a column to a table in a specific ordinal position, or moving an existing column to a different position?
IBM i 7.1 now allows you to add a column in front of another.
ALTER TABLE table ADD COLUMN colname ... BEFORE othercolumn
No. The ALTER TABLE statement will allow you add a column to a table, but, according to the documentation:
The new column is the last column of the table; that is, if initially there are n columns, the added column is column n+1.
If you'd like to change the order of columns in your table, your best bet is to:
Use the RENAME statement to rename the table.
Recreate the table, with its original name, with the columns in the order that you want.
Use an INSERT SELECT to populate the new table with the data from the renamed table.
When you're sure the data is intact, you can drop the renamed version of the table.