SQL Update Using From - sql

I know this is a contrived example so please do not jump all over me for the uselessness of the code. This is an issue in more complex chunk of code but I wanted to isolate the I am having.
The error I am getting is 'SQL Error: ORA-00933: SQL command not properly ended'. Any ideas? I am using SQL Developer by the way.
Once again...this is a contrived example and while the join is pointless in this case it is not in the more complex example.
Here is the code:
Update u
set first300pa = 1
from GameData_ME u
inner join
GameData_ME v on u.pitchandeventid = v.pitchandeventid

As this blog says,
Those who transitioned from SqlServer
to Oracle might find the absence of
the UPDATE FROM a significant loss.
Fortunately, the blog continues by showing a lot of the power of Oracle's UPDATE and how to use it to perform the tasks you need (but it won't be with a FROM clause in the UPDATE statement... not in Oracle!-).

Related

Two similar UPDATE statements in Redshift, one throws an error but the other doesn't

I have been working on converting some SQL scripts from T-SQL to Redshift and came across this error a few times: Error: Target table must be part of an equijoin predicate wit UPDATE statements using JOINs.
There are several questions on SO about this problem and I was able to use them to fix my UPDATE statements by using subqueries. (This question and this question specifically)
I thought I had this all figured out until just now when I was able to execute an UPDATE statement similar to the ones I was getting errors for without issue. I want to understand what is going on here so I can be better at writing queries for Redshift.
Here is an example of one of the UPDATE statements that threw the above error:
UPDATE billing_temp
SET spotlink = sl.spotlink
FROM billing_temp AS bt
LEFT JOIN spot_link AS sl
ON bt.dupeid = sl.dupeid;
And here is the UPDATE statement that is working without any changes:
UPDATE public.billingcombined
SET revenue_type = r.revenue_type
FROM public.billingcombined AS b
JOIN public.revenuetype AS r
ON b.contract_number = r.contract_number;
These look exactly the same to me. The only difference I can see is that the first one is using two temporary tables, but I wouldn't think this would cause an issue.
EDIT: I just saw the LEFT JOIN in the first query vs the JOIN in the second. Is this the reason the second query works?
EDIT2: After trying another UPDATE statement with a JOIN rather than a LEFT JOIN I was able to confirm that using a JOIN allows it to run without error.
For reference, here is how I had to correct the first query to run without errors based on what I learned from the linked SO questions:
UPDATE billing_temp
SET spotlink = btsl.spotlink
FROM
(
SELECT bt.dupeid,
sl.spotlink
FROM billing_temp AS bt
LEFT JOIN spot_link AS sl
ON bt.dupeid = sl.dupeid
) AS btsl
WHERE billing_temp.dupeid = btsl.dupeid;
The answers say the problem is you can't use the table getting updated directly in the FROM clause. But now that doesn't seem to be the case for my second UPDATE statement.

SQL Progress column not found

I have this query:
SELECT spechist.item,image."image-path",image."image-item",image."image-source"
FROM PUB.spechist left outer join PUB.image on (image."image-item"=spechist.item)
WHERE (spechist."photocard-display"=yes) AND (spechist."rec-type"='I') and (spechist.item='111')
For some reason I get this error when i run the query:
error message
[MERANT][ODBC PROGRESS driver][PROGRESS]Column not found/specified (7520)
When I remove the following from the query it runs fine:
WHERE (spechist."photocard-display"=yes) AND (spechist."rec-type"='I') and (spechist.item='111')
I know for a fact those columns are in the table. What am I doing wrong?
Thanks
With out seeing your schema I can't tell for sure but here are some things to try.
Is the capitalization correct?
Do you need to use the full table name PUB.spechist
Does it work with any of those three restrictions in the where clause? Try adding them one at a time and see which one stops execution.

SQL Query takes less than 1 sec in SSMS and forever in code

I'm running this query against a quite big table (i guess, around 150-200 000 rows):
select count(Distinct(eti.Email)) FROM table1 eti
LEFT OUTER JOIN table2 ti on ti.Email = eti.Email and ti.SiteId = eti.Site_Id
WHERE eti.Site_Id=1
In SMMS (SQL Server Management Studio) it takes less than 1 secound to execute but when I try to execute from my ASP.NET-site it times out.
I'm using PetaPoco to fetch the data which "under the hood" executes this code:
using (var cmd = CreateCommand(_sharedConnection, sql, args))
{
object val = cmd.ExecuteScalar();
OnExecutedCommand(cmd);
return (T)Convert.ChangeType(val, typeof(T));
}
I've been reading about that SSMS has som "special settings" when it executes the query? I really need to get this up and running.
Could the "MARS"-setting in the connection be have any impact on this? How do i debug and find the problem?
Thanks!
Thank you for the help!
I have finally found the issue (and it's a little embarrassing)... I found this blog question from msdn forums about sp_executesql being slow:
and Dan Guzman who is SQL Server MVP answers: http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/3cb08860-49a0-432a-8605-0af6b374dded/
Another possible issue is data type mismatches between the parameters and the column data types, resulting in non-sargable expressions and a bad plan. Please post your query and table DDL if you need further help.
So i doubled checked and it turned out that table2 had both the Email and the SiteId-fields set as "Nullable", changing them to match table1 fixed the issue.

SQL Query in Hibernate

I'm carrying out a SQL query which looks like:
SELECT thi.*
FROM track_history_items thi
JOIN artists art
ON thi.artist_id = art.id
WHERE thi.type = TrackBroadcast
Group By art.name
ORDER thi.created_at DESC
This works fine when I run it directly on my database from MySql Workbench, but when I run in through Hibernate, I get a No Dialect mapping for JDBC type: -1 error.
Anyone have any ideas what could be causing it?
Probably one or more of the columns in the query is not supported by the mysql dialect... try expanding the * and add one column at a time until you find the offending one.
Then it is just a matter of deciding whether you need that column or not.

Access DB update one table with value from another

I'm trying to update all records in one table with the values found in another table.
I've tried many versions of the same basic query and always get the same error message:
Operation must use an updateable
query.
Any thoughts on why this query won't work in Access DB?
UPDATE inventoryDetails as idet
SET idet.itemDesc =
(
SELECT bomItemDesc
FROM BOM_TEMPLATES as bt
WHERE bt.bomModelNumber = idet.modelNumber
)
also tried this because I realized that since the second table has multiple model number records for each modelnumber - and I only need the first description from the first record found for each model number.
UPDATE inventoryDetails as idet
SET idet.item_desc =
(
SELECT TOP 1 bomItemDescription
FROM BOM_TEMPLATES as bt
WHERE bt.bomModelNumber = idet.modelNumber
)
...still getting the same error though.
You have to use a join
UPDATE inventoryDetails
INNER JOIN BOM_TEMPLATES ON inventoryDetails.modelNumber = BOM_TEMPLATES.bomModelNumber
SET inventoryDetails.itemDesc = [bomItemDesc];
Any thoughts on why this query won't work in Access DB?
The answer is, because ACE/Jet SQL syntax is not SQL-92 compliant (even when in its ANSI-92 Query Mode!).
I'm assuming yours is a scalar subquery. This construct is simply not supported by ACE/Jet.
ACE/Jet has its own quirky and flawed UPDATE..JOIN syntax, flawed because the engine doesn't force the JOINed values to be scalar and it is free to silently use an arbitrary value. It is different again from SQL Server's own UPDATE..JOIN syntax but at least SQL Server supports the Standard scalar subquery as an alternative. ACE/Jet forces you to either learn its quirky non-portable ways or to use an alternative SQL product.
Sorry to sound negative: the ACE/Jet engine is a great piece of software but UPDATE syntax is absolutely fundamental and the fact it hasn't been changed since the SQL-92 Standard really show its age.
try:
update idet
SET idet.itemDesc = bt.bomItemDesc
from inventoryDetails as idet
inner join BOM_TEMPLATES as bt
on bt.bomModelNumber = idet.modelNumber
This is how I would write it for SQL server. Hope Access understands the same command.