简体   繁体   English

从SQL表中获取数据和列名

[英]Get both data and column name from sql table

I know how to get the column names from a table 我知道如何从表中获取列名

SELECT Column_name
from Information_schema.columns 
where Table_name like 'summary'

And I know how to get the data like below 而且我知道如何获取如下数据

SELECT * FROM [summary] where Id='12345";

but how do I combine these so that I atleast get column names of a table even if there is no data? 但我怎么组合这些,使我得到ATLEAST表的列名,即使没有数据?

Thanks in advance. 提前致谢。

EDIT__________________________________________________________ 编辑__________________________________________________________

table  
id    name
123    test
12345   test2

in the above example after I have run this combined query I expect the result to be 在上面的示例中,运行组合查询后,我期望结果是

id  12345
name test2

but if I run the query with an id like "hello" 但是如果我使用“ hello”之类的ID运行查询

then the result shall be 那么结果应该是

id
name

First Query Use exact column name to find 第一个查询使用确切的列名查找

SELECT Column_name
FROM Information_schema.columns 
WHERE Table_name LIKE 'Columns'

Second query: 第二个查询:

SELECT * FROM [summary] WHERE Id='12345";

Union all the both queries like below: 合并所有两个查询,如下所示:

SELECT Column_name
FROM Information_schema.columns 
WHERE Table_name LIKE 'IND'

UNION ALL

SELECT * FROM [summary] WHERE Id='12345";

What columns are returned when you execute a t-sql Select statement depends on your Select statement, data inside the columns does not affect the number columns being returned from your select statement. 执行t-sql Select语句时返回的列取决于您的Select语句,列中的数据不影响从select语句返回的列数。

SELECT Column1 , Column2 , Column3  --<-- COlumn names 
FROM Table_Name  

In above query regardless of what data is present in columns Column1 , Column2 and Column3 the query will return all three columns. 在上面的查询中,无论列Column1,Column2和Column3中存在什么数据,查询都将返回所有三列。

So your assumption that query should return a column even if no data exists is wrong. 因此,您认为即使没有数据存在,查询也应返回列的假设是错误的。

You can either Explicitly use the column names in your select statement that you want to return like in above mentioned statement, or you can make use of * instead of using column names which will return all the column present in that table regardless of if there is any data stored in them or not. 您可以像上述语句一样在要返回的select语句中显式使用列名,也可以使用*而不是使用列名来返回该表中存在的所有列,而不管是否存在是否存储在其中的任何数据。

SELECT *
FROM Table_Name

IF you want to column names of a table when there is no data then you used like this 如果要在没有数据的情况下对表的名称进行列,则可以这样使用

IF EXISTS(SELECT * FROM [summary] where Id='12345') 
BEGIN
    SELECT * FROM [summary] where Id='12345'
END  
ELSE
BEGIN
    SELECT Column_name
    FROM   Information_schema.columns 
    WHERE  Table_name like 'summary'
END

I think using simple CASE here would be enough: 我认为在这里使用简单的CASE就足够了:

SELECT 
   case when id is null then '' else id end ,  
   case when name is null then '' else name end
FROM [summary] where Id='hello';

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

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