sql update table from another table snappydata - sql

Hi I am using SnappyData's sql utility to update table my table from another table, say update Table_A with rows from Table_B.
Table_A(col_key, col_value) -- partitioned table with large number of rows
Table_B(col_key, col_value) -- small batch update in this table
Ideally a MERGE would be ideal (update if there is a match, or insert if the row with the key does not exists in Table_A)
But MERGE is not supported in SnappyData (or Gemfire), thus I am planning to insert first with an outer join to handle new col_key rows, then an update to update values in Table_A
where the same col_key also appears in Table_B.
However it seems that the "update ... set ... from ... " is also not supported in Gemfire
So is there a way to implement the "update .. set .. from .." in SnappyData sql statements? Thanks in advance :)

Yes, you can use PUT INTO when using SQL or you can do the same using the Snappy Spark extension APIs too.

I just found that GemFire actually use a "PUT INTO" statement to kind of support the "INSERT or UPDATE" (MERGE) function by other DBMSes.
Basically first retrieve the 'old' values from my TABLE_A where the col_key exists in , add them to TABLE_B, and use "PUT INTO" to put those rows in Table_B to Table_A and it's done!

Related

oracle sql multiple new rows merge in to a table

I need to insert new rows into an oracle(12c) SQL table only if they don't exists using java code
It is possible that there will be more then 100 rows to check and insert
ideally I would like to have one merge statement with multiple inserts
something like this:
MERGE INTO some_table t
USING(???)
ON(???)
WHEN MATCHED THEN UPDATE ???
WHEN NOT MATCHED THEN INSERT (t.id, t.val)
value("some_id","some_data")
...
...
...
but I have trouble with syntax and i can't find any examples for this case ether
I would like to know:
if this even possible
if yes, is it the correct way to go or there is a better solution
what should be the syntax(some example will be great)
if it not possible than what is the correct way.
thanks for help
Following OldProgrammer advice i will create temp table, insert there the new data and the will merge between the two tables

Insert overwrite in Hive

I am trying to use Insert overwrite in Hive. Basically I would like to insert overwrite not the complete partition but only a few records in the partition. I am not finding any solution to do it (Insert overwrite in destination table based on a filter on non partition column also).
Is there any way I can achieve it?
Hive is not as Regular RDBMS, If you want to update the record simple do INSERT OVERWRITE TABLE Table_Name...simple change your data in one temporary table or by using WITH clause simply insert overwrite..by using table partioning..it is safe.
QUERY[HIVE]:
WITH TEMP_TABLE AS (SELECT * FROM SOURCE_TABLE_NAME) INSERT OVERWRITE TABLE TARGET_TABLE_NAME SELECT * FROM TEMP_TABLE
Hive is not an RDBMS. What you are trying to achieve with Hive is not recommended. Hive is better suited for batch processing over very large sets of immutable data.
However, from what I could deduce, you are trying to update an existing record in your table. To do so, enable ACID support on the table that needs to be updated and your update queries will start working.
UPDATE <TABLE>
SET <COL1>='Value1',
SET <COL2>='Value2'
WHERE <Some Condition That Only Evaluates To The Rows You Need Updated>

Using OUTPUT with joined tables

Why doesn't the following work?
INSERT INTO dbo.Pending_Break
(Pending_Pod_ID, Break_Date_Time, Break_Length, Booked_Length)
OUTPUT INSERTED.Pending_BH_ID -- This is the inserted identity
, INSERTED.Pending_Pod_ID
, INSERTED.Break_Name
, INSERTED.Break_Date_Time
, pb.PENDING_BH_ID -- complains on this one
INTO #InsertedPendingBreaks
SELECT ippod.Pending_Pod_ID,
pb.Break_Date_Time
pb.break_length,
0
FROM PendingBreak pb
JOIN InsertedPod ippod ON ...
Can I not use anything other than Inserted or Deleted in the OUTPUT clause?
Can I not use anything other than Inserted or Deleted in the OUTPUT
clause?
No you can't. At least not with an insert. In SQL Server 2008 you can convert your insert to a merge statement instead and there you can use values from the source table in the output clause.
Have a look at this question how to do that in SQL Server 2008. Using merge..output to get mapping between source.id and target.id
The inserted and deleted tables are available only in DML triggers. I'm not sure if you just pulled a code snippet out of a trigger, but if that is a standalone batch then it won't work.
Also, there is no updated table. An update is a delete and then an insert for this. deleted contains the old data and inserted contains the new data on an UPDATE.

How to update multiple rows in the same table of MySQL with PHP?

If only one row with a distinct field is to be updated,I can use:
insert into tab(..) value(..) on duplicate key update ...
But now it's not the case,I need to update 4 rows inside the same table,which have its field "accountId" equal to $_SESSION['accountId'].
What I can get out of my mind at the moment is:
delete from tab where accountId = $_SESSION['accountId'],
then insert the new rows.
Which obviously is not the best solution.
Has someone a better idea about this?
Use the update just like that!
update tab set col1 = 'value' where accountId = $_SESSION['accountId']
Moreover, MySQL allows you to do an update with a join, if that makes your life a bit easier:
update
tab t
inner join accounts a on
t.accountid = a.accountid
set
t.col1 = 'value'
where
a.accountname = 'Tom'
Based on your question, it seems like you should review the Update Statement.
Insert is used to put new rows in - not update them. Delete is used to remove. And Update is used to modify existing rows. Using "Insert On Duplicate Key Update" is a hackish way to modify rows, and is poor form to use when you know the row is already there.
load all of the values in to a temporary table.
UPDATE all of the values using a JOIN.
INSERT all of the values from the temp table that don't exist in the target table.
You can use replace statement. This will work as a DELETE followed by INSERT

Using SQl Server CE; Possible to Insert Only If Not Exists and Delete if Exists?

I have a One Field Table in SQL CE which I need a SQL Statement for. The objective is to Delete the record if it already exists and insert the record if it does not exist. Is the possible with SQL CE?
INSERT INTO Source_Table
SELECT 'myvalue' AS Expr1
WHERE (NOT EXISTS
(SELECT Source_Data
FROM Source_Table AS Source_Table_1
WHERE (Source_Data = 'myvalue')))
Why not just...
DELETE Source_Table WHERE Source_Data = 'myvalue'
GO
INSERT INTO Source_Table (Source_Data) values('myvalue')
I'm not sure what the point of deleting a record and then inserting the same data would be, but this should accomplish it.
If the aim is to literally delete if it exists and only insert it if it does not exist, then you need to do what you've written.
If the aim is to insert if it doesn't exist and replace if it does, Adam's correct that you may as well just always run the delete followed by the insert. I'd run both in the same statement batch, to save overhead.
Were there more than one field, of course you'd do an update, not a delete followed by insert. (But delete followed by insert is how Sybase internally performs updates.)