UPDATE one table based on values in another table - sql-server

I have two tables, Table 1 has three fields (a1,a2,a3) and table 2 has three fields (b1,b2,b3) I want to update table one base on table two when a1=b1 . This is essay in Microsoft access but I want t to do it in Microsoft Access project Sql server 2005 as a back end.
UPDATE t1
SET a1 = t2.b1, a2 = t2.b2, a3 = t2.b3
where a1=b1

INNER JOIN the tables. Then you won't need to SET t1.a1=t2.b1 because those are already equal based on the join condition. So just SET the other two field values ...
UPDATE t1 INNER JOIN t2
ON t1.a1=t2.b1
SET t1.a2 = t2.b2, t1.a3 = t2.b3
Note this suggestion is Access SQL and should work from MS Access regardless of whether t1 and t2 are native Access tables, links to SQL Server tables, or a mix of both types.

Try the following for SQL Server:
UPDATE t1
SET a1 = t2.b1, a2 = t2.b2, a3 = t2.b3
FROM t1
INNER JOIN t2 ON t1.a1 = t2.b1

Related

Filter based on two databases

I have the same query on two databases, but in the first database it finds me data, while in the second one it does not. Is it possible to build on these two bases so that I can filter all those that exist in the first base and not the second. These are my sql query:
use firstDB;
SELECT A_ANSPRECHPARTNER.AAS_ID FROM A_ANSPRECHPARTNER LEFT OUTER JOIN A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID WHERE ADR_Nr = 106740
use secondDB;
SELECT A_ANSPRECHPARTNER.AAS_ID FROM A_ANSPRECHPARTNER LEFT OUTER JOIN A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID WHERE ADR_Nr = 106740
the current result
If these databases are on the same server you can put each of your queries in a subquery specifying the database name when referencing the table. Then left join the subquery on firstDB to the subquery on secondDB filtering the records where there is a record in firstDB, but not in secondDB.
SELECT DB_1.*
FROM (
SELECT A_ANSPRECHPARTNER.AAS_ID
FROM firstDB.dbo.A_ANSPRECHPARTNER
LEFT OUTER JOIN firstDB.dbo.A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID
) DB_1
LEFT JOIN (
SELECT A_ANSPRECHPARTNER.AAS_ID
FROM secondDB.dbo.A_ANSPRECHPARTNER
LEFT OUTER JOIN secondDB.dbo.A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID
) DB_2 ON DB_1.ADR_Nr = DB_2.ADR_Nr
WHERE DB_2.ADR_Nr IS NULL
AND DB_1.ADR_Nr = 106740
You can just remove the AND DB_1.ADR_Nr = 106740 in order to find all records in firstDB that are not in secondDB. If these databases are on different servers you would have to set up a linked server and add that to the beginning of your table reference.

How to join two tables in SQL Server, where the IDs are not the same?

I have 2 tables in MS SQL and I am using MS Studio. The first table is called register it has a list of fields. The second table is called Plan and it has a list of fields.
The unique key in the table register is called RegisterID
The unique key in the table Plan is called RegisterID
The goal is to link with a left join the Register to the Plan. My difficulty is that the IDs are not exactly the same: An ID in Plan is xxxx (xxxx being the value of the ID). An ID in Register is xxxx_0 (xxxx being the same value than Plan was added _0)
What is the solution to link those 2 tables in a simple query with fields of both tables?
Here is some more information.
You will find the 2 tables and the expect result of the query.
Tables structure
Expected result
Please note that I am a beginner.
Add the characters to the first field or remove them from the second
select *
from register
left outer join plan on register.RegisterID+'_0' = plan.RegisterID
select *
from register
left outer join plan on register.RegisterID = REPLACE(plan.RegisterID,'_0','')
If the length of the ID is always the same there is a third option
select *
from register
left outer join plan on register.RegisterID = LEFT(plan.RegisterID,6)
You could try something like:
SELECT *
FROM Register R
LEFT OUTER JOIN [Plan] P
ON LEFT(R.RegisterID, LEN(R.RegisterID) - 2) = P.RegisterID
The expression LEFT(R.RegisterID, LEN(R.RegisterID) - 2) will remove the '_0' from Register.RegisterID and the JOIN will work.

Joining Two databases on same server

enter image description hereI'm facing some weird problem off late, when i try to write a query on joining two different table located in two different databases on same server i get an error.
For EX: if i select Database A separately data is getting displayed and i have to manually select database from available databases dropdown on the top left.
If i select Database B then i should manually change it back to Database B on available database dropdown, due to this issue I'm not able join two tables together. Please help me in getting settings right.
You can use by adding database name before the table name.
Select * from Database1.[dbo].Table1 t1
join Database2.[dbo].Table2 t2 on t1.columnName = t2.columnName
From Your Query lets T1 from DatabaseA and Remaining From DatabaseB
select distinct [Material_Number] from DatabaseA.[dbo].[view_sku_universe_from_sku_data_quality] T1
left join DatabaseB.[dbo].[table_Mat_Deter_SWM] T2 on T1.[Material_Number]=T2.[Sub_Material_1]
left join DatabaseB.[dbo].[table_Mat_Deter_SWM] T3 on T1.[Material_Number]=T3.[Sub_Material_2]
where T2.[Sub_Material_1] is not null or T3.[Sub_Material_2] is not null
1.Create a user which have rights to access both Databases
2.Login to SSMS from that user which you created (user which have rights to access both Databases).
3.Create query like this
SELECT * FROM TestDatabase1.[dbo].Test1 t1
JOIN TestDatabase2.[dbo].Test2 t2 on t1.TestID= t2.TestID

Update SQL Server via MS Access Routine (Inner Join Error)

I've taken a query from MS Access that updates a sql server table via an inner join with another table. The Query works perfectly via ms access interface.
When copy the syntax into vb.net and try and run the routine via a vb.net interface I keep receiving an error referencing to keyword near 'Inner'.
Here is the problem in the simplest form:
Table_A:
Field_Id (Auto)
FieldX_Id (Integer)
Table_B:
Field_Id (Auto)
FieldX_Id (Integer)
FieldLink_Id (Integer)
Example:
Table_A:
Field_Id = 1
FieldX_Id = 0
Table_B:
Field_Id = 2
FieldX_Id = 1234
FieldLink_Id =1
Object:
Update Table_A (Set FieldX_Id = 1234)
Syntax from Query:
UPDATE Table_A
INNER JOIN Table_B
ON Table_A.FieldLink_Id = Table_B.Field_Id
SET Table_A.FieldX_Id = [Table_B].[FieldX_Id];
You're joining upon the condition:
ON Table_A.FieldLink_Id = Table_B.Field_Id
But according to your tables structure, Table_A does not have a column called FieldLink_Id. Perhaps you should change it to:
ON Table_A.Field_Id = Table_B.FieldLink_Id
Use this syntax UPDATE:
UPDATE ta
SET FieldX_Id = tb.[FieldX_Id]
FROM Table_A ta
JOIN Table_B tb
ON tb.FieldLink_Id = ta.[Field_Id];

Transfer data from one table to another in sql server

I have two tables, one of which I don't need anymore. I want to transfer the piece of data i need from the obsolete table, into the table I'm going to keep. There are bookingid columns in both tables, which I can use to match the rows up. Its a 1 to 0 or 1 relationship. I've looked around and built up this query to accomplish the transfer, but I'm getting a could not be bound error on bookingtoupdate.bookingid
WITH bookingtoupdate (bookingid) AS
(
SELECT bookingid
FROM bookings
)
UPDATE bookings
SET meetinglocation = (SELECT business.name
FROM abk_Locations
INNER JOIN business ON dbo.abk_Locations.IP_Number = business.businessid
WHERE
(dbo.abk_Locations.Booking_Number = bookingtoupdate.bookingid)
)
WHERE
bookingid = bookingtoupdate.bookingid
Are there any obvious issues with my code?
I referred the following pages...
http://msdn.microsoft.com/en-us/library/ms175972.aspx
SQL Server FOR EACH Loop
You declare bookingtoupdate but you don't select anything from it. That's why it can't be bound.
Here is a simplified query to do what you need without CTE
UPDATE bookings
SET meetinglocation = business.name
FROM bookings
INNER JOIN abk_Locations ON abk_Locations.Booking_Number = bookings.bookingid
INNER JOIN business ON dbo.abk_Locations.IP_Number = business.businessid

Resources