Search code examples
mysqlvariablestoaduser-defined

MySQL : using user-defined variables for a table name


I would like to use a user-defined variable in the FROM part of a query like in the example below :

SET @year = 2013,
    @ID3_1 = 107001001,
    @TableSource = "S_EXPO_RISQUES_METEO";

SELECT @ID3_1,
       @year,
       TS.ID_TER,
       TS.VALUE 
FROM @TableSource TS;

How can I produce the equivalent result with an other statement ?


Solution

  • Try this to get an equivalent without using variables.

    SELECT 107001001 ID3_1,
           2013 year,
           TS.ID_TER,
           TS.VALUE 
    FROM S_EXPO_RISQUES_METEO TS;
    

    Or this with variables:

    SET @year = 2013,
        @ID3_1 = 107001001;
    
    SELECT @ID3_1,
           @year,
           TS.ID_TER,
           TS.VALUE 
    FROM S_EXPO_RISQUES_METEO TS;