i get an uppercased table name calling a variable

75 Views Asked by At

I set a variable name for a table as lowercase:

Set TGT_TABLE = 'tab_name';

however when i try to call it:

select * from table($TGT_TABLE);

I get an error:

SQL compilation error: Object 'TAB_NAME' does not exist or not authorized.

Why is it calling the uppercase version of my table name Also I veriefied in the 'show variables' that my variable is indeed lowercase

2

There are 2 best solutions below

0
On

You can Write Your Code Like This :

declare @TGT_TABLE nvarchar(50),@query nvarchar(50)

Set @TGT_TABLE = 'name of your table';

SET @query = 'SELECT * FROM ' + @TGT_TABLE;
     
EXEC(@query);

It will worked.

0
On

in order to use lower case identifiers, these must be in double-quotes. in order to assign it to a variable, this must additionally be in single quotes.

@jarlh 's answer is correct, note the single quotes surrounding the double quotes.

Set TGT_TABLE = '"tab_name"';
select * from table($TGT_TABLE);

if in doubt, copy/ paste