简体   繁体   中英

Moving “INNER JOIN” Query from Access to C# and SQL

I'm having hard time with my Access query being run via C# code and SQL.

I'm getting error saying something about 'INNER'.

UPDATE Service INNER JOIN Sales ON ([Service].VIN=Sales.VIN) AND (Service.Address=Sales.address) SET Service.Selldate = Sales.Selldate
WHERE Service.Address=Sales.address And Service.VIN=Sales.VIN;

Access lets you write sql in that way but sql server wont you need to change your update statement so that the joins are outside the update

something along the lines of the following should work

UPDATE s
SET s.Selldate = sa.Selldate
from 
Service s INNER JOIN Sales sa ON s.VIN=sa.VIN AND s.Address=se.address
WHERE s.Address=se.address And s.VIN=sa.VIN;

You can use alias as ser forService and sale for Sales and build you updatestatement like this.

UPDATE ser SET ser.Selldate = sale.Selldate  
FROM Service ser    
JOIN Sales sale ON (ser.VIN=sale.VIN) AND (ser.Address=sale.address) 
WHERE ser.Address=sale.address And ser.VIN=sale.VIN;

Consider using a view in your DBMS, then all you have to do is use the view name as the table name.

In MSSQL you can create views which are effectively saved SELECT statements which you can call like tables. Open you DBMS right-click on views and add new view. Think of a view like a query in access.

I think you're better off doing this as two different queries, but if you wanted to do this in one go, you could use subqueries.

UPDATE Service SET Service.Selldate = Sales.Selldate
WHERE service.vin in (select sales.vin from sales) and service.address in (select sales.vin from sales) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM