SQL 如何在sql server中选择没有名称的列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15848415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select a column without its name in sql server?
提问by Ehsan Pakravan
how to
如何
select name,family from student where name="X"
without its column name.
没有它的列名。
for example :
例如 :
select "column1","column2" from student where "column1"="x"
or for example
或者例如
select "1","2" from student where "1"="x"
"1" is column1 "2" is column2
“1”是第 1 列 “2”是第 2 列
i dont want to say columns name. i want to say just its number or other.... idont tired from select *. but it just for that i dont know the columns name but i know where they are. my columns name are change every i want to read the file but its data are same, and the data are in the same columns in each file.
我不想说列名。我只想说它的数字或其他.... 不厌倦选择 *。但这只是因为我不知道列名称,但我知道它们在哪里。我的列名称每次我想读取文件时都会更改,但其数据相同,并且数据位于每个文件的相同列中。
回答by koriander
Although you can not use field positions specifiers in the SELECT statement, the SQL standard includes the INFORMATION_SCHEMA where the dictionary of your tables is defined. This includes the COLUMNS view where all the fields of all tables are defined. And in this view, there is a field called ORDINAL_POSITION which you can use to assist in this problem.
尽管您不能在 SELECT 语句中使用字段位置说明符,但 SQL 标准包括 INFORMATION_SCHEMA,其中定义了表的字典。这包括 COLUMNS 视图,其中定义了所有表的所有字段。在此视图中,有一个名为 ORDINAL_POSITION 的字段,您可以使用它来帮助解决此问题。
If you query
如果你查询
SELECT ORDINAL_POSITION, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TABLE'
ORDER BY ORDINAL_POSITION
then you can obtain the column names for the ordinal positions you want. From there you can prepare a SQL statement.
然后你可以获得你想要的序数位置的列名。您可以从那里准备 SQL 语句。
回答by Philippo
You could use temp table as:
您可以将临时表用作:
DECLARE @TB TABLE(Column1 NVARCHAR(50),...)
INSERT @TB
SELECT * FROM student
Then use it:
然后使用它:
SELECT Column1 FROM @TB WHERE Column1='aa'
回答by Z. Khullah
It's just not possible. Unfortunately, they didn't think about table-valued functions, for which information_schema is not available, so good luck with that.
这是不可能的。不幸的是,他们没有考虑表值函数,因为它没有 information_schema,所以祝你好运。
回答by éric Bergeron
If it's a string you can do this :
如果它是一个字符串,你可以这样做:
Select Column1 + '' From Table
If it's a number you can do this :
如果它是一个数字,你可以这样做:
Select Column1 + 0 From Table
If it's a datetime you can do this :
如果是日期时间,您可以这样做:
Select dateadd(d, 0, Column1) From Table
And similarly for other data types..
对于其他数据类型也类似..
回答by highwingers
No, you can not use the ordinal (numeric) position in the SELECT clause. Only in Order by you can.
不,您不能在 SELECT 子句中使用序数(数字)位置。只有在您可以订购。
however you can make your own column alias...
但是您可以创建自己的列别名...
Select Column1 as [1] From Table
回答by aIKid
You can use alias:
您可以使用别名:
SELECT name AS [1], family AS [2] FROM student WHERE name="X"