I need to write a query in Oracle, but I'm more familiar with SQL Server.
In SQL Server, the query would look as follows: (simplified)
if exists (
select * from table where a=1
)
begin
update table set b=1 where a=1
end else
begin
insert table (a,b) values(1,1)
end
Thanks for any help :)
===============================================================================
This is the Merge option, (I think):
MERGE INTO table T
USING (
SELECT a,b
FROM table
) Q
ON T.a = Q.a
WHEN MATCHED THEN
UPDATE SET T.a = 1
WHEN NOT MATCHED THEN
INSERT table (a,b) VALUES (1,1);
Is this correct?
This should be the correct syntax for Oracle 11g. I'm not an expert on Oracle so maybe someone else could explain it better, but I believe the dual table is used when inserting new values instead or trying too merge is from another table.
MERGE INTO table1 T
USING (
SELECT 1 a, 1 b FROM dual
) Q
ON (T.a = Q.a)
WHEN MATCHED THEN
UPDATE SET b = 1
WHEN NOT MATCHED THEN
INSERT (a,b) VALUES (Q.a,Q.b);
Working example
Related
This question already has answers here:
sql server 2008 management studio not checking the syntax of my query
(2 answers)
Closed 4 years ago.
I have the following query which gives no error when I used a non-existent column reference in the subquery. The column which I referred in the subquery is actually a column in the table being updated.
create table tbl1 (f1 bigint, f2 char(10), f3 integer);
insert into tbl1 values (1, 'aa', 0);
insert into tbl1 values (2, 'bb', 0);
insert into tbl1 values (3, 'cc', 0);
insert into tbl1 values (4, 'dd', 0);
create table temp_tbl (ref_num bigint);
insert into temp_tbl values (1);
insert into temp_tbl values (3);
update tbl1 set f2='ok' where f1 in (select f1 from temp_tbl);
-- 4 records updated
can anyone tell me why it is not giving any error? and records are updated irrespective of the condition.
I tried this in both Oracle and SQLserver. results are same
The sub-query's column reference goes to the outer table!
update tbl1 set f2='ok' where f1 in (select f1 from temp_tbl);
Is read as
update tbl1 set f2='ok' where f1 in (select tbl1.f1 from temp_tbl);
Qualify your columns:
update tbl1 set f2='ok' where f1 in (select temp_tbl.ref_num from temp_tbl);
This is happening because the values in a SELECT don't just have to be columns from the table you're selecting from, the sub-query is returning the value for f1 from the outer query, instead of a value from temp_tbl.
Consider if you re-wrote the UPDATE query to:
SELECT *
FROM tbl1
WHERE f1 IN (select f1 from temp_tbl);
The results returned would actually be:
When you're trying to reason about something like this (and as a generally good way of working to get queries right!), it's useful to write your UPDATE query in the form:
UPDATE T
SET F2 = 'ok'
FROM TBL1 T
WHERE T.f1 IN
(
SELECT F1
FROM temp_tbl
)
By writing it this way you can readily comment out the UPDATE and SET components of the query, replace them with a SELECT and see what the set of data the query will operate on is.
I need to execute insertions for around 10 tables, before inserting I have to check for a condition, condition remains the same for each of the tables, instead of giving that condition within insert query, I wish I could give in if condition (a select query), if satisfied then execute insert statements, is there a way to give if condition in Vertica SQL file ? If condition is not satisfied I dont want to execute any of the insert queries.
If the condition is, for example, that you only insert the data on a Sunday, try this:
a) a test table:
CREATE LOCAL TEMPORARY TABLE input(id,name)
ON COMMIT PRESERVE ROWS AS
SELECT 42,'Arthur Dent'
UNION ALL SELECT 43,'Ford Prefect'
UNION ALL SELECT 44,'Tricia McMillan'
KSAFE 0;
From this table, select with a WHERE condition that tests whether it's sunday -- that's all, see here:
SELECT
*
FROM input
WHERE TRIM(TO_CHAR(CURRENT_DATE,'Day'))='Sunday'
;
id|name
42|Arthur Dent
43|Ford Prefect
44|Tricia McMillan
With a different value for the week day (I'm writing this on a Sunday...), you get this:
SELECT
*
FROM input
WHERE TRIM(TO_CHAR(CURRENT_DATE,'Day'))='Monday'
;
id|name
select succeeded; 0 rows fetched
I use this technique in SQL generating SQL to create a script or an empty file determining on circumstances, and then to call that script (full or empty), implementing a conditional SQL execution that way....
I know it is an old post, but i want to share what i recently solved my problem.
I need to insert in one table or another based on some condition. You first should have a field or value what will be your search condition.
create table tmp1 (
Col1 int null
,Col2 varchar(100) null
)
--Insert values
insert into tmp (Col1,Col2) Values
(1,'Text1')
,(2,'Text2')
--Insert into table001
insert into table001
select
t.field1
,t.field2
,......
from table1 t
inner join tmp t2
on t2.col1 = t.ColX
where 1 = case when t2.Col2 = 'Text1' then 1 else 0 end --Search condition; if 1<>0 then it doesn't do anything; otherwise insert.
--Insert into table002
insert into table002
select
t.field1
,t.field2
,......
from table2 t
inner join tmp t2
on t2.col1 = t.ColX
where 1 = case when t2.Col2 = 'Text2' then 1 else 0 end --Search condition; if 1<>0 then it doesn't do anything; otherwise insert.
Or you can use an UNION/UNION ALL based on this if it is the same working table.
Regards!
I'm using MERGE to insert records in a table but only if a column in the source satisfies a condition.
Is the following a valid MERGE statement:
MERGE INTO sometable AS [Target] USING
(
--Here SELECT and INNER JOINS and WHERE
) AS [Source] ON 1 = 0
WHEN NOT MATCHED AND [Source].somecolumn > 0 THEN
INSERT
--columns to insert
VALUES
--their values
EDIT
Modified the statement to show the change according to the docs.
I need to create a sql statement that does this work: let's say I have two tables A and B containing integer fields. What I have to achieve is:
if (!(C is contained into A)) insert C into B
I'm using SQLite. Thanks for help
Actually, in your specific case it may happen to be as easy as
insert into B (c_value) select c_value from A where c_value = #your_c_value_here
see INSERT statement
sorry I haven't noticed the negation in your question for the C in A condition
I have another option for you
with temp_val as (select #your_val_goes_here as val)
insert into b
select val from temp_val where not exists
(select 1 from a where c = val)
check out this fiddle
I am using SQL server management tool 2008 to import data to the web host database. I have tables with primary keys. (Id for each row) Now I can import data normally. But when I am importing data for the second time..I need to make sure only those rows that doesn't currently exists only then it's inserted. If there's a to do this using the wizard? If not, then what's the best practice?
Insert the data into a temp table
use left join with main table to identify which records to insert
--
CREATE TABLE T1(col1 int)
go
CREATE TABLE Temp(col1 int )
go
INSERT INTO T1
SELECT 1
UNION
SELECT 2
INSERT INTO TEMP
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
UNION
SELECT 4
INSERT INTO T1
SELECT TEMP.col1
FROM Temp
LEFT JOIN T1
ON TEMP.col1 = T1.col1
WHERE T1.col1 IS NULL
I've used this some time ago, maybe it can help:
insert into timestudio.dbo.seccoes (Seccao,Descricao,IdRegiao,IdEmpresa)
select distinct CCT_COD_CENTRO_CUSTO, CCT_DESIGNACAO, '9', '10' from rhxxi.dbo.RH_CCTT0
where CCT_COD_CENTRO_CUSTO not in (select Seccao from TimeStudio.dbo.Seccoes where idempresa = '10')
Or, just use simple a IF Statement.