Query Syntax help! - sql

I am running this query in Access 2007 and getting a syntax error on this query
UPDATE INQuery SET Awords=(SELECT Coalsce("SELECT (parentdesc & '/' & keyword) From Awords LEFT JOIN INQuery ON Awords.id=INQuery.item_id",";"))
WHERE Awords.id=IN_Query.item_id;
System error in query expression
'(SELECT Coalsce("SELECT (parentdesc &
'/' & keyword) From Awords LEFT JOIN
INQuery ON
Awords.id=INQuery.item_id",";"))'.
When I run the query expression separately I get the desired result.
NOTE: To see what Coalsce function is
please refer
VBA + String splitting

Do you actually need the additional SELECT?
UPDATE INQuery SET Awords=Coalsce(...) WHERE ...

Could it be that it's Coalesce not Coalsce? (typo?)

Related

MS Access - Ambiguous outer joins?

I am not sure why my query is netting me the following error:
The SQL statement could not be executed because it contains ambiguous outer joins. To force one of the joins to be performed first, create a separate query that performs the firm join and then include that query in your SQL statement.
This is my query, I only see one join:
SELECT PC.[Mother_Board_Name] & ',' & PC.[Mother_Board_Manufacturer]
FROM PersonalComputerHardware PC, Registers R
WHERE ',' & R.[Names].Value & ',' LIKE '*,' & PC.[Computer_ID] & ',*';
R.Names.Value is a reference to a multi-value field, by the way. I don't understand this error message because I am only using one Cross JOIN
Try this:
SELECT PC.Mother_Board_Name & ',' & PC.Mother_Board_Manufacturer
FROM PersonalComputerHardware PC
INNER JOIN Registers R ON R.Names LIKE '*,' & PC.Computer_ID & ',*'
Assuming your actual logic is correct, this should work.
However your logic is probably not correct. At best it will only match Computer_ID that is in the middle (not first or last) unless your Names has commas at the beginning and end as well.

Convert SQL Server Query to MS Access to Update Table

Could anyone help with this. I don't use Access often, but this process I'm building needs to utilize Access for the business to use. I have the following code, which will not work in Access. I keep getting the error 'Syntax Error (missing operator) in query expression'. Would anyone be able to convert this query into something Access will accept.
UPDATE Auto_DailyDiary_PrimaryTbl a
SET a.TotalDiary = sub.TotalDiary
FROM
(
SELECT CaseEEID, Count(CaseID) as TotalDiary
FROM dbo_Case
WHERE CaseStatus <> 6
GROUP BY CaseEEID
) sub
WHERE sub.EEID = a.EEID AND a.DiaryDt = Date()
Pretty sure Access doesn't like having a joined group by subquery in an update query.
Maybe try something like this?
UPDATE Auto_DailyDiary_PrimaryTbl
SET TotalDiary =
DCOUNT('CaseID', 'dbo_Case',
"CaseStatus <> 6 AND " &
"CaseEEID = '" & Auto_DailyDiary_PrimaryTbl.EEID & "'")
WHERE DiaryDt = Date()

How do I run an SQL update query using a like statement

I am trying to update a field in a table using an SQL update query where there is a like statement referencing a value in another table. They syntax unfortunately is not working. Below is my code. In short, I am trying to put a '1' in the field 'Query07ParolaChiave' in the table 'tblSearchEngine01' when the value located in table 'tblsearchengine07' is present in the field 'tblMasterListOfEventsNotes' located in the table 'tblSearchEngine01'. I think my code is almost complete but there is a syntax issue which i cant find.
st_sql = "UPDATE tblSearchEngine01, tblSearchEngine07 SET tblSearchEngine01.Query07ParolaChiaveSelect = '1' WHERE ((([tblSearchEngine01].[tblMasterListOfEventsNotes]) Like " * " & [tblsearchengine07].[ParolaChiave] & " * "))"
Application.DoCmd.RunSQL (st_sql)
I suggest you 2 solutions :
This one is using EXISTS functions, and will check for each row in tblSearchEngine01 if there is a matching value in tblsearchengine07
UPDATE
tblSearchEngine01
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
WHERE
EXISTS (SELECT 1
FROM tblsearchengine07
WHERE [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*')
This one is more performant because it uses JOIN
UPDATE
tblSearchEngine01
INNER JOIN tblsearchengine07
ON [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*'
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
I read something like in ADO/VBA, you have to use % instead of * as the wildcard.
You can have more information on wildcard and LIKE comparator here
UPDATE
Why the '1' after select in your first solution?
EXISTS (SELECT 1 ... is better for performance because it return only the number 1 instead of fields, anyway EXISTS just stop the excecution after 1 element found.
'Performant' means more consuming in regards to space and memory?
JOIN is more performant in term of time of execution, RDBMS are far better at joining tables than using subquery, in some rare case, it's more interesting to use the 1st solution.
Also, any initial thoughts as to why my original solution (coming straight from an Access Query which works) does not function?
I cannot really know but perhaps it's because of " * ", because you are saying SPACE + * + SPACE + VALUE + SPACE + * + SPACE. For ex : 'John' LIKE ' John '
May be with "*" instead of " * " could solve it...
I have no other track, I'm not Access sql developper, I usually play around Sql server/Oracle/mySql, hope it helped. ;)
Try to change your like this way:
... Like '*" & tblsearchengine07.parolachiave & "*'))"
The like statement go into the WHERE clause.
If you do want to use LIKE without you care about caps letters, then you can use it like this:
LIKE COLUMN_NAME = '%WhatYouLike%'
My suggestion is:
Use a table variable (#Table) with a unique/primary key coming from the table to be updated.
SELECT all the data to be updated (you can add the like statement here) and then INSERT that in the created table variable.
Construct the UPDATE statement with an INNER JOIN to the table variable matching with the unique/primary key.
I know this may take a lot of steps but believe me these are more efficient than using a black list approach.

Brackets in Access Query Using ConcatRelated Function

I'm attempting to concatenate multiple records of field "dbo_race.racedesc" into one record based on username. I'm attempting to use the ConcatRelated function (http://allenbrowne.com/func-concat.html). As the username is text, I followed the article's example to include extra quotes.
The error I get is "Error 3126: Invalid bracketing of name '[dbo_indrace.username]'."
The error seems to be referring to the period within the brackets. However, if I only list the field without the table name, I am prompted that it could refer to more than one table listed in my FROM clause.
What would be the correct syntax for the ConcatRelated function?
SELECT DISTINCT dbo_ind.username,
ConcatRelated("[racedesc]", "[dbo_race]", "[dbo_indrace.username] = """ & [dbo_ind.username] & """") AS racedescription
FROM dbo_race INNER JOIN (dbo_ind INNER JOIN dbo_indrace ON dbo_ind.username = dbo_indrace.username) ON dbo_race.race = dbo_indrace.race
WHERE dbo_race.lang='E';
Try changing [dbo_indrace.username] to [dbo_indrace].[username]
Same with [dbo_ind.username]

SQL concatenation inside VBA string

I'm using VBA excel 2003,SQL 2005 to make a sql query call and inside my sql statement I'm using '+' operator to concatenate two strings.
dim query as string
query = "Select distinct ', '+emailaddress1 "
query = query & "from contact "
would this work inside vba? My query returns too many records in excel but not in SQL?
Please just focus on this 2 lines of code and not worry about the rest of my sql call, I'm just wondering whether or not this specific string would work?
Your code will return a column where each row would be an email address with a comma in front of it. If that is what you want, then yes, it will work.
If, on contrary, you want a single string where all email addresses would be listed, separated with commas, that'd be
query = "declare #foo varchar(max);"
query = query & "select distinct #foo = isnull(#foo,'') + emailaddress1 + ', ' from contact;"
query = query & "select left(#foo, len(#foo)-2);"