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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cursor name already in use

Status
Not open for further replies.

Toman

Technical User
Mar 30, 2004
190
CZ
SQL query sends results into cursor named tmp. If at the moment some table with name tmp is open, error message “Alias name is already in use” appears, regardless to “SAFETY” setting. In given context existing tmp table could be closed or even deleted.

I understand that:
- well behaved programmer destroys unused tmp cursors immediately,
- some unique name for cursor can be used.
- is possible to check whether tmp exists before SQL query and use other name
Nevertheless I’d like to know, how professionals are dealing with a such situation.
Thanks, Tom
 
Yes it is to be expected that if you already have a data table/cursor TMP in USE, then you will get that error message.

The question is, do you need the first TMP table/cursor?

If so, they I'd suggest that you use some other table/cursor name (maybe Tmp2) for the return results of your SQL Query.

If not, then you can do something like:
Code:
* --- [I]check whether tmp exists[/I] ---
IF USED('Tmp')
   SELECT Tmp
   USE
ENDIF

SELECT <whatever> FROM <wherever> INTO CURSOR TMP

Good Luck,
JRB-Bldr

 
Tom,

Your analysis is correct, but it's never been a big problem for me.

I use a naming convention, which includes the prefix "csr" for cursor names. Since I never use that with tables, I never see a clash.

That said, a naming convention can never be a guarantee that clashes won't occur. Perhaps using SYS(2015) to generate a unique cursor name would be a good approach. But then you'd still need an alias, and that might still clash with a table name (unless you did something tricky like storing the generated cursor name in a variable).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Well, Mike,

that's exactly what I do all the time:

Local lcTempalias
lcTempalias = "_"+Sys(2015)

select ... into cursor (lcTempalias)
or
sqlexec(..,lcTempalias)

Tidying up mostly is done via
Use In Select(lcTempalias)

and if that is forgotten or there is a reason to keep such a cursor open longer, it does not hurt in regard to free workareas (there is no limit anymore) or filehandles and cursors will finally be closed with the datasession.

Bye, Olaf.
 
Gentlemen,

I thank you for your opinions.

I have realized, that that bloody tmp sitting in my workarea was a table. Being it a cursor with the same name it would be overwritten without any error message.
Tom
 
If you don't use an existing table's name as alias you wouldn't have such a problem. SQL select automatically destroy open cursor and open new one with same alias. You can even select from a cursor into same cursor (alias) like:

select * from tmp where blahblah into cursor tmp

Cetin Basoz
MS Foxpro MVP, MCP
 
Just a little code efficiency hint

this
Code:
IF USED('Tmp')
   SELECT Tmp
   USE
ENDIF

Can be written as
Code:
IF USED('Tmp')
   Use in TMP
ENDIF

I'm 99% sure their is a way to do it in one line without a macro but I can't remember it.
 
Yes, alan92rttt
It is:
Code:
USE IN SELECT('Tmp')
very nice piece of code (not from my head but published here earlier).
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top