I created a table modeled on an existing table:
CREATE TABLE new_table AS
(SELECT * FROM old_table WHERE record_status = 'xyz' ) WITH DATA;
I then added a column:
ALTER TABLE new_table ADD new_column VARCHAR(100);
I now want to select more rows from the old table into the new table, but add a fixed value (eg. 'blabla') to the new column for each row selected. I tried a bunch of things, such as:
INSERT INTO new_table
SELECT *, 'blabla'
FROM old_table)
WHERE is_blabla = 'Y'
Tried every permutation I could think of but keep getting syntax errors. Any suggestions? I'm using Teradata.
Dave
INSERT INTO new_table
SELECT old_table.*,'your_fixed_value'
FROM old_table
WHERE is_blabla='Y'
You just need to alias your table
INSERT INTO new_table
SELECT a.*,'your_fixed_value'
FROM old_table a
WHERE is_blabla='Y'
Related
I am curious as to how I can go about copying data from four columns in an old table and placing the data into two columns in a new table. I know that I can use the following for two old columns into one new one. But how do I do it for many columns?
INSERT INTO new_table(new_column_xy)
(
SELECT old_column_x
FROM old_table
UNION ALL
SELECT old_column_y
FROM old_table
)
Below is what I am trying to accomplish.
Just add the other column in your select:
INSERT INTO new_table(employee, employee_id)
SELECT pilot, pilot_id
FROM old_table
UNION ALL
SELECT copilot, copilot_id
FROM old_table
insert into new_table (employee, employee_id) (
select pilot as employee
, pilot_id as employee_id
from old_table
union
select copilot as employee
, copilot_id as employee_id
from old_table
)
I have a table that contains data and I want to use insert into statement
here is my query
INSERT INTO [table]
SELECT Distinct * FROM [DataSource]
how to to ignore a row from being inserted if it's already found in the table?
Assuming your target table and your data source table have identical structure, you could do something like this:
INSERT INTO [table]
SELECT Distinct *
FROM [DataSource]
EXCEPT
SELECT *
FROM [table]
Alternatively, you could use MERGE statement or SELECT ... WHERE NOT EXISTS.
I know we could use
Insert Into table_name(cloumn_names)
Select column_names
From table_name
but how about Select Into? Can we also use Select Into to copy data from one table to another EXISTING table using? I mean can we specify the column names of the latter table using Select Into?
Thx!
You can use insert into select syntax
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
It basically selects columns from table1 and respectively insert in table 2
SELECT INTO
This method is used when table is not created earlier and needs to be created when data from one table is to be inserted into newly created table from another table. New table is created with same data types as selected columns.
Inserting Rows by Using SELECT INTO
You can try this :
UPDATE [TestDB].[dbo].[Table1]
SET [Content] = (
Select [TestDB].[dbo].[Table2].[Content] from Table2 where ID= 'XYZ')
WHERE [ID] = 'XYZ'
GO
I have a source table that is NewID|Fruit|Apples and I need to insert those rows into target table OldID|Fruit|Apples
Is there a way to select into a new table while changing columns?
Thanks!
Try:
CREATE TABLE new_table SELECT NewID as OldID, Fruit, Apples FROM old_table
Assuming target table already exists and you want to copy all the columns
INSERT INTO target_table(OldID,Fruit,Apples)
SELECT NewID,Fruit,Apples FROM source_table
I would like to do something like this:
create table NEW_TABLE (select * from OLD_TABLE where id=3582)
And have a new table created from the resulting columns of that select statement.
I bet its possible in mySQL, but how to do it in Oracle?
Try this:
CREATE TABLE new_table AS select * from OLD_TABLE where id=3582