Find all columns with the same column name (regardless of type and accuracy) from different tables. Note that the number 3 in the last column is the number of tables to be queried.
Select a. Name, count (1) as TT from syscolumns A, sysobjects B
Where a. ID = B. ID and B. xtype = 'U' and B. Name in ('table _ 1', 'table _ 2', 'table _ 3 ')
Group by A. Name
Having count (1) = 3;
After the same columns are obtained from different tables, the results returned after the same columns are removed from each table.
Create view test
As
Select a. Name, count (1) as TT from syscolumns A, sysobjects B
Where a. ID = B. ID and B. xtype = 'U' and B. Name in ('table _ 1', 'table _ 2', 'table _ 3 ')
Group by A. Name
Having count (1) = 3;
Go
Select a. Name as column name
From syscolumns A, sysobjects B
Where B. xtype = 'U' and A. ID = B. ID and B. Name in ('table _ 1') and A. name not in (Select name from test)
Group by B. Name, A. Name having count (1) = 1
Drop view test
Original post address: How to query the same columns in N tables and different columns in each table (N is not fixed and the table structure is unpredictable)