Encountered " "MERGE" "MERGE "" at line 1, column 1. Was expecting: <EOF> - google-bigquery

We must need to use Legacy SQL in BigQuery. But, Merge is not working in Legacy SQL. How we write below query in Legacy SQL?
MERGE [ABC:xyz.tmp_cards] AS target_tbl
USING [ABC:xyz.tmp_cards_1533188902] AS source_tbl
ON target_tbl.id = source_tbl.id
WHEN MATCHED AND target_tbl.id = source_tbl.id THEN
UPDATE SET target_tbl.id = source_tbl.id,
target_tbl.user_id = source_tbl.user_id,
target_tbl.expiration_date = source_tbl.expiration_date,
target_tbl.created_at = source_tbl.created_at,
target_tbl.updated_at = source_tbl.updated_at
WHEN NOT MATCHED THEN
INSERT (id, user_id, expiration_date, created_at, updated_at)
VALUES (source_tbl.id, source_tbl.user_id, source_tbl.expiration_date, source_tbl.created_at, source_tbl.updated_at)

Support for DML MERGE statements appeared in Beta just this year for standard SQL. It's not possible to do it in Legacy SQL and this is why Standard SQL is the preferred SQL dialect for querying data stored in BigQuery. Because the new features are for the last DML for BigQuery and not the old one.

Related

Convert BigQuery's legacy SQL to standard SQL

I want to convert below code from legacy to standard SQL. The code is in Legacy SQL (Big Query). Here is the code:
MAX(IF((hi.Info.Action='complete'),1, 0)) WITHIN RECORD AS transact
what will be the equivalent for standard SQL?
#legacySQL
SELECT
MAX(IF((hi.Info.Action='complete'),1, 0)) WITHIN RECORD AS transact
FROM table
will be
#standardSQL
SELECT
(SELECT MAX(IF(Info.Action='complete',1, 0)) FROM UNNEST(hi)) AS transact
FROM table
Note: I assumed that hi is REPEATED RECORD and info is RECORD

hive doesn't support merge function

trying to update the value from table to another table, both of these tables have the same field name but different values, the query must be work fine on any normal DB but here it returns
Error while compiling statement: FAILED: ParseException line 1:0
cannot recognize input near 'MERGE' 'INTO' 'FINAL'
MERGE
INTO FINAL
USING FIRST_STAGE
ON IMSI = FIRST_STAGE.IMSI and Site = FIRST_STAGE.Site
WHEN MATCHED THEN UPDATE SET
Min_Date = least(FIRST_STAGE.Min_Date, Min_Date),
Max_Date = greatest(FIRST_STAGE.Max_Date, Max_Date),
NoofDays = FIRST_STAGE.NoofDays + NoofDays,
Down_Link = FIRST_STAGE.Down_Link + Down_Link,
up_Link = FIRST_STAGE.up_Link + up_Link,
connection = FIRST_STAGE.connection + connection
WHEN NOT MATCHED THEN INSERT ( Min_Date,
Max_Date,
NoofDays,
IMSI,
Site,
Down_Link,
Up_Link,
Connection )
VALUES ( FIRST_STAGE.Min_Date,
FIRST_STAGE.Max_Date,
FIRST_STAGE.NoofDays,
FIRST_STAGE.IMSI,
FIRST_STAGE.Site,
FIRST_STAGE.Down_Link,
FIRST_STAGE.Up_Link,
FIRST_STAGE.Connection )
Hive merge statement is introduced in Hortonworks distribution.
Prerequisite for these merge statement to run is:
Final table needs to be created with transactional enabled ,ORC format ,and bucketed.
AFAIK In case of Cloudera distribution we need to use Kudu to perform upsert operations starting from cloudera-5.10+.
Note: Upsert statement only works for Impala tables that use the Kudu storage engine.
I don't think we can run merge statements as mentioned in the post in CDH distributions as of now.

Update multiple values in an oracle table using values from an APEX collection

I am using APEX collections to store some values and pass them between pages in Oracle Application Express 4.2.3.
I would like to then perform an update statement on a table called "project" with the values from the collection.
My code so far is as follows:
update project
SET name=c.c002,
description=c.c007,
start_date=c.c004,
timeframe=c.c005,
status=c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
and id = :p14_id;
where :p14_id is the value of a page item.
However, I am getting the following error:
ORA-00933: SQL command not properly ended
Anyone have any idea on how to approach this?
Thanks!
The UPDATE syntax you are using is not valid in Oracle; it does not allow you to use FROM in the way you are attempting.
The simplest way to do this in Oracle would with a subquery:
update project
set (name, description, start_date, timeframe, status) =
(select c.c002, c.c007, c.c004, c.c005, c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
)
WHERE
id = :p14_id
;
Note that if the subquery returns no rows, the columns in the target table will be updated to NULL; this could be avoided by adding a similar EXISTS condition in the predicate for the update. It could also be avoided by using a MERGE statement instead of an UPDATE.
If the subquery returns multiple rows, the statement will throw an error. It looks like tthat should not be the case here.

how do i write a single query to insert the data

iam trying insert rows into a sql table in java. i need to insert into a table simulateneously with 2 different data.here am using 2 queries to insert data to dbo.Company obtained from some manipulations into a single table
statement1.executeUpdate("insert into
dbo.Company(CName,DateTimeCreated,DateTimeLastModified)
values('"+cname[i]+"','"+ts+"','"+ts+"')");
statement3.executeUpdate("insert into
dbo.Company(CName,DateTimeCreated,DateTimeLastModified)
values('"+EMpname[i]+"' ,'"+ts+"','"+ts+"')");
i need to insert cname[i] and EMpname[i] into dbo.Company using single query... please help to write this query.
Multiple VALUES supported since 2008. If your server version is lower use UNION: http://blog.sqlauthority.com/2007/06/08/sql-server-insert-multiple-records-using-one-insert-statement-use-of-union-all/
String query = "insert into
dbo.Company(CName,DateTimeCreated,DateTimeLastModified)
values('"+cname[i]+"','"+ts+"','"+ts+"');" + "insert into
dbo.Company(CName,DateTimeCreated,DateTimeLastModified)
values('"+EMpname[i]+"' ,'"+ts+"','"+ts+"')";
statement1.executeUpdate(query);
Use insert statement in form:
INSERT Table(fields)
VALUES
(set1),
(set2),
...,
(setN)
Your case is:
statement1.executeUpdate("insert into
dbo.Company(CName,DateTimeCreated,DateTimeLastModified)
values('"+cname[i]+"','"+ts+"','"+ts+"'),
('"+EMpname[i]+"' ,'"+ts+"','"+ts+"')");
I would recommend using a transaction.
Transaction tx = session.beginTransaction();
statement1.executeUpdate("insert into
dbo.Company(CName,DateTimeCreated,DateTimeLastModified)
values('"+cname[i]+"','"+ts+"','"+ts+"')");
statement3.executeUpdate("insert into
dbo.Company(CName,DateTimeCreated,DateTimeLastModified)
values('"+EMpname[i]+"' ,'"+ts+"','"+ts+"')");
tx.commit();
This ensures that either all statements are successfully executed or none of them (rollback).
I would also consider using parametrized queries to avoid SQL injections: https://www.owasp.org/index.php/Preventing_SQL_Injection_in_Java#Prepared_Statements

Oracle and Sybase compatibility for create table new_table as

I am trying to write an SQL query which needs to be compatible on both a Sybase and Oracle database. The query looks like the following :
SELECT *
INTO new_table
FROM other_table
This query is working great on a Sybase database but not on an Oracle one. I found the equivalent for Oracle :
CREATE table new_table AS
SELECT *
FROM other_table
Is there a way to write a third query that would do the same and that can be executed on a Sybase and on an Oracle database?
As you found, Oracle supports INTO but doesn't use it like Sybase/SQL Server do. Likewise, Sybase doesn't support Oracle's extension of the CREATE TABLE syntax.
The most reliable means of creating a table & importing data between the systems is to use two statements:
CREATE TABLE new_table (
...columns...
)
INSERT INTO new_table
SELECT *
FROM OLD_TABLE
Even then, syntax is different because Oracle requires each statement to be delimited by a semi-colon when TSQL doesn't.
Creating a table & importing all the data from another table is a red flag to me - This is not something you'd do in a stored procedure for a production system. TSQL and PLSQL are very different, so I'd expect separate scripts for DML changes.
There is no query that would do what you want. These environments are very different.
It is possible.
SELECT * INTO new_table FROM existing_table;
Thanks