Copy record to another table adding fields - sql

I have 2 tables:
tab1 (field1, field2, field3)
tab2 (field1, field2,field3, field4)
I want to copy a record from tab1 to tab2 taking all the fields and adding a value for field4.
How can I select field1, field2 and field3 from tab2 and also add a value? I know that SELECT and VALUES in a INSERT query are mutually exclusive.
Thanks in advance.
Gustavo.

I don't know Oracle, but in Ms SQL it works like this:
insert into tab2 (field1, field2, field3, field4)
select field1, field2, field3, 'New Value' from tab1

Related

insert Default value For a column while inserting data from other table

I am facing an issue when I am inserting data into table1 by selecting data from table2
like
INSERT INTO table1
SELECT * FROM table2
So there is a column in both table like field1 for which value is null in table2.
But in table I have set the default value for field1
after executing ,
INSERT INTO table1
SELECT * FROM table2
I am having null value for field1 insted of getting default value of it what i set.
You Can Specify the column names and then set the value in your Select Statement
INSERT INTO table1 (field1, field2, field3, ...)
SELECT field1,
CASE WHEN field2 IS NOT NULL THEN field2
ELSE 'Your_default_value' END ,
field3, ...
FROM table2
INSERT INTO table1 (field2, field3, ...)
SELECT field2, field3, ...
FROM table2
You just skip the column for which default value oyu want to use and it will be automatically populated with default.
Interesting, One way to achieve this is use of CASE EXPRESSION, Assuming that field2 is column which you wants to put default value
INSERT INTO TableName1 (field1, field2, field3, ...)
SELECT field1, CASE WHEN field2 IS NULL THEN 'DefaultValue' ELSE field2 END, field3, ...
FROM TableName2
Try using NVL
INSERT INTO TableName1 (field1, field2, field3, ...)
SELECT field1, NVL(field2,'Your Default Value'), field3, ...
FROM TableName2
For example:
INSERT INTO DESTINATION_TABLE(A, B, C, D, E, F)
SELECT W, X, Y, 'Some custom value', CASE WHEN Z IS NULL THEN 'Some other value' ELSE Z
FROM SOURCE_TABLE
WHERE ....
Any time you want to replace a null value, use COALESCE. COALESCE is available in both Oracle and SQL Server, it doesn't evaluate arguments unless needed (NVL always evaluates the second argument), and it is simpler than a case statement:
Insert into table1(field1, field2)
select field1, coalesce(field2, field3, field4, 'novalue') from table2;
One big difference between NVL and COALESCE, if the second argument has side effects, you could come up with different results.

How to create a SELECT query FROM "TABLE1 AND TABLE2"

I have a PostgreSQL database, with only SELECT permissions. In this DB there are two tables with the same structure (the same columns).
I need to write several query in each table and join the results.
There is a way for writing a query like this one?
SELECT
field1,
field2,
field3
FROM
table1
AND
table2
WHERE
condition;
Select from 2 tables. Query = table1 OR table1 + table2 have no answer and it is not my question.
UNION ALL
SELECT field1, field2, field3
FROM table1
WHERE condition
UNION ALL
SELECT field1, field2, field3
FROM table2
WHERE condition;
Or to simplify your WHERE condition
SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;
You can use Union:
SELECT
field1,
field2,
field3
FROM
table1
UNION
SELECT
field1,
field2,
field3
FROM
table2
SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;

SQL select only specific fields and insert them in another table + static values

I would like to insert data from specific fields from one table in another table + some static values. Roughly I would like to do this:
INSERT INTO TableA(Field1, Field2, Field3)
SELECT Field1, Field2, 'staticvalue', Field3
FROM TableB
WHERE TableB.Field6 = 'XYZ'
Any idea how can I mix both specific fields from one table and static values?
You're on the right track... you need to supply a column for the static value to be inserted into.
INSERT INTO TableA(Field1, Field2, someOtherField, Field3)
SELECT Field1, Field2, 'staticvalue', Field3
FROM TableB
WHERE TableB.Field6 = 'XYZ'

Trigger to copy few columns from table1 to table2 automatically when new data is imported into table1

I have a table1 with columns (date, field1, field2, field3, field4, field5, field6, field7, field8)
I have another table2 with columns (date, field1,someothercolumns, field3, field5, field7, unrelatedcolumns1, someothercolumns2)
Now every time when new data is imported into table2, I also want data from columns (date, field1, field3, field5, field7) from table2 to automatically be copied into table1 (date, field1, field3, field5, field7).
Missing columns in table1 can have null values
Is it possible using trigger or any stored procedures?
Try something like this:
CREATE TRIGGER YourTriggerName
ON dbo.Table2
AFTER INSERT
AS BEGIN
INSERT INTO dbo.Table1(date, field1, field3, field5, field7)
SELECT
date, field1, field3, field5, field7
FROM
Inserted
END
In a trigger, you can access the "pseudo" tables Inserted (and Deleted for UPDATE and DELETE triggers) which contains all rows that have been affected by the statement that caused the trigger to fire.
This is important : if you have an INSERT statement that inserts 10 rows, your trigger will fire once and the pseudo table Inserted will have 10 rows in it. Do not ever assume that your trigger fires once per row that's being inserted - that is not the case.
So in this trigger here, whenever an INSERT into table2 has happened, the columns in question are extracted from the Inserted pseudo table and inserted into table1.
Does that solve your problem?

Generate field in MySQL SELECT

If I've got a table containing Field1 and Field2 can I generate a new field in the select statement? For example, a normal query would be:
SELECT Field1, Field2 FROM Table
And I want to also create Field3 and have that returned in the resultset... something along the lines of this would be ideal:
SELECT Field1, Field2, Field3 = 'Value' FROM Table
Is this possible at all?
SELECT Field1, Field2, 'Value' Field3 FROM Table
or for clarity
SELECT Field1, Field2, 'Value' AS Field3 FROM Table
Yes - it's very possible, in fact you almost had it!
Try:
SELECT Field1, Field2, 'Value' AS `Field3` FROM Table