How to Update Using Join (Linked Server) - sql

I want to match the data between 2 databases.
I have 2 Databases. Aa and Bb, and I want to compare Aa to Bb. Database Bb is in linked server
I have join code like this
SELECT
B.Employee_Name, B.Employee_NIP, B.DomainName, A.NAMA, A.NIP,
A.StatusEmployee, A.ActiveStatus
FROM
[SERVER-B].Bb.dbo.employee_hierar AS B RIGHT OUTER JOIN
Bb AS B ON B.NIP = A.Employee_NIP
and I want update A.StatusEmployee from Y to N if there is NULL data on B.Employee_Name and B.Employee_NIP
note:
SQLServer
Please Advice

You can use a join. Something like:
update a
set StatusEmployee = 'N'
from bb a LEFT JOIN
[SERVER-B].Bb.dbo.employee_hierar b
on B.NIP = A.Employee_NIP
where b.EmployeeName is null and b.Employee_NIP is null and
a.StatusEmployee = 'Y';

This is same to update multi tables in one database. Standard sql should be written like this:
UPDATE A
SET StatusEmployee = "N"
WHERE NOT EXISTS
( SELECT * FROM B WHERE B.NIP = A.Employee_NIP )

Related

How to find a value if we have 2 same columns in 2 different tables?

As you can see on the photo. I have 2 tables(A and B). KlantId is common in 2 tables. What I want to achieve is, if I provide the Email then I can get the reservatieNummer.
select b.reservatieID
from tablea as a
inner join tableb as b on a.KlantId = b.KlantId
where a.email="whatever"
and you specify the email in the where condition
To use linq:
var ReservatieID = tableA.Join(tableB, ta =>ta.KlantId, tb =>tb.KlantId, (ta,tb) =>new{tableA = ta, tableB = tb})
.Where(a=> a.email == "whatever#domain.com").Select(a=> new{a.reservatieID});

update multiple rows with joins

I have this query in postgresql:
select *
from A s
join B m on (s.id=m.id)
where m.key=4 and s.ran=some_input_from_user
This gives me all the rows that I need to update.
I want to set A.value to be 90 for all these rows.
It doesn't look like a standart update query
if I do...
Update A set value=90 where.....
then I can't do the join.
any ideas how to do it?
This is the basic update syntax for PostgreSQL where you are updating based on a join to another table:
update A s
set
value = 90
from B m
where
s.id = m.id and
m.key = 4 and
s.ran = some_input_from_user
The trick is you never use the alias in the lvalue for the set commands. In other words, value = 90 is not s.value = 90. It seems minor, but I'm pretty sure it will prevent your query from working. The rationale is if you are updating table A (alias s) then any fields you are updating are, de-facto, from table A -- no need to alias them, and to allow aliases would almost imply you could update something other than A with this statement, which you cannot.
You can definitely use them in the rvalues, so this would certainly be okay (if it were your desire to update A based on B):
update A s
set
value = m.salary * s.commission
from B m
where
s.id = m.id and
(s.value is null or
s.value != m.salary * s.commission)
Here is the query:
update a set value = 90
where exists (
select 1 from b
where a.id = b.id and b.key=4
and a.ran=some_input_from_user);
The above query will eliminate the requirement of reading table a twice.
Also you can use this query:
update a set value = 90
where a.id in
(select b.id from b
where a.id = b.id and b.key = 4
and a.ran=some_input_from_user);
TRY THIS
UPDATE A
SET A.VALUE = 90
from A
join B m on (A.id=m.id)
where m.key=4 and s.ran=some_input_from_user

Update a table by using a join [duplicate]

This question already has answers here:
Update a table using JOIN in SQL Server?
(13 answers)
Closed 8 years ago.
I have two tables ta, tb. ta columns - cId, c1, c2. c1 and c2 contain nulls and need to be filled with data. tb columns - cId, c3, c4. The data for c1 and c2 will come from c3 and c4 respectively.
So, I tried to do a simple inner join first. Both tables were aliased as al_ta and al_tb respectively. Then, I put an update statement -
UPDATE ta SET
al_ta.c1 = al_tb.c3,
al_ta.c2 = al_tb.c4
FROM ta AS al_ta
INNER JOIN tb AS al_tb
ON al_tb.cId = al_tb.cId
This does not work and I get an error - The multi-part identifier al_ta.c1 could not be bound. How do I make this work ?
Sample tables -
ta
cId c1 c2
1 NULL NULL
2 NULL NULL
3 NULL NULL
tb
cId c3 c4
1 11 111
2 22 222
3 33 333
4 44 444
When referencing the columns, you need to use the alias, not the base table name, if you've abstracted the table names away in the JOIN. Guessing at what your join might look like, you probably meant to write it this way:
UPDATE ta SET
ta.c1 = tb.c3,
ta.c2 = tb.c4
FROM dbo.some_long_table_name_a AS ta
INNER JOIN dbo.some_long_table_name_b AS tb
ON ta.cId = tb.cId
WHERE ta.c1 IS NULL OR ta.c2 IS NULL;
I don't understand the purposes of saying:
FROM ta AS al_ta
Why would you bother using an alias here that is actually harder to write than the original table name?
Please Try it
update ta set
ta.c1 = b.c3,
ta.c2 = b.c4
from ta a join tb b on a.cid = b.cid

Update on join in Sqlite

I have two two tables A and B in which both P columns are common and I need to use update command in table B only when both p values are same and C column from table A is given
What I am trying is:
update B
set P =100
where B.P=A.P
and A.C=60
But it's giving me error no such column A.P
You are updating table B and do not have reference to table A, so sqlite just does not know where to look for. Try this:
UPDATE B
SET P = 100
WHERE B.P IN (SELECT A.P
FROM A
WHERE A.C = 60)
You can do it like this
Update B set P = 100 WHERE B.P = (Select P from A WHERE C = 60)

SQL query get record table A based on parentid Table B

[SOLVED: thanks to bluefeet and Brian I got to build this working query (turned out I also needed a fourth table / view)]"
SELECT A.SalesLineID, A.ArticleResultID, B.ID, C.ID, D.Value
FROM VIEWA AS A
INNER JOIN TABLEB AS B ON A.ArticleResultID = B.ArticleResultID
AND B.Name = N'Collect' AND DAPR.Value = 1
INNER JOIN TABLEC AS C ON B.ArticleResultID = C.ID
AND C.Name='Assemble'
AND C.Value = 1
INNER JOIN TABLED AS D ON D.ArticleResultID = C.ParentId
AND D.Name = 'IndexY'
WHERE (A.SalesID = #SalesID)
[UPDATE: I've made a mistake assuming IndexY/Color and ProdId where fields Table A is some kind of parameter / property table with only 5 columns ID - NAME - VALUE - ARTICLERESULTID - PRODID. IndexY is a value of the Name field..]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
ID Name Value ArticleResultID ProdID Color
1 Operation Collect 110 10 BLACK
2 IndexY 10 110 10 -
3 Operation Collect 101 11 WHITE
Table B
ID ParentID Name Value
101 110 Assemble 1
101 100 Assemble 0
Steps:
Find record in A with Name = Operation and Value = Collect and ProdId = 11 AS ORG_A
Find record in B With B.ID = ORG_A.ArticleResultId AND B.NAME = 'Assemble'AND B.VALUE = 1 AS B
Find record in A With A.ArticleResultID = B.ParentID as NEW_A
In the above scenario thats ORG_A.ArticleResultID = 11 --> B.ParentID = 110 --> NEW_A.ARTICLERESULTID = 110 --> (IndexY - Value - 10)
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D
[OLD DESCRIPTION:]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
Name Value ArticleResultID IndexY Color ProdID
Operation Collect 110 10 - 0
Operation Collect 101 _ Black 100
Table B
ID ParentID Name Value Dx Dy
101 110 Assemble 1 1000 500
101 100 Assemble 0 400 300
I want to fetch all records from A where NAME equals 'operation', VALUE equals 'Collect' and PRODID = '100' but I also want (here's my problem) the IndexY value of the record in Table A with the PARENTID in Table B which joins on TABLE B.ID = A.ArticleResultID AND Name = 'Assemble' and VALUE = '1'
In the above scenario thats ParentID 110 which gives me the record in Table A with ArticleResultID 110 with IndexY (10).
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D
Since your requirements are not the clearest. How about this:
SELECT a1.*, a2.indexy as additionalIndexY
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
Here is a sqlfiddle with a working version
If you just want the record with the 110 articleresultid, then:
SELECT a2.*
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN join ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
see the sqlfiddle for the second example
Does this work for you?
SELECT a.*,c.IndexY
FROM [TableA] a
JOIN [TableB] b ON a.ArticleResultId = b.id
AND b.Name ='Assemble'
AND b.Value = 1
JOIN [TableA] c ON b.ParentId = c.ArticleResultID
WHERE a.name = 'Operation'
AND a.Value = 'Collect'
AND a.ProdId = 100