Create Table # TABLE (Num int, name1 nvarchar (10), name2 nvarchar (10) Create Table # Table1 (Num int, name1 nvarchar (10 )) insert into # Table1 select 1, 'A' Union all select 2, 'B' CREATE TABLE # Table2 (num1 int, name2 nvarchar (10) insert into # Table2 select 1, 'A' Union all select 2, 'bb' declare @ sqlstr nvarchar (1000) -- set @ sqlstr = 'select. *, B. name2 from # Table1 as a inner join # Table2 as B on. num = B. num1 'insert into # Table exec (@ sqlstr) Select * from # tabledrop table # table1drop table # table2drop table # Table
According to the above method, if there is such a situation:
1. Data is queried by a combination of multiple temporary tables to form a large table. Data in a large table, such as a primary key, cannot be duplicated.
2. The column fields in the temporary table are not fixed and may be randomly generated.
Solution:
1. Use @ SQL to dynamically create a new large table, which contains the dynamic column fields to be generated.
2. Use dynamic SQL combined query syntax.
3. Execute the query syntax using exec to load the result set to the big table.