Solved

how to pass dynamic name of table into query

Posted on 2013-11-11
8
269 Views
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

ex:
SELECT * from ACTIVITIES a
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?
0
Comment
Question by:portlight
  • 4
  • 2
  • 2
8 Comments
 
LVL 24

Accepted Solution

by:
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');
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Open in new window

0
 

Author Comment

by:portlight
ID: 39640676
Can this be called in the same manner a stored procedure is called?
0
 
LVL 24

Expert Comment

by:chaau
ID: 39640692
Just put this code to a stored procedure and make @LogID a parameter of this stored procedure. Then, just call the procedure
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39653499
Are you using a scripting language such as PHP?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:portlight
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.

CREATE PROCEDURE reportNameTest (
        IN groupId varchar (50),
        IN logId varchar (50),
        IN fromDate varchar (50),
        IN toDate varchar (50))

    BEGIN
            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,g.name AS groupName,l.logId,l.name AS logName, i.itemID,
      i.name AS itemName, le.userName,completed, i.optimalMin,i.optimalMax ,le.value,
      u.name AS unitDescription,
      g1.parentId AS parentId1, g1.name AS group1Name,g2.parentId AS parentId2, g2.name AS group2Name,
      g3.parentId AS parentId3, g3.name 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 //

DELIMITER ;

Open in new window

0
 

Author Comment

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

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

DELIMITER ;

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

0
 
LVL 108

Expert Comment

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

Author Comment

by:portlight
ID: 39654977
no using straight mySql query
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now