I have two tables. My first table A contains
Tran_Particular | Dr_Tran_Amt | BeneficiaryName | PRNNo
columns.
My second table B contains
BeneficiaryName | Dr_Tran_Amt | PRNNo
columns.
I want to update table A.PRNNo but When I am updating this on BeneficiaryName and Dr_Tran_Amt that time it updates only first value. But In table B there are two PRNNO's are exist and In table A also two different Tran_Particular are exist. I want to update unique PRNNO wrt Tran_Particular, BeneficiaryName and Dr_Tran_Amt.
Query.
update A
set a.PRNNo = b.PRNNo
from A a
inner join B b
on a.Dr_Tran_Amt= b.Amount
and a.BeneficiaryName = b.BeneficiaryName;
HoW to update this in SQL server.
The code below should get you only the first matching row (the lowest PRNNo value):
UPDATE A
SET a.PRNNo = b.PRNNo
FROM A a
INNER join B b
ON a.Dr_Tran_Amt = b.Amount
AND a.BeneficiaryName = b.BeneficiaryName
AND b.PRNNo = (SELECT MIN(c.PRNNo) FROM B c WHERE c.BeneficiaryName = B.BeneficiaryName AND c.Dr_Tran_Amt = b.Dr_Tran_Amt);
Related
I have a table Product with 240 000 rows.
I want to select from this table data where idproduct = idproductcomponent
Output makes a table with 3 columns A12345678 35655455952625 9638520963258960, so in different column.
Always idproductcomponent of idproducttype 1 is 0, and idproductcomponent of idproducttype 2,3 are the same of idproduct of idproducttype 1.
Pelase, can you share with me any idea for this select ?
Assuming your database is SQL Server (you don't say which one it is) the query could look like:
select
a.name,
b.name,
c.name
from t a
left join t b on b.idproductcomponent = a.idproduct and b.idproducttype = 2
left join t c on c.idproductcomponent = a.idproduct and c.idproducttype = 3
where a.idproducttype = 1
and a.idproduct = 11163 -- parameter you are searching for
To increase the performance of this query you can add the index:
create index ix1 on t (idproductcomponent);
In my java code I have foreach loop which iterates though list
foreach(MyObject obj:list){
String status = obj.getStatus();
String is = obj.getId();
// DB call
1. To update Status in Table A
jdbcobj.updtastatus(status,id);
2. Get status from table B
String tableBStatu= jdbcobj.getstatufromtableB(status,id):
obj.setStatus(tableBStatus):
}
To avoid 2 dB calls in for loop I am using inner join and trying to achieve same output as above
I am using inner-join and get the new result set based on common field.I want to update the result set but I am unable to figure out how?
I have two tables "A" and "B".
Table "A" has columns id,name,statusA
Table "B" has columns id,city,statusB
As stated at start, I am using inner-join and my query looks like this.
Select A.id A.statusA,B.statusB FROM A INNER JOIN ON B where A.id=B.id
Which gives me result as "id", status from table "A" and status from table "B".
Now i want use the inner-join result, to update statusA column from table "A" and set value ="DONE"
And want to use the statusB column value in java object.
String statusfromColumnB = get statusB col value
and set in my java object like this
myObj.setStatus(statusfromColumnB)
Sample Data
Suggest a solution.
If I understand you correctly, an Oracle MERGE query could properly respond to your need :
Consider :
MERGE INTO A
USING B ON (A.id = B.id)
WHEN MATCHED THEN UPDATE SET A.statusA = B.statusB
This query will update the status in table A from that of the corresponding record in table B.
Oracle merge is a vendor-specific statement that is optimized for multi-rows upserts (inserts/updates).
Demo on DB Fiddle :
Select A.id, A.statusA, B.statusB FROM A INNER JOIN B ON A.id=B.id
ID | STATUSA | STATUSB
-: | :------ | :--------
1 | Pending | Initiated
2 | Pending | Completed
MERGE INTO A
USING B ON (A.id = B.id)
WHEN MATCHED THEN UPDATE SET A.statusA = B.statusB
2 rows affected
Select A.id, A.statusA, B.statusB FROM A INNER JOIN B ON A.id=B.id
ID | STATUSA | STATUSB
-: | :-------- | :--------
1 | Initiated | Initiated
2 | Completed | Completed
If you want to set statusA to a fixed value instead, then you could go :
MERGE INTO A
USING B ON (A.id = B.id)
WHEN MATCHED THEN UPDATE SET A.statusA = 'Finished'
Do you want something like this?
update a
set (status, somewhereelsecolumn) =
(select 'DONE', <whatever>
from b
where A.id = B.id
)
where exists (select 1 from b where a.id = b.id);
I have a pretty big MSSQL stored procedure that I need to conditionally check for certain IDs:
Select SomeColumns
From BigTable b
Join LotsOfTables l on b.LongStringField = l.LongStringField
Where b.SomeID in (1,2,3,4,5)
I wanted to conditionally check the SomeID field, so I did the following:
if #enteredText = 'This'
INSERT INTO #AwesomeIDs
VALUES(1),(2),(3)
if #enteredText = 'That'
INSERT INTO #AwesomeIDs
VALUES(4),(5)
Select SomeColumns
From BigTable b
Join LotsOfTables l on b.LongStringField = l.LongStringField
Where b.SomeID in (Select ID from #AwesomeIDs)
Nothing else has changed, yet I can't even get the latter query to grab 5 records. The top query returns 5000 records in less than 3 seconds. Why is selecting from a table variable so much drastically slower?
Two other possible options you can consider
Option 1
Select SomeColumns
From BigTable b
Join LotsOfTables l on b.LongStringField = l.LongStringField
Where
( b.SomeID IN (1,2,3) AND #enteredText = 'This')
OR
( b.SomeID IN (4,5) AND #enteredText = 'That')
Option 2
Select SomeColumns
From BigTable b
Join LotsOfTables l on b.LongStringField = l.LongStringField
Where EXISTS (Select 1
from #AwesomeIDs
WHERE b.SomeID = ID)
Mind you for Table variables , SQL Server always assumes there is only ONE row in the table (except sql 2014 , assumption is 100 rows) and it can affect the estimated and actual plans. But 1 row against 3 not really a deal breaker.
I need to make an update in a table based on other information.
How to proceed?
My case is:
I have a column iniciantes table MEMB_INFO
I need to make an update MEMB_INFO SET iniciantes = 0
But I need to make a query on WHERE table column resets the character table
Example:
UPDATE MEMB_INFO
SET iniciantes = 0
FROM MEMB_INFO CROSS JOIN
Character
WHERE (Character.Resets >= 100)
necessary that the update the memb_info only occur in cases where the reference character is greater than or equal to 100
Simply, join MEMB_INFO with your Character table and specify their relations:
update m
set iniciantes = 0
from MEMB_INFO m
inner join Character c on
c.Resets >= 100
AND m.CharacterId = c.CharacterId --Specify your tables' relations.
you have to specify columns on which you're joining this two tables. If you have characterId column in both tables, your query will be:
update MEMB_INFO set
iniciantes = 0
from MEMB_INFO as m
where
m.CharacterId in (
select c.CharacterId from Character as c where c.Reset >= 100
)
Evening all,
Actually, it's night. About 11pm. My brain is shutting down and I need a bit of help so I can finish and go home :)
I have two tables - table a and table b.
I need to update a field in table a with the value from a field in table b when two other fields match. The tables don't have a unique id for each record :(
Basically, I want to do this:
update a
set importantField =
(select b.importantfield
from b
where a.matchfield = b.matchfield
and a.matchfield2 = b.matchfield2
)
where a.matchfield = b.matchfield
and a.matchfield2 = b.matchfield2
Or at least... I think that's what I want to do...
Can someone help me out, please?
You can do this via a join in the update:
Update a
Set a.importantField = b.importantField
From a Join b
On a.matchfield = b.matchfield
And a.matchfield2 = b.matchfield2
Use:
UPDATE TABLE_A
SET importantField = (SELECT b.importantfield
FROM TABLE_B b
WHERE b.matchfield = matchfield
AND b.matchfield2 = matchfield2)
SQL Server doesn't support table aliases on the table being updated, but the above is a correlated query - those fields without the table alias b attached will serve values from TABLE_A because it doesn't have an alias.
The only issue beyond that is if there are multiple b.importantfield values for records with the matching records to TABLE_A. Use:
UPDATE TABLE_A
SET importantField = (SELECT TOP 1
b.importantfield
FROM TABLE_B b
WHERE b.matchfield = matchfield
AND b.matchfield2 = matchfield2)
..but you should use an ORDER BY as well or you'll get any random b.importantfield value.