Good afternoon in my timezone.
i have to insert a row in a table but one of the columns are values from another table.So what i want to accomplish is something like this
INSERT TABLE_NAME(COL1,COL2,COL3,COL4) VALUES("VAL1","VAL2","VAL3",(SELECT COL_A FROM TABLE2 WHERE COL_B = 'X'))
But i think the above code is not possible so i use the following code:
INSERT INTO TABLE_NAME(COL1,COL2,COL3,COL4)
SELECT "COL1","COL2","COL3", COL_A FROM TABLE2 T2
WHERE COL_B= "X"
My question is:
I want to insert values even the select does not return values and in this case the COL4 will be NULL
How can i achieve this ?
Thanks in advance
Best regards
No, you cannot insert a row that is not in the table.
If you are expecting one row (in the case of a match), you can use aggregation:
INSERT INTO TABLE_NAME(COL1, COL2, COL3, COL4)
SELECT "COL1","COL2","COL3", MAX(COL_A)
FROM TABLE2 T2
WHERE COL_B = 'X';
This will return NULL if there is no match -- but only one row even if there are multiple matches in the table.
As i understood from your description, only one column is NULL, other 3 columns have values. You should use Select INTO as https://www.w3schools.com/sql/sql_select_into.asp
SELECT COL1,COL2,COL3,COL4 INTO TABLE2 FROM TABLE_NAME WHERE COL_B= "X"
You can create a temp table to store the primary keys of TABLE2 (for eg TABLE2 has primary key 'X', 'Y')
CREATE TABLE #TempPK (COLB int null);
insert into #TempPK(COLB) values ('X');
insert into #TempPK(COLB) values ('Y');
--then you do a FULL OUTER JOIN on your insert select from statement
INSERT INTO TABLE_NAME(COL1, COL2, COL3, COL4)
SELECT "COL1","COL2","COL3", MAX(COL_A)
FROM TABLE2 T2 FULL OUTER JOIN #TempPK TPK
ON T2.COL_B = TPK.COLB
This way you should be able to insert both rows(X and Y) and the Row X should show NULL values all across the row. Hope it makes sense.
Related
So I have 2 tables and I want to UPDATE Table 1 so that it is the Union of Table 1 and Table 2. Any suggestions?
It looks like you just want to insert rows of table2 into table1. If so:
insert into table1 (col1, col2)
select col1, col2 from table2
You might be looking to a more subtle logic, like,: insert names that do not exist, and update values on name that exist already. If so, I would recommend on conflict. For this to work, you need a unique constraint on table2(col1), and then:
insert into table1 (col1, col2)
select col1, col2 from table2
on conflict (col1) do update set col2 = excluded.col2
I have a table with a column that contains some values. I want to replace all the existing values with random values from another table (but only the existing ones - so WHERE COL1 IS NOT NULL). There is no way to correlate the two tables. Each of the random values needs to be different (well... unless they are randomly the same, in which case it's fine).
For example:
CREATE TABLE T1 (ID NUMBER(11,0), COL1 VARCHAR2(20));
CREATE TABLE T2 (COL2 VARCHAR2(20));
INSERT INTO T1 VALUES (1, NULL);
INSERT INTO T1 VALUES (54, NULL);
INSERT INTO T1 VALUES (941, 'Some text');
INSERT INTO T1 VALUES (251, NULL);
INSERT INTO T1 VALUES (352, 'Some other text');
INSERT INTO T1 VALUES (354, NULL);
INSERT INTO T2 VALUES ('Val1');
INSERT INTO T2 VALUES ('Val2');
INSERT INTO T2 VALUES ('Val3');
INSERT INTO T2 VALUES ('Val4');
INSERT INTO T2 VALUES ('Val5');
INSERT INTO T2 VALUES ('Val6');
INSERT INTO T2 VALUES ('Val7');
I have tried a few things and have searched this site for answers. The answers I've found seem to require that there is some correlation between the two tables. A lot of the examples are for SQL Server. I've tried a few out anyway, but I can't seem to get a MERGE or CROSS APPLY approach to work (I appreciate this is most likely my failure...).
The only solution I have that actually works at the moment is the following:
BEGIN
FOR X IN (
SELECT ID FROM T1 WHERE COL1 IS NOT NULL
)
LOOP
UPDATE T1 SET COL1 = (
SELECT COL2 FROM (
SELECT COL2 FROM T2 ORDER BY SYS_GUID()
) WHERE ROWNUM = 1
)
WHERE T1.ID = X.ID;
END LOOP;
END;
/
This produces a (desired) result of:
SELECT * FROM T1;
ID COL1
---------------------------
1
54
941 Val3
251
352 Val7
354
(COL1 values are both random each time I run the loop).
... But I know there must be a set-based way to achieve this... right?
You could do it with a MERGE statement.
merge into t1
using ( select a1.id
, a2.col2
from ( select id
, row_number() over (order by dbms_random.value) rn
from t1
where col1 is not null ) a1
join ( select col2
, row_number() over (order by dbms_random.value) rn
from t2) a2
on a1.rn = a2.rn
) q
on ( q.id = t1.id)
when matched then
update set t1.col1 = q.col2
/
The USING query is a little unorthodox. There are two subqueries, one for each table, which generate analytic row_number() in a random order (this is tidier than using rownum. The two subqueries are joined on the random row numbers which gives a random combination of T1.ID and T2.COL_2. After that, it's a straightforward MERGE.
You can create a FUNCTION named GET_RANDOM_VALUE
create or replace FUNCTION GET_RANDOM_VALUE
RETURN VARCHAR2 AS
sValue VARCHAR2(100) := '?';
BEGIN
select col2 INTO sValue
from t2
order by dbms_random.value
fetch first 1 rows only;
RETURN sValue;
END GET_RANDOM_VALUE;
and the UPDATE command can be
UPDATE t1
SET col1 = GET_RANDOM_VALUE()
WHERE col1 IS NOT NULL;
Normally, I avoid using SELECT in function, but in this case, I found this solution more readable and more easy to understand.
So for each distinct value in a column of one table I want to insert that unique value into a row of another table.
list = select distinct(id) from table0
for distinct_id in list
insert into table1 (id) values (distinct_id)
end
Any ideas as to how to go about this?
Whenever you think about doing something in a loop, step back, and think again. SQL is optimized to work with sets. You can do this using a set-based query without the need to loop:
INSERT dbo.table1(id) SELECT DISTINCT id FROM dbo.table0;
There are some edge cases where looping can make more sense, but as SQL Server matures and more functionality is added, those edge cases get narrower and narrower...
insert into table1 (id)
select distinct id from table0
The following statement works with me.
insert into table1(col1, col2) select distinct on (col1) col1 col2 from table0
The below query will also check the existing data in the Table2.
INSERT INTO Table2(Id) SELECT DISTINCT Id FROM Table1 WHERE Id NOT IN(SELECT Id FROM Table2);
Other Simple way to copy distinct data with multiple columns from one table to other
Insert into TBL2
Select * from (Select COL1, ROW_NUMBER() over(PARTITION BY COL1 Order By COL1) AS COL2 From TBL1)T
where T.COL2 = 1
I wanted to select the last record from the table1 and insert into another table .Here is my query.
Insert into table2 values(select top 1 col1,col2 from table1 order by id desc).
I know for adding the value into table,need to be in cotation.But where to add?
You can select literals to fill in the other columns that table1 can't provide, something like this:
insert into table2 (col_a, col_b, col_c, col_d)
select top 1 col1, col2, 'foo', 'bar'
from table1
order by id desc
Any columns you do not name in the column list will get the default value, or null if no default is defined.
The number and type of columns selected must match the number and type of columns in the insert column list.
In SQL, there are essentially basically two ways to INSERT data into a table: One is to insert it one row at a time, the other is to insert multiple rows at a time. Let's take a look at each of them individually:
INSERT INTO table_name (column1, column2, ...)
VALUES ('value1', 'value2', ...)
The second type of INSERT INTO allows us to insert multiple rows into a table. Unlike the previous example, where we insert a single row by specifying its values for all columns, we now use a SELECT statement to specify the data that we want to insert into the table. If you are thinking whether this means that you are using information from another table, you are correct. The syntax is as follows:
INSERT INTO table1 (column1, column2, ...)
SELECT t2.column3, t2.column4, ...
FROM table2 t2
So, in you case, you can do it like this:
Insert into table2
select top 1 t1.col1,t1.col2 from table1 t1 order by id desc
Or you can use your syntax like this:
declare #col1 type_of_col1, #col2 type_of_col2
select top 1 #col1 = t1.col1, #col2 = t1.col2 from table1 t1 order by id desc
Insert into table2 values(#col1, #col2)
Offcourse, this all works assuming that the column datatypes are matched.
I'm trying to insert data to a table from another table and the tables have only one column in common. The problem is, that the TABLE1 has columns that won't accept null values so I can't leave them empty and I can't get them from the TABLE2.
I have TABLE1:
id, col_1 (not null), col_2(not null), col_3 (not null)
and TABLE2:
id, col_a, col_b, col_c
so how could I insert id from TABLE2 to TABLE1 and fill the col_1-3 with hard coded strings like "data1", "data2", "data3"?
INSERT INTO TABLE1 (id) SELECT id FROM TABLE2 WHERE col_a = "something";
will result in:
ERROR: null value in column "col_1" violates not-null constraint
You can supply literal values in the SELECT:
INSERT INTO TABLE1 (id, col_1, col_2, col_3)
SELECT id, 'data1', 'data2', 'data3'
FROM TABLE2
WHERE col_a = 'something';
A select list can contain any value expression:
But the expressions in the select list do not have to reference any columns in the table expression of the FROM clause; they can be constant arithmetic expressions, for instance.
And a string literal is certainly a value expression.
For referential integtity :
insert into main_tbl (col1, ref1, ref2, createdby)
values ('col1_val',
(select ref1 from ref1_tbl where lookup_val = 'lookup1'),
(select ref2 from ref2_tbl where lookup_val = 'lookup2'),
'init-load'
);
Very late answer, but I think my answer is more straight forward for specific use cases where users want to simply insert (copy) data from table A into table B:
INSERT INTO table_b (col1, col2, col3, col4, col5, col6)
SELECT col1, 'str_val', int_val, col4, col5, col6
FROM table_a
You could use coalesce:
insert into destination select coalesce(field1,'somedata'),... from source;