Is it possible to select column data using the ordinal_position for a table column? I know using ordinal positions is a bad practice but for a one-off data import process I need to be able to use the ordinal position to get the column data.
So for example
create table Test(
Col1 int,
Col2 nvarchar(10)
)
instead of using
select Col2 from Test
can I write
select "2" from Test -- for illustration purposes only
Yes, you could do this with some really ugly hits into the system tables. You'd probably need to fall into the world of dynamic sql.
I really, really do not recommend this approach.
If that didn't deter you, then this might get you started (ref):
You'd have to do something like
If you know quantity of columns, but don't know its names and types, you can use following trick:
As a result, you will have a table with 2 columns [C1] and [C2]. This method is not very usefull if you have 100 columns in your table, but it works well for tables with small predefined number of columns.
I don't know of any way to do this short of using dynamic SQL. Maybe if you include a bit more information about why you feel you have to use the ordinal values someone on here can give you advice on how to get around that problem.
EDIT: I see that you answered this to some degree in another comment. Can you provide more specifics? The import proc and/or table definitions?
If you know the number of columns, one way might be to transfer the data into a tmp table with that number of columns and select from the temp table...
An option you have is using conditions: In your example:
Going to schema will slow query I am afraid...