简体   繁体   English

从SQL Server 2008中的所有表中选择所有列

[英]Select all columns from all tables in SQL Server 2008

How can I Select all columns from all tables from the DB, like: 如何从数据库中选择所有表中的所有列,例如:

Select * From * 

in SQL Server 2008??? 在SQL Server 2008中???

The table list it´s very very big, and have so many columns, is it possible to do it without writing the column names? 表列表非常大,并且有很多列,是否可以在不编写列名的情况下执行此操作?

Or maybe make a select that returns the name of the tables. 或者可以创建一个返回表名称的选择。

This SQL will do this... 这个SQL会这样做......

DECLARE @SQL AS VarChar(MAX)
SET @SQL = ''

SELECT @SQL = @SQL + 'SELECT * FROM ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES

EXEC (@SQL)
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID where t.name = 'ProductItem'  AND C.name like '%retail%'
ORDER BY schema_name, table_name 

Try this, works fine 试试这个,工作正常

SELECT * FROM INFORMATION_SCHEMA.COLUMNS 

then you could add 然后你可以添加

WHERE TABLE_NAME LIKE '' AND COLUMN_NAME LIKE ''

It is possible to retrieve the name of all columns from sys.columns 可以从sys.columns中检索所有列的名称
It is possible to retrieve the name of all table from sys.tables 可以从sys.tables中检索所有表的名称

But is impossible to retrieve all the data from all the tables. 但是无法从所有表中检索所有数据。 As soon as more than one table is involved in a query, a JOIN is necessary. 只要查询中涉及多个表,就需要JOIN。 Unless join conditions are provided, tables are joined as full Cartesian product, meaning each row from each table is matched with each row from ll other tables. 除非提供了连接条件,否则表将作为完整的笛卡尔积连接,这意味着每个表中的每一行都与来自其他表的每一行匹配。 Such a query as you request would produce for 10 tables with 10 records each no less than 10e10 records, ie. 您请求的此类查询将为10个表生成10个记录,每个记录不少于10e10记录,即。 100 billion records. 1000亿条记录。 I'm sure you don't want this. 我相信你不想要这个。

Perhaps if you explain what you what to achieve, not how , we can help better. 也许如果你解释你要实现什么 ,而不是如何 ,我们可以更好地帮助你。

To select * from each table, one after another, you can use the undocumented but well known sp_msforeachtable: 要从每个表中逐个选择*,您可以使用未记录但着名的sp_msforeachtable:

sp_msforeachtable 'select  * from ?'

If you are going to send to Excel, I would suggest you use the export wizard and simply select all the tables there. 如果您要发送到Excel,我建议您使用导出向导,只需选择那里的所有表。 In the object browser, put your cursor on the database name and right click. 在对象浏览器中,将光标放在数据库名称上,然后单击鼠标右键。 Chose Tasks - Export Data and follow the wizard. 选择任务 - 导出数据并按照向导进行操作。 WHy anyone would want an entire database in Excel is beyond me, but that's the best way. 任何人都希望Excel中的整个数据库超出我的范围,但这是最好的方法。 If you need to do it more than once you can save the export in an SSIS package. 如果您需要多次执行此操作,可以将导出保存在SSIS包中。

In SQL Server 2016 Management Studio ( Version: 13.0.15900.1), to get all column names in a specified table, below is the syntax: 在SQL Server 2016 Management Studio(版本:13.0.15900.1)中,要获取指定表中的所有列名,请使用以下语法:

   **Select name from [YourDatabaseName].[sys].[all_columns] 
   where object_id=(Select object_id from [YourDatabaseName].[sys].[tables] 
   where name='YourTableName')**

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

相关问题 如何通过避免在所有表中重现的某些列从SQL Server 2008中的多个表中选择列名 - How to select column names from multiple tables in SQL server 2008 by avoiding certain columns which reoccur in all tables SQL Server 2008:查找包含具有指定名称的列的所有表 - SQL Server 2008: find all tables containing columns with specified name SQL从多个表中选择所有列 - SQL Select all columns from multiple tables 选择*不返回所有列 - Coldfusion / SQL Server 2008 - Select * not returning all columns - Coldfusion / SQL Server 2008 SQL Server 2008基于列值选择表的所有列+来自同一表的列 - SQL Server 2008 Select all columns of a table + a column from the same table based on a column value SQL Server 2008 Standard,获取包含所有列和列中可能数据的所有表 - SQL Server 2008 Standard, get all tables with all columns and possible data within the colums 更快从SQL Server 2008 R2中的数据库中全选 - Faster Select all from databases in SQL Server 2008 R2 SQL从两个或多个不相关表中选择所有列 - SQL Select all columns from two or more Unrelated tables 在2列上选择distinct,但从SQL Server返回所有列 - Select distinct on 2 columns, but return all columns from SQL Server SQL选择包含字符串的所有表的所有列的所有行 - SQL Select all rows on all columns on all tables that contain a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM