MySQL Forums
Forum List  »  Newbie

Using a global variable in a DECLARE statement
Posted by: Steve Emmett
Date: August 20, 2021 01:38PM

I create a table every month that is composed of the table name (precincts) and the date of the data file that will populate that table. So, for instance, the table name for 15 Aug 2021 would be precincts_20210815. This table name is assigned to the global variable @voteprecincts.

I have no problem creating and populating these tables. The issue comes when I try to use the table name in the declaration of a cursor. The "normal" declaration would be something like:

DECLARE pcursor CURSOR FOR SELECT district, precinct from precincts_20210815.

But I want to generalize the code so I can use it each month without having to explicitly enter the table name. Instead, I would like to do:

DECLARE pcursor CURSOR FOR SELECT district, precinct from @voterprecincts.

This doesn't work. So I tried doing prepared statements a couple different ways:

SET @_ = CONCAT("DECLARE pcursor CURSOR .....", @voterprecincts);

or

SET @_ = CONCAT("SELECT district, precinct .. ", @voterprecincts);

and a couple other variations

I quess I have three questions:

1) can you use a global variable in a DECLARE statement?
2) can you use a global variabe in a cursor DECLARE statement?
3) if you can - HOW?!

Thanks

Options: ReplyQuote


Subject
Written By
Posted
Using a global variable in a DECLARE statement
August 20, 2021 01:38PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.