Simplify a Select

zequion 446 Reputation points
2024-09-21T01:21:29.91+00:00

I have many databases where each one has the book tables and there is also a "Books (Libros)" table that contains details of each book such as the Subject (Tema), Author, and Editorial. These fields are linked via LEFT JOIN to their corresponding tables that are common to all databases.

I would like to simplify the SQL code because it can be very long and is always repetitive.

I would like to summarize it to:

SELECT TABLE1 FROM BASE1, TABLE2 FROM BASE2
LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Temas ON [Libros].IdTema = Temas.IdTema 
LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Autores ON [Libros].IdAutor = Autores.IdAutor 
LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Editoriales ON [Libros].IdEditorial = Editoriales.IdEditorial 
WHERE [Libros].Estado IS NULL OR [Libros].Estado != 'X'

Real:

SELECT 'Esp Libros en Español' as Database_Name, * 
FROM [Esp Libros en Español].[dbo].[Libros]
LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Temas ON [Libros].IdTema = Temas.IdTema 
LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Autores ON [Libros].IdAutor = Autores.IdAutor 
LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Editoriales ON [Libros].IdEditorial = Editoriales.IdEditorial 
WHERE [Esp Libros en Español].[dbo].[Libros].Estado IS NULL OR [Esp Libros en Español].[dbo].[Libros].Estado != 'X'

(continue)

UNION ALL SELECT 'Esp BOE - Agricultura, Ganadería, Pesca y Alimentación' as Database_Name, * 
FROM [Esp BOE - Agricultura, Ganadería, Pesca y Alimentación].[dbo].[Libros] 
LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Temas ON [Libros].IdTema = Temas.IdTema 
LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Autores ON [Libros].IdAutor = Autores.IdAutor 
LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Editoriales ON [Libros].IdEditorial = Editoriales.IdEditorial 
WHERE [Esp BOE - Agricultura, Ganadería, Pesca y Alimentación].[dbo].[Libros].Estado IS NULL OR [Esp BOE - Agricultura, Ganadería, Pesca y Alimentación].[dbo].[Libros].Estado != 'X'
ORDER BY Temas.Tema, Titulo
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,817 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rodger Kong 190 Reputation points
    2024-09-21T03:16:01.0133333+00:00

    I think this script will be more clear and easy to maintain, but might not such simple like what you want, you can try it to see the result and performance.

    WITH [Libros]
    AS
    (
    --SERVER 1
    	SELECT 'Esp Libros en Español' as Database_Name, *
    	FROM [Esp Libros en Español].[dbo].[Libros] AS [L]
    	WHERE [L].Estado IS NULL OR [L].Estado != 'X'
    	UNION
    --SERVER 2
    	SELECT 'Esp BOE - Agricultura, Ganadería, Pesca y Alimentación' as Database_Name, *
    	FROM [Esp BOE - Agricultura, Ganadería, Pesca y Alimentación].[dbo].[Libros] AS [L]
    	WHERE [L].Estado IS NULL OR [L].Estado != 'X'
    --	UNION
    --  ALL OTHER SERVERS
    )
    SELECT * FROM 
    [Libros]
    LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Temas ON [Libros].IdTema = Temas.IdTema 
    LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Autores ON [Libros].IdAutor = Autores.IdAutor 
    LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Editoriales ON [Libros].IdEditorial = Editoriales.IdEditorial 
    

2 additional answers

Sort by: Most helpful
  1. Rodger Kong 190 Reputation points
    2024-09-21T03:11:17.4633333+00:00

    I think this will be more clear and easy to maintain, but looks not like what you want, you can try it to see the result and performance:

    WITH [Libros]
    AS
    (
    --SERVER 1
    	SELECT 'Esp Libros en Español' as Database_Name, *
    	FROM [Esp Libros en Español].[dbo].[Libros] AS [L]
    	WHERE [L].Estado IS NULL OR [L].Estado != 'X'
    	UNION
    --SERVER 2
    	SELECT 'Esp BOE - Agricultura, Ganadería, Pesca y Alimentación' as Database_Name, *
    	FROM [Esp BOE - Agricultura, Ganadería, Pesca y Alimentación].[dbo].[Libros] AS [L]
    	WHERE [L].Estado IS NULL OR [L].Estado != 'X'
    --	UNION
    --  ALL OTHER SERVERS
    )
    SELECT * FROM 
    [Libros]
    LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Temas ON [Libros].IdTema = Temas.IdTema 
    LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Autores ON [Libros].IdAutor = Autores.IdAutor 
    LEFT JOIN [Server\MSSQLSRV].[Libros].[dbo].Editoriales ON [Libros].IdEditorial = Editoriales.IdEditorial 
    

  2. LiHongMSFT-4306 27,016 Reputation points
    2024-09-23T01:53:59.8733333+00:00

    Hi @zequion

    You may consider using temporary tables to deal with complex datasets in this scenario.

    After splitting complex queries into temporary tables, this can simplify logic and make it easier to optimize queries.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.