We help IT Professionals succeed at work.

Database query using MFC

slim asked
I have an SQL query to carry out, but one of the column names has a space in it. I managed to get an SQL query to work in Java by putting the Column name in double quotes. This does not seem to work in MFC. My query is as follows:

m_strCmdText = _T("select top 100 * from tbl_persons where Surname LIKE '"+Surname+"' AND \"Given name\" LIKE '"+GivenName+"'");

My query works if I just use Surname. I have tried using single quotes, but that doesn't work either.
Any ideas?
Chris Walker
Watch Question

I have found that no matter what MS database technology I use it never allows spaces in the column names. You will have to remove all the spaces.  This is strange since Access will allow you to create a column(field) with spaces in the name but you can't programatically make queries with a space in the name.

My 2 cents...
Have you tried brackets around the column name?
"[given name]"
My DoFieldExchange of the test app put the name in brackets
void test::DoFieldExchange(CFieldExchange* pFX)
     RFX_Text(pFX, _T("[Given Name]"), m_Given_Name);
     RFX_Text(pFX, _T("[LAST Name]"), m_LAST_Name);
     RFX_Long(pFX, _T("[ROW_ID]"), m_ROW_ID);

So, I'm assuming that the query would use them, too.
m_strCmdText.Format("select top 100 * from tbl_persons where Surname LIKE '%s' AND [Given name] LIKE '%s'",
Surname, GivenName);
I messed up and learned something new 2day... ;-)

My 2 cents...

Explore More ContentExplore courses, solutions, and other research materials related to this topic.