Multiline SQL Query Error in Excel

  • Thread starter Thread starter Arvid Qvigstad
  • Start date Start date
A

Arvid Qvigstad

How do you get MS Excel to accept an SQL query that spans
multiple lines? If I put the query on a single line, it
runs. If I split it to multiple lines, Excel only read
the first line, then gives an error like "Missing
expression" or "no FROM statement found", etc.

Here is how I would like to write the query:

SELECT CompanyName, ContactName
FROM customers
WHERE CompanyName > 'g'
AND ContactName > 'g'

Here is how Excel expects it:

SELECT CompanyName, ContactName FROM customers WHERE
CompanyName > 'g' AND ContactName > 'g'

I have a long query and Excel splits it to 2 lines, then
gives and error because it can't find the 2nd line.
 
Arvid Qvigstad said:
How do you get MS Excel to accept an SQL query that spans
multiple lines? If I put the query on a single line, it
runs. If I split it to multiple lines, Excel only read
the first line, then gives an error like "Missing
expression" or "no FROM statement found"

How do you get *Excel* to accept SQL?! Whatever, make sure your white
space still allows the SQL text to concatenate properly. Although I
more usually see trailing spaces in these ngs, I prefer leading spaces
because they are easier to spot.

strSql3 = ""
strSql3 = strSql3 & "SELECT employee_ID, surname, forname1,"
strSql3 = strSql3 & " forname2, dob, is_developer, hourly_rate"
strSql3 = strSql3 & " FROM " & strTable & ";"

For the same reasons, I indent (with trailing spaces) when posting SQL
e.g.

SELECT
employee_ID, surname, forname1,
forname2, dob, is_developer, hourly_rate
FROM
MyTable
;

Jamie.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top