Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a column without its name in sql server?

Tags:

sql

sql-server

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

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.

like image 758
Ehsan Pakravan Avatar asked Apr 06 '13 07:04

Ehsan Pakravan


1 Answers

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.

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.

like image 112
koriander Avatar answered Nov 15 '22 00:11

koriander