This question already has answers here:
Upserting in MS-access
(6 answers)
Closed 6 years ago.
I know this question must have been asked a million times but I have yet to find a "NON-VBA" solution to this in MS Access 2007 SQL.
I am using 2 Tables TBLDestination and TBLSource. I have to update the destination table records from source table records when a matching ID is found. For a non-matching ID (i.e. new ID) I want to insert the new record
(Please refer the tables below).
-----TBLSource-------
ID (match if ID exists in Destination table)
EmpName
EmpAdd
---TBLDestination-----
ID
EmpName (to be updated/inserted)
EmpAdd (to be updated/inserted)
Salary
Access DOES have the equivalent using an UPDATE with a LEFT JOIN. See here.
MS Access doesn't have an equivalent to UPSERT where you can do both in a single query.
However, you can get the same effect by doing an UPDATE query where you join to the table you want to update from. Then you do an INSERT query where you OUTER JOIN to the table and return only those where it doesn't exist.
Related
This question already has an answer here:
What I am doing wrong in this correlation query in oracle SqlDevelopper
(1 answer)
Closed 9 months ago.
I am trying to run a SQL query that looks like below.
For my purpose I need to query the same price list twice, but for different articles. To do this I have joined the same table twice but using two aliases. Then selected the same column but with the prefix of the alias. There are also nested queries at the bottom to the same table to get the correct date from said list.
The query worked as intended until I tried to add the second table of the price list, using aliases "MC1" and "MC2". Now I get SQL error 203 "Column %1 in more than one table".
The code passes all the syntax checks I made so I am at a loss currently.
Any help is appreciated.
(I was not allowed to post a picture directly in the threat so it is a link)
SQL Query Code snapshot
You need to prefix your columns that you are selecting with the table alias.
As an example, if I had this table
CREATE TABLE test_table
(
someid NUMBER,
someval VARCHAR2 (20)
);
I could query all of the columns like this
SELECT t1.someid,
t1.someval,
t2.someid,
t2.someval
FROM test_table t1 JOIN test_table t2 ON (t1.someid = t2.someid);
This question already has answers here:
How to update two tables in one statement in SQL Server 2005?
(10 answers)
Closed 2 years ago.
I have a problem. I have a 2 table which is table orders and table delivery. table order has column order_recieved. by default NULL. and table delivery has column delivery_status with value order shipped. now, if customer click a button to update order_received, the column will change NULL value to Recieved value. and my problem is how column delivery_status will also change to order shipped value to complete value? the PK table orders is order_no and FK table delivery is orderFK.
Can someone help me how to update the value?
You can only update one table per update statement. I'd recommend sticking them both in the same stored procedure so that you can make one database call and have both values updated.
This question already has answers here:
Query to list number of records in each table in a database
(23 answers)
Closed 7 years ago.
I'm trying to add a WHERE clause to a SELECT statement that checks to see if there is data in the table or if it was a 0-row table.
Basically, I'm trying to get this to work but obviously tables.NAME isn't a valid object.
SELECT NAME
FROM sys.tables
WHERE (SELECT Count(*)
FROM tables.NAME) <> 0
I don't want to have to create a temp table, declare a cursor and checking a value row by row, but I'm having a hard time thinking a bout how to do this otherwise.
Using a cursor is a suitable solution to this problem. You can't dynamically interpret a column from one table as a table name and simultaneously select from that table to see if it's empty like you're attempting to do. In fact you can't even select from a dynamic table without using dynamic SQL. See the documentation for FROM. You can either iterate over each table and check each, or you can query system tables that hold storage statistics to see if tables are empty.
This question already has answers here:
Insert, on duplicate update in PostgreSQL?
(18 answers)
How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL?
(7 answers)
Closed 9 years ago.
i'm manually updating table in my DB using import functionality in PostgreSQL. Working with large number of data i came across an issue of duplicating primary keys. I am looking for a script to upload only values that do not violate primary key assumption, and those that to violate are to be ignored (not uploaded or updated).
I have already seen a code that would kind of do what i need however not quite sure if it will work for me.
Columns i am working with are:
acc_ph_id (primary_key);acc_ph_no;ph_type;ph_flag
Any suggestions will be highly appreciated as i am rather new to Postgresql in general.
Upload the table into a staging table with no constraints.
Then load the table into the full table eliminating duplicates:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table
order by acc_ph_id;
EDIT:
Oh, if the problem is the keys that already exist, then do:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table st
where not exists (select 1
from real_table rt
where rt.acc_ph_id = st.acc_ph_id
)
order by acc_ph_id;
This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I have a file SRQ which is having 10000 SRQ_ID which are unique.
I have one table(TABLE1) which is having 2 columns namely SRQ_ID,WORK_ID .
I needs to write a query which will search the table(TABLE1) for all the SRQ_ID's in the file SRQ and will display the output with corresponding WORK_ID.
I tried the below code. But IN clause is only applicable for 1000 records. How to run the same if I have 100k records?
select WO_ID
from TABLE1
where SRQ_ID in ('B6512DF0','5838FABC','EC5D804C','074DD65C')
Is there a reason you can't just do a join between the tables using SRQ_ID?
select wo_id from table1 join srq using srq_id
This will give you the work id for all rows that have a srq_id value in the srq table.
If the file s on the database server then you could access it as an external table, and join the two. Otherwise, I'd suggest bulk loading the codes into a global temporary table and performing the join against that.
In case you don't like to create a temporary table, you can use a nested table:
CREATE OR REPLACE TYPE VARCHAR_TABLE_TYPE AS TABLE OF VARCHAR2(20);
select WO_ID
from TABLE1
where SRQ_ID MEMBER OF VARCHAR_TABLE_TYPE('B6512DF0','5838FABC','EC5D804C','074DD65C');
But I don't know the limit for initializing a nested table. Oracle documentation says: "Because a nested table does not have a declared size, you can put as many elements in the constructor as necessary."