Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Statement confusion 1

Status
Not open for further replies.

Scott24x7

Programmer
Jul 12, 2001
2,813
11
38
JP
Hi All,
Trying to do something that I perceive as "simple" but I can't get the syntax in the SQL statement to work.
So, I have two tables, which have name values, but different field names, and in different tables. What I want to achieve is a single cursor that has the contents (distinctly) in one common field (called TESTNAMES). So my attempted syntax looks like this:

Code:
 SELECT DISTINCT CONTACTFIRSTNAME, UNION SELECT DISTINCT FNAME FROM CONTACT, CTFNAMES INTO CURSOR TESTNAMES

I had also tried:

Code:
 SELECT DISTINCT CONTACTFIRSTNAME, FNAME FROM CONTACT, CTFNAMES INTO CURSOR TESTNAMES

The first example fails, the second example gives me the cursor named TESTNAMES, but two columns, where first column has all the distinct values from CONTACT repeated with the second column that has the distinct values from CONTACT.

What am I doing wrong here?



Best Regards,
Scott
ATS, CDCE, CTIA, CTDC

"Everything should be made as simple as possible, and no simpler."[hammer]
 
The syntax in your first example is incorrect. When you use a UNION, each part of the statement must be a syntactically-correct SELECT in its own right. In particular, it must have its own FROM clause. So the following would be correct:

Code:
SELECT DISTINCT ContactFirstName FROM Contact ;
  UNION SELECT Fname FROM CtfNames ;
  INTO CURSOR etc.

However, just because that has the correct the syntax, I can't say for sure that it will do what you want, as I am not certain I understand what you are trying to achieve. But give it a try anyway.

As for your second example, that definitely won't work, Where you have multiple table names in the FROM clause, but without a WHERE condition, you will get a cartesian join, which I feel sure is not what you want.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike,
Awesome, that was it... I made a quick adjustment to UPPER() both of the clauses so that capitalization issues didn't result in two results the same. That worked perfect after that!
Many thanks.


Best Regards,
Scott
ATS, CDCE, CTIA, CTDC

"Everything should be made as simple as possible, and no simpler."[hammer]
 
Yeah, I added the AS TESTNAMES after each of the two FROM clauses so that the cursor combined it to a single field name (instead of Exp_1 as default returned), and then didn't have to modify any of my downstream code as the field names then matched. So in all it worked perfectly.


Best Regards,
Scott
ATS, CDCE, CTIA, CTDC

"Everything should be made as simple as possible, and no simpler."[hammer]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top