Home > Enterprise >  i get an uppercased table name calling a variable
i get an uppercased table name calling a variable

Time:10-07

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related