Trying to rewrite Access query in SQL Server - sql

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

Related

Translating MS Access Query for SQL Server

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 .

how to use another table\query inside an update query

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

Query runs forever ORACLE

Am trying to update the isdeleted column in one table when a record is not in the other user table . My problem is the query l have written runs forever. how best can l write the query below.
update TBLG2O_REGISTER a set a."isDeleted" = '1'
where a."UserID" not in (select k."UserID" from TBLG2O_USER k)
The answer is going to be database engine-specific. Performance characteristic differ wildly, across different database engines, and you failed to specify which DB server you are using.
However, subqueries are frequently MySQL's Achilles heel; I wouldn't be surprised that if this was MySQL. If so, the following approach should have better performance characteristics with MySQL:
update TBLG2O_REGISTER a left join TBLG20_USER k using(UserID)
set a.isDeleted = '1' where k.UserID is null;
Finally got it to work Thank you for your help
Update TBLG2O_REGISTER a set a."isDeleted" = '1' where a."UserID" in (select p."UserID"
from TBLG2O_REGISTER p left join TBLG2O_USER k on p."UserID" =k."UserID"
where k."UserID" is null)

Using MS Access how to perform an update with multiple joins and where clauses?

I am having a bit of trouble with getting some MS Access SQL to work. Here is the high level:
I have values in one table, by15official that I need to use to update related records in another table, investmentInfo. Pretty straight forward except there are quite a few joins I need to perform to make sure the right record is updated in the investmentTable and I think I could figure this out with regular sql, but Access is not playing nicely. The following is my sql I am trying to use (which results in this error: "Syntax error (missing operator) in query expression ..."
update ii
set ii.investmentType = by15.InvestmentType
from investmentInfo as ii
inner join
(select by15Official.InvestmentType, by15Official.InvestmentNumber
from (((by15official left join investmentinfo on by15official.InvestmentNumber = investmentInfo.investID)
left join fundingSources on fundingSources.investId = investmentInfo.id)
left join budgetInfo on budgetInfo.fundingID = fundingSources.id)
where investmentinfo.submissionType = 2
and budgetInfo.byYear = 2015
and budgetInfo.type = 'X') as by15
on by15.InvestmentNumber = ii.investID
This seems like it should work, I am trying to join this group of tables that provide the investmentType which is what I want to update in the main table investmentInfo. Thoughts? Can this be done in Access? I have googled around and found the above which I adapted to meet my needs (actually I am pretty sure I found the above on SO).
Thoughts?
Thank you very much!
I did get some help from someone over at the MS forums. The solution was to format my SQL slightly differently. Here is the code that eventually worked.
UPDATE
(
(
by15official LEFT JOIN investmentinfo
ON by15official.InvestmentNumber = investmentInfo.investID
)
LEFT JOIN
fundingSources
ON investmentInfo.id = fundingSources.investId
)
LEFT JOIN budgetInfo
ON fundingSources.id = budgetInfo.fundingID
SET investmentInfo.investmentType = by15official.InvestmentNumber
WHERE (investmentinfo.submissionType = 2)
And (budgetInfo.byYear = 2015)
Perhaps the above Access SQL can help others.
Basically you want to do the update and the joins before doing the set and where clauses. Makes sense, and I am sure if I were better skilled at writing SQL I would have known that.
I know this doesn't use the original code, but this is a generic example with the proper syntax:
UPDATE ([My First Table]
LEFT JOIN [My First Table] ON [My Second Table].[RequestID] =
[My First Table].[ID])
INNER JOIN [My Third Table] ON [My Second Table].[Some Field] =
[My Third Table].[Matching Field]
SET
[My First Table].[Approved By] = [My Third Table].[Approver],
[My First Table].[Approval Date] = [My Second Table].[Modified]
WHERE [My First Table].[Review Status] LIKE '*Approved*'
The easiest way to figure out Joins in SQL in Access is go to the Query Design window, add the tables you want, and click and drag Columns to the table you want to join based on. You do this for all your tables and boom your joins are done. Also if your joins are getting too complicated it generally means there is a database design issue though I know fixing this is not always an option in production databases. Good Luck!

Updating Field Based on Another Table

As someone new to SQL can you please point me in the right direction. I know the following is wrong but I am not sure why.
UPDATE cus
SET cus.leg_no = new.leg_no
WHERE cus.c_no = new.c_no
The cus table currently has null in the leg_no. I want to update it from the new table. I will be joining on c_no which is in both tables.
I have tried searching the web but I am getting further confused. This has lead me to think I need FROM but something is telling me that is when using SELECT rather than UPDATE.
UPDATE cus,new
SET cus.leg_no = new.leg_no
WHERE cus.c_no = new.c_no
Here's the standard way to do it:
UPDATE cus
SET cus.leg_no = (select new.leg_no from new WHERE cus.c_no = new.c_no)
Here's the SQL Server/MS access dialect way to do it:
UPDATE cus
SET cus.leg_no = new.leg_no
FROM cus
INNER JOIN new
ON cus.c_no = new.c_no
Note that, with the standard method, if there are multiple rows in new that match a particular row from cus, you'll get an error message. Whereas with the SQL Server/MS access dialect form, the system will arbitrarily select one of the rows, and issue no warning or error.