I am trying to recreate a query that was done in MS Access, and now is being handled in a SQL Server environment. I understand that some of the SQL syntax is different in Access than it is in SQL Server. Is there somewhere online that points out the main differences, or will help translate one to the other?
This is a query that I need to update to use in SQL Server:
UPDATE
dbo.TPR00100
INNER JOIN (
dbo.UPR10302
LEFT JOIN dbo.B3980280 ON dbo.TPR10302.COMPTRNM = dbo.B3980280.COMPTRNM
) ON dbo.TPR00100.STAFFID = dbo.TPR10302.STAFFID
SET
dbo.B3980280.COMPTRNM = dbo.TPR10302.comptrnm,
dbo.B3980280.BI_LOC_ID = dbo.TPR00100.locatnid
WHERE
(((dbo.B3980280.COMPTRNM) Is Null))
What are they key aspects that need to be handled differently in a SQL Server transaction for this query?
If find it handy to use an updateable CTE for this:
with cte as (
select
b39.comptrnm b39_comptrnm
b39.bssi_loc_id b39_bssi_loc_id,
tpr.comptrnm tpr_comptrnm,
tpr.locatnid tpr_locatnid
from dbo.tpr00100 tpr
inner join dbo.upr10302 upr on tpr.staffid = upr.staffid
inner join dbo.b3980280 b39 on tpr.comptrnm = b39.comptrnm
where b39_comptrnm is null
)
update cte
set b39_comptrnm = tpr_comptrnm, b39_bssi_loc_id = tpr_locatnid
Note: I am not really sure why the table to update is left joined in the original query, so I turned it to an `inner join .
Related
I am trying to re-write this query which I didn't originally write. It was originally created in Access 2000.
UPDATE DISTINCTROW NewEx
INNER JOIN NewIm
ON NewEx.Number = NewIm.[7]
SET NewEx.Response = [4] ****
From what I understand from T-SQL this should do the same thing.
UPDATE NewEx
SET NewEx.Response = NewIm.[4]
FROM NewEx
INNER JOIN NewIm ON NewEx.Number = NewIm.[7]
**** I can't understand that you don't have to specify the table name for the join in Access but this totally works.
I had a side question, is there any machine assisted way to convert these queries? A lot of the things I am running into from Access are basically unreadable.
Your rewritten query is fine. You might introduce table aliases and just use JOIN for a more concise version:
UPDATE ne
SET Response = ni.[4]
FROM NewEx n JOIN
NewIm ni
ON ne.Number = ni.[7];
I have been trying to figure out why the following SQL works
SELECT c_Supplier.Supplier_ID AS A_ID, c_Supplier.Name, c_Supplier.RFC, c_Supplier_Direccion.Description, c_Supplier_Direccion.Address, c_Supplier_Phone.Phone
FROM c_Supplier LEFT JOIN (c_Supplier_Direccion LEFT JOIN c_Supplier_Phone ON c_Supplier_Direccion.Supplier_Direccion_ID = c_Supplier_Phone.Supplier_Direccion_ID) ON c_Supplier.Supplier_ID = c_Supplier_Direccion.Supplier_ID
WHERE (c_Supplier.Supplier_ID=1);
But when I try to use the aliasname (A_ID) in the WHERE clause, I got an error
SELECT c_Supplier.Supplier_ID AS A_ID, c_Supplier.Name, c_Supplier.RFC, c_Supplier_Direccion.Description, c_Supplier_Direccion.Address, c_Supplier_Phone.Phone
FROM c_Supplier LEFT JOIN (c_Supplier_Direccion LEFT JOIN c_Supplier_Phone ON c_Supplier_Direccion.Supplier_Direccion_ID = c_Supplier_Phone.Supplier_Direccion_ID) ON c_Supplier.Supplier_ID = c_Supplier_Direccion.Supplier_ID
WHERE (A_ID=1);
Any ideas?
I don't understand your question. This is a reasonably formed SQL query:
SELECT c_Supplier.Supplier_ID AS Entidad_ID, c_Supplier.Name,
c_Supplier.RFC, c_Supplier_Direccion.Description,
c_Supplier_Direccion.Address, c_Supplier_Phone.Phone
FROM c_Supplier LEFT JOIN
(c_Supplier_Direccion LEFT JOIN
c_Supplier_Phone
ON c_Supplier_Direccion.Supplier_Direccion_ID = c_Supplier_Phone.Supplier_Direccion_ID
) ON c_Supplier.Supplier_ID = c_Supplier_Direccion.Supplier_ID
WHERE (c_Supplier.Supplier_ID = 1);
(I would recommend table aliases for readability, but that is a separate issue.)
It has no alias called A_ID anywhere in the query, so there is no reason to ever expect a reference to A_ID to work (unless it is a column in one of the tables).
And, SQL doesn't allow the re-use of table aliases in the SELECT where they are defined or the WHERE clause. This is not an MS Access limitation; it is how the SQL language is defined.
If you want to do so in MS Access, you can use a subquery and reference the table alias in the outer query.
I am a complete beginner to SQL Server, and I have reached my limit.
Currently I am using a script to update a table from another table using a column. Since both databases are assigned to 2 different 3rd party software, I created a .bat script to use for task manager in windows server, that way it can update every 10 minutes.
While this is tested and works, I feel there has to be a way to create a relationship between the two databases without having to use the task.
UPDATE therefore.dbo.thecat51
SET num_factura =
(SELECT therefore.dbo.documentos.num_factura
FROM therefore.dbo.Documentos
WHERE therefore.dbo.thecat51.num_albaran=therefore.dbo.documentos.num_albaran)
WHERE therefore.dbo.thecat51.num_albaran =
( SELECT therefore.dbo.documentos.num_albaran
FROM therefore.dbo.Documentos
WHERE therefore.dbo.thecat51.num_Albaran = therefore.dbo.documentos.num_albaran)
Also, we are using SQL Server Express, so I don't have the option to create a scheduled job.
You can do the UPDATE with an INNER JOIN to perform the update you need:
UPDATE A SET
num_factura = B.num_factura
FROM therefore.dbo.thecat51 A
INNER JOIN therefore.dbo.Documentos B
ON A.num_albaran = B.num_albaran
Use an INNER JOIN between your two tables. At the time I posted this, you had not told us which RDBMS you are using so I will give answers for SQL Server and MySQL:
SQL Server:
UPDATE t1
SET t1.num_factura = t2.num_factura
FROM therefore.dbo.thecat51 AS t1
INNER JOIN therefore.dbo.Documentos AS t2
ON t1.num_albaran = t2.num_albaran
MySQL:
UPDATE therefore.dbo.thecat51 AS t1
INNER JOIN therefore.dbo.Documentos AS t2
ON t1.num_albaran = t2.num_albaran
SET t1.num_factura = t2.num_factura
I tried to make an UPDATE query using data from another table
which in my case was a result of a query and it looks like this:
UPDATE CalculatedQueryINNER JOIN Orders
ON CalculatedQuery.orderid = Orders.OrderID
SET Orders.TotalPrice = [CalculatedQuery].[calculated];
But it's not working, it shows an error must use an updateable query
But i went and copied the results from the query to a table named temp and i did the same and it worked!!
UPDATE temp INNER JOIN Orders
ON temp.orderid = Orders.OrderID
SET Orders.TotalPrice = [temp].[calculated];
Can anyone please provide me with a solution on how to use the query as a table
please?
EDIT: I tried to treat a query as if it was a table by itself, that was my issue, joining tables is not an issue in update queries inaccess
I read a lot through different sites
The problem that I introduced is unsolvable in access.
There is an issue with the update queries since they are not standart in access.
The problem is That in access you can't use another sql query inside an update statement because as i read any queries that use group by or join or select within select etc.. Are marked as unupdateable queries and you can't use them.
As i read i found a few good solutions using a temporary table like i did
Or using dlookup() function.
Keep in mind that you can also perform DELETE and UPDATE statements with a 'FROM', which will help in writing the query in a syntax that is more familiar. Given that you are running two very different update statements above, I've re-written both of them here. Also, given that you're using an Inner Join on both statements, which performs an intersect of data, I've rearranged the tables in the order of update.
UPDATE O
SET [TotalPrice] = [T].[calculated]
FROM [Orders] AS O
INNER JOIN [temp] AS T
ON [T].[orderid] = [O].[OrderID];
UPDATE O
SET [TotalPrice] = [CQ].[calculated]
FROM [Orders] as O
INNER JOIN [CalculatedQuery] as CQ
ON [CQ].[orderid] = [O].[OrderID];
Setting up your Updates (and likewise Deletes) in this syntax is pretty slick, as it allows for another nice feature: running a select to see what data will be changing:
SELECT [O].[TotalPrice]
, [T].[calculated]
FROM [Orders] AS O
INNER JOIN [temp] AS T
ON [T].[orderid] = [O].[OrderID];
SELECT [O].[TotalPrice],
, [CQ].[calculated]
FROM [Orders] as O
INNER JOIN [CalculatedQuery] as CQ
ON [CQ].[orderid] = [O].[OrderID];
I have the following T-SQL query (a simple test case) running fine in MS SQL but cannot get the equivalent query in MS Access (JET-SQL). The problem is the additional criteria in the LEFT JOIN. How can I do this in MS Access?
T-SQL:
SELECT * FROM A
LEFT OUTER JOIN B ON A.ID = B.A_ID
AND B.F_ID = 3
JET-SQL (what I have so far but crashes Access!):
SELECT * FROM dbo_A
LEFT JOIN dbo_B ON (dbo_A.ID = dbo_B.A_ID AND dbo_B.F_ID = 3)
You need to use a subselect to apply the condition:
SELECT *
FROM dbo_A LEFT JOIN
[SELECT dbo_B.* FROM dbo_B WHERE dbo_B.F_ID = 3]. AS dbo_B
ON dbo_A.ID = dbo_B.A_ID;
If you're running Access with "SQL 92" compatibility mode turned on, you can do the more standard:
SELECT *
FROM dbo_A LEFT JOIN
(SELECT dbo_B.* FROM dbo_B WHERE dbo_B.F_ID = 3) AS dbo_B
ON dbo_A.ID = dbo_B.A_ID;
Do you need this to be editable in Access? If not, just use a passthrough query with the native T-SQL. If so, I would likely create a server-side view for this, and I'd especially want to move it server-side if the literal value is something you would parameterize (i.e., the F_ID=3 is really F_ID=N where N is a value chosen at runtime).
BTW, I write these subselect derived table SQL statements every single day while working in Access. It's not that big a deal.
Do you get an error message when it crashes or does it just lock up? Judging by the dbo_B name I'm going to guess that these are linked tables in Access. I believe that when you do a join like that Access doesn't tell SQL server that it needs the result of the join, it says, "Give me all of the rows of both tables" then it tries to join them itself. If the tables are very large this can cause the application to lock up.
You're probably better off creating a view on SQL Server for what you need.
I think ms access expect to both tables name in each section of Joins ON clause. As a trick this work for me:
SELECT * FROM A
LEFT OUTER JOIN B ON A.ID = B.A_ID
AND B.F_ID = IIF(True, 3, A.ID)
A.ID or any other else field from table A
That last condition technically isn't a join but a comparison to a literal value. Put it in a WHERE clause:
SELECT *
FROM a LEFT OUTER JOIN b ON a.ID = b.a_id
WHERE b.f_id = 3;