1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Microsoft SQL Server Ways to use string as table name in query [duplicate]

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 4, 2024 às 07:52.

  1. Stack

    Stack Membro Participativo

    The usual way to use string as table name in MS SQL query is to create query as a string and then use the command: exec sp_executesql. Question is: is there a way to avoid that? My query is overly complicated to turn it into proper string and execute later. Trouble is I have a table name which is a string, like:

    declare @table_name nvarchar(100) = 'sometable';


    and I have quite complicated query where table name is used. This is an example since my query is too complex:

    select a, b, c
    from table1 inner join @table_name on a = a1
    where ... some conditions


    Is there another way besides using exec sp_executesql?

    Continue reading...

Compartilhe esta Página