简体   繁体   English

创建动态 SQL 查询以连接多个表?

[英]Creating Dynamic SQL Query to join multiple tables?

I have a dataset in SQl which includes huge numbers of tables (more than 2000).我在 SQl 中有一个数据集,其中包括大量表(超过 2000 个)。 Within these tables, I have a main table called 'main_table', that includes some parameters and one of them is the key named 'id'.在这些表中,我有一个名为“main_table”的主表,其中包含一些参数,其中一个是名为“id”的键。 I have also a table called 'Tables_name', which consists of the name of the tables (around 500 tables name) that I want to join to the main table (main_table).我还有一个名为“Tables_name”的表,其中包含我要加入主表 (main_table) 的表的名称(大约 500 个表名称)。 Each table has two parameters (id and value).每个表都有两个参数(id 和 value)。

Basically I want to left join all the tables (in the dataset) which their names are in 'Tables_name', based on their 'id'.基本上我想根据他们的'id'加入他们名字在'Tables_name'中的所有表(在数据集中)。 I need a query that automatically join those tables (and creat a new table having all parameters from the main_table and values of those joint tables) based on thier key which is 'id'.我需要一个查询,该查询根据它们的键“id”自动连接这些表(并创建一个新表,其中包含 main_table 中的所有参数和这些联合表的值)。 They all have the 'id' as the key parametrs.它们都将“id”作为关键参数。

   main_table:
     id, name, date, category, age, brand, number 

   Tables_name:
              tablxcd
              tableasd
              tablefgd
                  ..
                  ..
                  ..
   tablxcd:
           id,value
   tablasd:
           id,value
   tablegd: 
           id,value

My desired table should be like this:我想要的表应该是这样的:

   output_table:
               id, name, date, category, age, brand, number, tablxcd_value, tablasd_value, ...

It should be left join, because there might be some ids which are not matched for all tables, so in the output table the values of those rows which are not matched should be zero.应该是左连接,因为可能有一些id不匹配所有表,所以在output表中,那些不匹配的行的值应该为零。 I have tried this:我试过这个:

 SELECT mn.Id,mn.name
  ,mn.Date
  ,mn.brand
  ,mn.category
  ,xcs.value as tablxcd_value
  ,asd.value as tablasd_value
  FROM [mydatabase].[dbo].[main_table] mn LEFT JOIN [mydatabase].[dbo]. 
 [tablxcd] xcs ON mn.Id=xcd.Id
 LEFT JOIN [mydatabase].[dbo].[tablasd] asd ON asd.Id=mn.Id 
 where mn.Date > '2018-01-01' 

But this is manual, and some how it is not possible to put all the 500 tables' name here.但这是手动的,有些不可能将所有 500 个表的名称都放在这里。

This will assemble a dynamic query from a list of tables.这将从表列表中组装一个动态查询。 It assumes that Tables_name has an IDENTITY.它假定 Tables_name 具有 IDENTITY。 If it does not, then create a temp table with an IDENTITY and use it instead.如果没有,则创建一个带有 IDENTITY 的临时表并改用它。 It also assumes that the tables names do not contain special characters or use reserved words.它还假定表名称不包含特殊字符或使用保留字。

DECLARE @qry NVARCHAR(MAX), @strJoins NVARCHAR(MAX) = '', @strColumns NVARCHAR(MAX) = ''
DECLARE @i SMALLINT = 1, @imax SMALLINT = (SELECT COUNT(*) FROM dbo.tables_name)

WHILE @i <= @imax
BEGIN

SELECT @strColumns = @strColumns + CHAR(10)  + ', ' + name + '.value as ' + name + 'value' FROM dbo.tables_name WHERE Id = @i
SELECT @strJoins = @strJoins + CHAR(10) + 'LEFT JOIN ' + name + ' ON ' + name + '.Id = main.Id' FROM dbo.tables_name WHERE Id = @i

SET @i += 1
END

SELECT @qry = 'SELECT main.* ' + @strColumns + CHAR(10) + 'FROM main ' + @strJoins

PRINT @qry

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM