T
Ted Allen
Hi Stephanie,
Are you writing this query in Access? The syntax looks
different (like SQL Server?). I say this partly because
you are using the % symbol for the wildcard, but
Access/Jet uses the * symbol.
I think you are getting an error referencing EXISTS
because AFAIK that is the only time you can use
subqueries that return multiple fields.
The EXISTS reserved word is used with subqueries
basically as a boolean that will return true if the
subquery returns any records, and false if not.
Therefore it would normally be used to set the condition
for a boolean field (to be honest I never really use
EXISTS).
Unless used with IN (or NOT IN) or EXISTS (or NOT
EXISTS), Access expects only one value for one field to
be returned by a subquery. When using IN (or NOT IN)
Access expects one or more values for a single field.
I get the feeling that the EXISTS statement won't really
be useful in your case, rather I think it is just a case
of needing to adjust your sql syntax to be compatible
with JET, which means breaking up your criteria to
compare directly with the individual fields in your query.
For instance, it looks like the FENAME field condition
could be:
<> " " AND NOT LIKE "*ABANDONED*"
For length it looks like you are trying to restrict the
updates to only those records where the length is equal
to the max length for a particular FENAME. If that is
the case, you need to use a correlated subquery to
restrict the domain of the query such as the following:
(SELECT Max(LENGTH) FROM rail_sw AS VT WHERE VT.FENAME =
rail_sw.FENAME)
You could also use Dlookup to achieve this, but I prefer
subqueries and they are often faster.
So, if I can put this together without any typos it would
look something like:
UPDATE rail_sw
SET fetype = 1
WHERE rail_sw.FENAME <> " " AND rail_sw.FENAME NOT
LIKE "*ABANDONED*" AND rail_sw.LENGTH = (SELECT Max
(LENGTH) FROM rail_sw AS VT WHERE VT.FENAME =
rail_sw.FENAME)
A couple of other notes, the comparison to the string
with one space, this will only restrict the query from
updating records where the FENAME exactly equals one
space. If you are trying to eliminate blank fields you
may want to add the condition IS NOT NULL, and if you
want to eliminate empty strings, you may want the
condition <>"", or you could combine these by using Nz
(FENAME,"")<>""
HTH, Ted Allen
another column. The query is:
main query's FROM clause. Revise the SELECT statement of
the subquery to request only one field.
3306 and to contact Microsoft Product Support Services
for more information.
the update?
Are you writing this query in Access? The syntax looks
different (like SQL Server?). I say this partly because
you are using the % symbol for the wildcard, but
Access/Jet uses the * symbol.
I think you are getting an error referencing EXISTS
because AFAIK that is the only time you can use
subqueries that return multiple fields.
The EXISTS reserved word is used with subqueries
basically as a boolean that will return true if the
subquery returns any records, and false if not.
Therefore it would normally be used to set the condition
for a boolean field (to be honest I never really use
EXISTS).
Unless used with IN (or NOT IN) or EXISTS (or NOT
EXISTS), Access expects only one value for one field to
be returned by a subquery. When using IN (or NOT IN)
Access expects one or more values for a single field.
I get the feeling that the EXISTS statement won't really
be useful in your case, rather I think it is just a case
of needing to adjust your sql syntax to be compatible
with JET, which means breaking up your criteria to
compare directly with the individual fields in your query.
For instance, it looks like the FENAME field condition
could be:
<> " " AND NOT LIKE "*ABANDONED*"
For length it looks like you are trying to restrict the
updates to only those records where the length is equal
to the max length for a particular FENAME. If that is
the case, you need to use a correlated subquery to
restrict the domain of the query such as the following:
(SELECT Max(LENGTH) FROM rail_sw AS VT WHERE VT.FENAME =
rail_sw.FENAME)
You could also use Dlookup to achieve this, but I prefer
subqueries and they are often faster.
So, if I can put this together without any typos it would
look something like:
UPDATE rail_sw
SET fetype = 1
WHERE rail_sw.FENAME <> " " AND rail_sw.FENAME NOT
LIKE "*ABANDONED*" AND rail_sw.LENGTH = (SELECT Max
(LENGTH) FROM rail_sw AS VT WHERE VT.FENAME =
rail_sw.FENAME)
A couple of other notes, the comparison to the string
with one space, this will only restrict the query from
updating records where the FENAME exactly equals one
space. If you are trying to eliminate blank fields you
may want to add the condition IS NOT NULL, and if you
want to eliminate empty strings, you may want the
condition <>"", or you could combine these by using Nz
(FENAME,"")<>""
HTH, Ted Allen
for a group of records based on the maximum value of-----Original Message-----
Hello World,
I am writing an update query that changes a column value
another column. The query is:
one field without using the EXISTS reserved word in theUPDATE rail_sw
SET fetype = 1
where (FENAME, Length) in
(SELECT FENAME, MAX(LENGTH)
FROM rail_sw
WHERE FENAME <> ' ' AND
FENAME NOT LIKE '%ABANDONED%'
GROUP BY FENAME)
When I try running the query, the following message pops up:
You have written a subquery that can return more than
main query's FROM clause. Revise the SELECT statement of
the subquery to request only one field.
additional information I get is that this is an ErrorWhen I select Help in the pop-up window, the only
3306 and to contact Microsoft Product Support Services
for more information.
how I can use it in conjunction with the subquery to runCan anyone tell me about this EXISTS reserved word and
the update?