0

I need to create a LEFT JOIN with another server in SQL Server, and the information to what the server would be is inside table Servidor. So, I've tried to use the s.dns and s.banco as the information for the linked server information, but it doesn't work that way. Is there any way I'd be able to set s.dns and s.banco dynamically like my snippet?

INNER JOIN 
    [Server].[dbo].[Servidor] s ON cli.idServidor = s.idServidor
LEFT JOIN 
    [s.dns].[cli.banco].[dbo].[RecepcaoModulo] rm 
           ON r.codigo_modulo = rm.codigo 
           AND r.latitude = rm.latitude 
           AND r.longitude = rm.longitude 
           AND MAX(rm.TimeStampModulo)
4
  • 2
    Use Dynamic SQL. Construct your SQL statement as a string that you run via sp_executesql Commented Mar 20 at 18:59
  • 1
    see dba.stackexchange.com/questions/145590/… Commented Mar 20 at 19:01
  • 1
    You can't even query linked server from a variable, even less a table row! Commented Mar 20 at 19:02
  • You can't even use a local table or column like this. SQL, the language, is essentially strongly-typed and the types are the tables and columns. Those MUST be known when the query gets compiled to an execution plan. The database needs to know whether the tables are local or not, the column types, whether they have indexes or not, and even the data statistics, to decide whether to use indexes or not, which join strategies to use, whether it needs to create intermediate copies of the data Commented Mar 21 at 11:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.