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

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];

Related

SQL Server : update old ID to new ID (SELF JOIN in UPDATE)

I have a SQL Server table, and column category IDs (kategori) needs to be updated.
I need to find id_old first and update as new id instead:
How can I do this in batch with a T-SQL statement?
use SELF JOIN. You need to use a different alias on the table.
UPDATE t2
SET kategori = t1.id
FROM yourtable t1
INNER JOIN yourtable t2 ON t1.id_old = t2.kategori
WHERE t2.kategory <> 0 -- not sure but you might need this condition

UPDATE one table based on values in another table

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

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

Update a column of a table using another column of the same table

I have 2 tables vehicle_table and vehicle_group_table
vehicle_table --> Has columns --> vehicleid,groupname,groupid
vehicle_group_table --> has columns --> groupid,groupname
I want to update my vehicle_table's groupid column by joining vehicle_group_table on its common groupname column
UPDATE vehicle_table
SET vehicle_table.groupid = vehicle_group_table.groupid
WHERE vehicle_table.groupname = vehicle_group_table.groupname
But this seems to be not working.
UPDATE
V
SET
groupid = VG.groupid
FROM
vehicle_table V
JOIN
vehicle_group_table VG ON V.groupname = VG.groupname
You need to correlate the 2 tables via a JOIN. There are other ways with subqueries etc
Note: don't use aliases in destination/target columns in the SET clause as suggested in other answers. It fails in SQL Server Not in SQL Server 2012 so I think behaviour changed
For a more complicated setup: SQL update query using joins
You can apply the following syntax in your scenario:
UPDATE TargetTable
FROM SourceTable
SET TargetTableField = SourceTable.SourceTableField
WHERE TargetTable.Field = TargetTable.Field;
Try it
UPDATE A SET A.groupid = B.groupid
FROM vehicle_table A INNER JOIN vehicle_group_table B ON
A.groupname = B.groupname
Try this :
UPDATE a set a.groupid =b.groupid
from vehicle_table a
inner join
vehicle_group_table b
on a.groupname = b.groupname
Try this :-
Update vt
Set vt.groupiid = vgt.groupid
from vehicle_table vt
inner join vehicle_group_table vgt
on vt.groupname = vgt.groupname

Reference column in a table on another server in an UPDATE/JOIN (SQL Server)

I'm familiar with 4-part naming, but I get an error every time I try to reference a column. For example:
UPDATE my_table
SET my_table.column1 = otherserver.otherdatabase.dbo.othertable.column1
FROM my_table INNER JOIN otherserver.otherdatabase.dbo.othertable
ON my_table.column2 = otherserver.otherdatabase.dbo.othertable.column2
This throws the following error:
The multi-part identifier "otherserver.otherdatabase.dbo.othertable.column1" could not be bound.
I never have trouble if I am only reference a table, but when I append the column name, it always throws an error. Any ideas? SQL Server 2008
Just use the table name when you qualify your columns.
UPDATE my_table
SET my_table.column1 = othertable.column1
FROM my_table INNER JOIN otherserver.otherdatabase.dbo.othertable
ON my_table.column2 = othertable.column2
Or use an alias.
UPDATE my_table
SET my_table.column1 = OT.column1
FROM my_table INNER JOIN otherserver.otherdatabase.dbo.othertable as OT
ON my_table.column2 = OT.column2
Use table alias then access the column:
UPDATE my_table
SET my_table.column1 = A.column1
FROM my_table INNER JOIN otherserver.otherdatabase.dbo.othertable A
ON my_table.column2 = A.column2

Resources