0

Is it possible to write a query:

SELECT * from table1, table 2, table 3 ... 

where table1.column name like table2.column name like table3.column name

We don't know the name of our column, we say any of our columns which are in table1.column name like table2.column name in like table3.column name

For example: employee id maybe is a column that is common to 3 tables, or publisher id is a column that is common to 3 tables.

I can find the foreign key or primary key with a diagram in my database, but I want to write a query that shows just columns which have a same title as column name.

3 Answers 3

1

No clue as to why you are doing this, so a bit hard to steer, but there are a number of tables and views in the database that describe the database.

Information_Schema.Columns is probably where you should start. Note this is meta data, if you want the content of the actual tables you'll have to go a step further, and build some dynamic sql, or equivalent.

Select Column_Name,Count(*) As NumberOfOccurences From Information_Schema.Columns will give you the column names and how often you've used them for instance.

Sign up to request clarification or add additional context in comments.

Comments

1

If you know the column name and want to know in which different table same column name is used then write SQL query as

SELECT schema_name(t.[schema_id]) + '.' + t.[name] AS Tables FROM sys.tables t INNER JOIN sys.columns c on t.[object_id] = c.[object_id] WHERE c.[name] = 'Your_Common_Column_Name'

You will get all the table list with their respective schema

Comments

0

To get column names:

select sc.name
from sysobjects so inner join syscolumns sc on so.id = sc.id
where so.name = 'tableName'

3 Comments

As of SQL Server 2005, it's better to use the new views in the sys schema, e.g. sys.columns and sys.tables
thank you HMariD,your syntax show all column name in one table,but my question is just show the same column name in 3 table with their data. something like that union.
user1940864. You have to construct the sql to do something like that e.g. Exec('Select * From ' + @TableName) where you've got TableName from some query on the schema.

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.