strSelect = "Select lastname, firstname, midinitial AS NameFull, casefind from"
strSelect = strSelect & " (SELECT tabVeteran.lastname, tabVeteran.firstname, tabVeteran.midinitial, tabVeteran.casefind"
strSelect = strSelect & " FROM tabVeteran"
strSelect = strSelect & " WHERE tabVeteran.lastname Like " & strSearch
strSelect = strSelect & " UNION"
strSelect = strSelect & " SELECT tabVeteran.lastname, tabVeteran.firstname, tabVeteran.midinitial, tabVeteran.casefind"
strSelect = strSelect & " FROM tabVeteran"
strSelect = strSelect & " WHERE tabVeteran.firstname Like " & strSearch & " ORDER BY NameFull ASC);"
Unfortunately, I get the following error message:
Message="The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified."
Remove ORDER BY NameFull ASC from the derived table.
Your code should be
select coalesce(FirstName + ' ','') + coalesce(MidInitial + ' ','') + coalesce(LastName,'') as NameFull, CaseFind
from (select ... UNION select ..) X ORDER BY NameFull -- take a look at X alias for derived table
Beware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth)
Cincinnati_L...
Member
11 Points
25 Posts
Please proof my SQL Select Statement
Jul 21, 2010 05:09 PM|LINK
Here's my SQL Statement
Unfortunately, I get the following error message:
Message="The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified."
Thank you
SQL 2008 EE select statement
-Cincinnati Lin :)
jackym
Participant
872 Points
141 Posts
Re: Please proof my SQL Select Statement
Jul 21, 2010 07:19 PM|LINK
Here you go. the change is at the line 08
sql
snop222
Member
360 Points
81 Posts
Re: Please proof my SQL Select Statement
Jul 22, 2010 02:17 AM|LINK
I think the problem is the "ORDER BY NameFull ASC". Because the "NameFull" is not refered in sub-SELECT clause. It should be "ORDER BY midinitial ASC"
In your SELECT statement, it do "UNION" for the same tabel only for different "WHERE". So, actually you could use single SELECT, such as :
If you want to "repeat" the rows which both matches these two "WHERE" codition, then you should use "UNION ALL". Such as :
Naom
All-Star
36004 Points
7901 Posts
Re: Please proof my SQL Select Statement
Jul 22, 2010 04:28 AM|LINK
Remove ORDER BY NameFull ASC from the derived table.
Your code should be
(Donald Knuth)
Visit my blog
Microsoft Community Contributor 2011-12