Can someone please help me out with a query? In Microsoft SQL Server, I have the following query, which executes fine:
SELECT * FROM ControlPoint
INNER JOIN Project ON ControlPoint.ProjectID = Project.ProjectID
INNER JOIN Site ON Site.SiteID = Project.SiteID
WHERE Project.ProjectName LIKE '%Flood%'
My problem is, when I try to execute this on Microsoft Access, it gives me some kind of syntax error. It's been forever since I've used Access, but if I remember right, I think the joins need to be in parenthesis or something. Any help would be useful!
You will need some parentheses in addition to changing the wild cards:
SELECT * FROM (ControlPoint
INNER JOIN Project ON ControlPoint.ProjectID = Project.ProjectID)
INNER JOIN Site ON Site.SiteID = Project.SiteID
WHERE Project.ProjectName LIKE '*Flood*'
Note that the asterisk is used in the Access query window and with DAO, percent is used with ADO.
Access uses different wildcard patterns.
In your case, it will be - LIKE '?Flood?' (replace question mark with asterisk).
I don't know formatting codes to apply here so that it shows it correctly.
See the link for details - http://www.techonthenet.com/access/queries/like.php
The syntax error is caused because access uses " instead of ' as the string delimiter. As previously mentioned you will also need to change the % wildcards to *
Use this sintax in access never fails
(this is an example)
select * from customer, adress
where customer.id = adress.customerId
Related
Is there anything wrong with this statement?
SELECT *
FROM Movies INNER JOIN
Sessions
ON Movies.MovieID=Sessions.MovieID INNER JOIN
Tickets
ON Sessions.SessionID=Tickets.SessionID;
When ever I run it on Access I get a Syntax error 'Missing Operator'.
Also are there any alternatives to Access that I can import data from an excel spread sheet?
In general, no. In MS Access, yes. It likes extra parentheses, probably because the database developers don't believe in readability:
SELECT *
FROM (Movies INNER JOIN
Sessions
ON Movies.MovieID = Sessions.MovieID
) INNER JOIN
Tickets
ON Sessions.SessionID = Tickets.SessionID;
You could enable OPENROWSET if you have a local instance of SQL, and install MDACs (I would install both x86 and x64 if you have a 64 bit pc). Below is a link to an article that will help you get setup. Also, be sure to run the management studio with elevated privileges.
How to enable Ad Hoc Distributed Queries
Below is how the query would look. In my example I use Excel 8.0 instead of 12 because the column names are addressable in my select statements for 8.
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 8.0;Database=C:\Temp\MyExcelDoc.xlsx;',
'SELECT * FROM [Sheet1$]')
You can export data from an excel spreadsheet into a number of formats. I find two of the best to be;
Comma Separated Values (CSV)
XML
In many cases, you can deal with the data directly from the Excell spreadsheet.
It really depends on what you want to do.
Your SQL query looks fine - but with Access you need to do the extra joins in brackets;
SELECT * FROM ((Movies
INNER JOIN Sessions ON Movies.MovieID=Sessions.MovieID)
INNER JOIN Tickets ON Sessions.SessionID=Tickets.SessionID)
;
working on linking data between a SQL Server Database and MS Access. Right now someone is manually calculating Data from a SQL Database report and entering this into Access to run other reports within Access.
I have created a pass through query to pull the relevant information into an Access Table from the SQL Database( all working nicely )
Now I need to update the existing Access Tables with Data retrieved from the SQL pass through. I have tried a number of different queries all fussing at me for various reasons. Here is an example of the latest query that will get me what I need. This works if I setup a Sandbox in SQL Server and run it MSSQL Management Studio, but will not work in access
UPDATE JT
SET JT.ContractAmt = SBD.TotalSum
FROM JobTable_TEST AS JT
INNER JOIN (
SELECT Sum( Main.amt ) as TotalSum, Main.job
FROM Main
GROUP BY Main.job
) AS SBD
ON SBD.job = JT.JobNumber
In Access the Above Generates the following error "Syntax error( missing operator) in query expression.
Updating following attempt at using SQL Passthrough to run the update Query.
I updated my Query to do this directly from a Passthrough SQL Statement as suggested and get the following error.
ODBC--call failed.
[Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'TableName'.(#208)
Here is what the pass through query i used looked like.
UPDATE AccessTable
SET AccessTable.amt = SQLResult.Total
FROM TableName AS AccessTable
INNER JOIN ( SELECT SUM( SQLTableA.amt) as Total, SQLTableA.job
FROM SQLTableA
LEFT OUTER JOIN SQLTableB ON (SQLTableA.company = SQLTableB.company)
AND (SQLTableA.job = SQLTableB.job)
GROUP BY SQLTableA.job
) AS SQLResult
ON SQLResult.job = AccessTable.JobNum
hopefully that better describes where my tables are located and how my update needs to happen, and maybe someone can point out how this is wrong or if it will even work this way.
Any suggestions would be greatly appreciated
It appears your subquery, aliased as SBD, is missing a job_no column. Therefore you aren't going to be able to join on it.
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.
In MySQL, you can use the keyword USING in a join when you join on columns from different tables with the same name. For example, these queries yield the same result:
SELECT * FROM user INNER JOIN perm USING (uid)
SELECT * FROM user INNER JOIN perm ON user.uid = perm.uid
Is there an equivalent shortcut in SQL Server?
nope, have to use:
SELECT * FROM user INNER JOIN perm ON user.uid = perm.uid
No, SQL Server does not support this kind of shortcut.
I would like to point out that even if it did, shortcuts like these are NOT A GOOD IDEA. I recently worked on a database where the developers thought it would be a good idea to use the *= and =* shortcuts for RIGHT JOIN and LEFT JOIN. Good idea until someone upgraded the SQL Compatibility level to 90. Then it became an extremely Bad Idea.
So, learn from us. Shortcuts are bad. A little extra typing never killed anyone.
Also, I would add not to use wild characters, "*" in your select statement - explicitly specify the column name.
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.