Using column number inplace of column name in SQL Select statement

Using column number inplace of column name in SQL Select statement

Post by Sande » Thu, 17 Jul 2003 17:31:15



Hello All,

Is there a way to run sql select statements with column numbers in
place of column names in SQLServer.

Current SQL ==> select AddressId,Name,City from Address

Is this possible ==> select 1,2,5 from Address

Thanks in Advance,
-Sandeep

 
 
 

Using column number inplace of column name in SQL Select statement

Post by Anith Se » Thu, 17 Jul 2003 17:36:28


There is no column numbers in relational tables. And in SQL you must use
column names in your SELECT statement. One can come up with meaningless
workarounds using COL_NAME with Dynamic SQL or using a t-SQL view. (Search
the archives & you can find some).

--
- Anith
( Please reply to newsgroups only )

 
 
 

Using column number inplace of column name in SQL Select statement

Post by Cole Shelto » Thu, 17 Jul 2003 18:36:49


If you look in INFORMATION_SCHEMA.Columns you will see an ordinal_position
field which is the position of the column in the table.  You could get the
column names and build a dynamic query.  Why you would want to do this, I
have no idea.  Give us an idea of what you are trying to do and maybe we can
help you find a better solution.

Cole


Quote:> There is no column numbers in relational tables. And in SQL you must use
> column names in your SELECT statement. One can come up with meaningless
> workarounds using COL_NAME with Dynamic SQL or using a t-SQL view. (Search
> the archives & you can find some).

> --
> - Anith
> ( Please reply to newsgroups only )

 
 
 

1. Using a Variable as Column name in Select statement in a Cursor

Hi,

I'm trying to have the Column as a variable in my Select statement in
my Cursor, but I can't get the SQL-server to understand

Server: Msg 207, Level 16, State 3, Line 7

Using the variable other places - like in the Where clause - works
fine!

Any good tips and tricks are warmly welcome!

Thanx

Here's the code:

-- The Modelid are the same as the Column name I'm trying to use in my
Select statement



DECLARE Column_cur CURSOR FOR

column name


OPEN Column_Cur


        BEGIN

        PRINT 'There was a TRUE value in the table/column'
        END

CLOSE Column_Cur
DEALLOCATE Column_Cur

2. SQLMail on SQLSERVER 2000

3. ORDER BY column names vs column numbers

4. SQL Server Driver Backward Compatibility

5. SELECTING Columns by Column number

6. DB Connection String

7. Single Column to Multiple column Select Statement

8. a problem with AUTO-INCREMENT

9. new column specifying row number from select statement?

10. select * statement to return only column names?

11. Getting column name in Select statement

12. Returning column names is a select statement

13. Column Name Formating logic in select statement