类似于sql中的SELECT *但只有一行

时间:2011-08-16 14:47:17

标签: sql

当我们SELECT * FROM table时,我们获取表中的所有记录,如果我只想获得一行但不知道列数

id   att1  att2  att3.... attx
-------------------------------
1     45     5      3        7

如何执行返回所有列的select语句? 我知道我必须使用

from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'myTable'

和Where子句:WHERE id = 1

但我不知道列数...

3 个答案:

答案 0 :(得分:1)

*表示您想要所有列,而不是所有“记录”。

select *
from YourTable
where ID = 1

答案 1 :(得分:1)

只需在@sql语句的末尾添加一个where子句,将您的选择限制为所需的行:

declare @cols nvarchar(max)

select @cols = coalesce(@cols + ', ' + column_name, column_name)
from information_schema.COLUMNS
where TABLE_NAME = 'mytable'

declare @sql nvarchar(max)
select @sql = 'select ' + @cols + ' from myTable where Id = 1'

exec sp_executesql @sql

答案 2 :(得分:0)

如果您有兴趣了解所有列的详细信息,则无需知道列数。只需将where条件设为select *