-2

I would like to ask how to declare a table in MySQL. I tried using this code but I am getting the error below:

DECLARE @TBL VARCHAR(20)
SET @TBL = 'A171107001';

select APPLICATION, count(JOB_NAME) as 'JOB COUNT' from @TBL

Error Message:

Java::JavaSql::SQLException: Must declare the table variable "@TBL".: DECLARE @TBL VARCHAR(20)

Yee
  • 32
  • 7
  • @B001 she wants to declare a table variable – Valerica Nov 09 '17 at 10:55
  • 1
    **Which** SQL? MySQL, Oracle, SQL Server, or something else? Please [edit] your post to tag it. @Valerica No, though not worded clearly, the code shows that the question is how to select from a table having a dynamic name, which is taken from a variable. Once the DBMS is tagged, it will be trivial to mark this as a duplicate. – underscore_d Nov 09 '17 at 10:55
  • Table names can't be passed as parameters like that. – jarlh Nov 09 '17 at 10:57
  • It's MySQL. Edited my question. Its not a duplicate question, I like to call a table in mysql. – Yee Nov 09 '17 at 11:01
  • You're asking how to `select` from a table whose name you get from a variable. That is precisely what the other question asks. If others are interpreting the wrong meaning from your question, that's because you're not explaining it well. – underscore_d Nov 09 '17 at 11:02
  • 1
    Your wording asks how to create a table variable. Your code asks how to select from a table whose name is stored in a variable. Choose one. And see why you need to tag your DBMS at the outset? Three people have posted off-topic answers, falsely assuming you're using MS SQL Server. – underscore_d Nov 09 '17 at 11:09

2 Answers2

0

in sqlserver at least

declare @l_table varchar(100) = 'a'
declare @l_sql varchar(100) = 'select count(*) from '+@l_table
exec (@l_sql)
Ab Bennett
  • 1,391
  • 17
  • 24
-1

Your question is not clear. But this is how you declare a Table Variable in T-SQL:

DECLARE @TBL AS TABLE (
JOB_NAME VARCHAR(20)
)
INSERT INTO @TBL VALUES ('A171107001')

select JOB_NAME from @TBL

After your edit I could see you are using MySQL.

Anyways, you are asking how to declare a table variable and it seems you are expecting how to select from a certain table, which it's name comes from a variable.