how to pass dynamic name of table into query

Posted on 2013-11-11
Last Modified: 2013-12-13
I have a database that keeps a table logs - each log id will be numeric
There are matching tables to join on that are NAMED log + the numeric logId

I need to be able to join these tables based on a log id that will be passed

INNER JOIN (LOG + LogID) l2 ON a.activityID = l2.activityID

The 'logID' will be passed as a paramater
SO if logID is 100 I am looking for table log100

How can I do this?
Question by:portlight
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
LVL 25

Accepted Solution

chaau earned 500 total points
ID: 39640668
You need to use MySQL prepared statements. The syntax in your case will be similar to this:

PREPARE stmt USING CONCAT('SELECT * from ACTIVITIES a INNER JOIN LOG', @LogID, ' l2 ON a.activityID = l2.activityID');

Open in new window


Author Comment

ID: 39640676
Can this be called in the same manner a stored procedure is called?
LVL 25

Expert Comment

ID: 39640692
Just put this code to a stored procedure and make @LogID a parameter of this stored procedure. Then, just call the procedure
Webinar: MariaDB® Server 10.2: The Complete Guide

Join Percona’s Chief Evangelist, Colin Charles as he presents MariaDB Server 10.2: The Complete Guide on Tuesday, June 27, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

LVL 110

Expert Comment

by:Ray Paseur
ID: 39653499
Are you using a scripting language such as PHP?

Author Comment

ID: 39654718
I have not ever done a stored proc like this before. However this database design is already in place and I need to work with it as is.

Here is the basic stored procedure. I need to add this inner join to this stored proc....If someone can help me by showing me this would look after added I would appreciate it.

        IN groupId varchar (50),
        IN logId varchar (50),
        IN fromDate varchar (50),
        IN toDate varchar (50))

            DECLARE logInS varchar (50);
            DECLARE groupIdS varchar (50);
            DECLARE startDate varchar (50);
            DECLARE endDate varchar (50);

             SET logInS   = CASE logId   WHEN '-1' THEN '%' ELSE logId END;
             SET groupIdS = CASE groupId WHEN '-1' THEN '%' ELSE groupId END;
             SET startDate = FROM_UNIXTIME(fromDate/1000);
             SET endDate = FROM_UNIXTIME(toDate/1000);
 SELECT g.groupId, AS groupName,l.logId, AS logName, i.itemID, AS itemName, le.userName,completed, i.optimalMin,i.optimalMax ,le.value, AS unitDescription,
      g1.parentId AS parentId1, AS group1Name,g2.parentId AS parentId2, AS group2Name,
      g3.parentId AS parentId3, AS group3Name,i.ordering
  FROM logExceptions le
      INNER JOIN logs l ON l.logID = le.logID
      INNER JOIN groups g ON g.groupId = l.groupId
      INNER JOIN items i ON le.itemId = i.itemId
      LEFT JOIN units u ON i.unitId = u.unitId
      LEFT JOIN groups g1 ON g.parentId = g1.groupId
      LEFT JOIN groups g2 ON g1.parentId = g2.groupId
      LEFT JOIN groups g3 ON g2.parentId = g3.groupId
        WHERE  l.logID LIKE logInS
            AND g.groupID LIKE groupIdS
            AND le.completed
        BETWEEN startDate
            AND endDate;
    END //


Open in new window


Author Comment

ID: 39654898
I have gone back to the drawing board with this query.
New query
USE vorpaldev;

DROP PROCEDURE IF EXISTS vorpaldev.searchLogId2$$
CREATE DEFINER = 'root'@'%'
PROCEDURE vorpaldev.searchLogId2 (userId varchar(300))
  SET userId = CONCAT("log", userId);
  SET @statment = "Select *      
                     from @userId
  PREPARE stmt FROM @statment;
  SET @a = userId;
  EXECUTE stmt USING @a;


Open in new window

To call I am using
CALL searchLogId2 (131)

Open in new window

What I want the end results to be is
Select * from log131

Open in new window

LVL 110

Expert Comment

by:Ray Paseur
ID: 39654918
Are you using a scripting language such as PHP?

Author Comment

ID: 39654977
no using straight mySql query

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question