Link to home
Start Free TrialLog in
Avatar of John Esraelo
John EsraeloFlag for United States of America

asked on

SQL: TSQL:: statement

I need to be able to "select"  records using table's columns' ordinal position, is this possible?

select 1, 2 from MyTable order by 2, 1


thx

JohnE
Avatar of tim_cs
tim_cs
Flag of United States of America image

No, that isn't possible.
Avatar of Jim Horn
Nope.  You can ORDER BY ordinal positions, not SELECT BY.

-- This works
SELECT id, name
FROM YourTable
ORDER BY 1, 2

-- This does not work
SELECT 1, 2
FROM YourTable
ORDER BY 1, 2
ASKER CERTIFIED SOLUTION
Avatar of maggiec58
maggiec58
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John Esraelo

ASKER

excellent method.. I was doing almost the same thing except that I was trying to build the string and then use sp_executesql and was not able to get the field name..
I like your method because incorporates 2 processes into one.
I will be right back with results..

thx

JohnE
Thank you much.. it worked nicely..
There is a bit of a difference between yours and mine.. I am just getting the fieldname..
and then passing it to another USP for the final process.
awesome!


JohnE
Hi jesraelo

I'm not challenging your awarding of points, but I'm curious as to what your requirements are that you would want to call only the ordinal positions of columns, and not the column names themselves?

Reason I ask is I haven't come across this before, and would like to know your situation in case I come across something similar.

Thanks.
Jim
To be honest with you this is my first time that have encountered a DB set of tables in this fashion.
Few hundred tables are prefixed xyz and then suffixed by a 3 or 4 digit int
example:  xyz453
Therefore, in order to identify which table I need to pull the data from I will have to have a process for that and then go get the value(s) that I need.
Now, all those tables have the same number of columns and the PK is named the same.. however, the value that I am looking for is in a field name that is NOT the same in all those tables..
for instance.
xyz001       MyKey, charcurr, status
xyz002        MyKey, intNum , status
so on..
therfore, I would need to get the field name from information_schema since I cannot use ordinal positioning in my query..
I hope I made sense..
it is crazy, I know..

JohnE
>Few hundred tables are prefixed xyz and then suffixed by a 3 or 4 digit int
I've only experienced this once, and that was when I was granted access to Great Plains / Dynamics back-end, with no previous GP experience, and no developer docs.   Eventually I was able to beg/borrow/threaten/Google my way to something useful that identified the correct tables.

>Now, all those tables have the same number of columns and the PK is named the same
Wow, that's just super-developer-friendly...

Makes sense.  Good luck.
If memory serves, there's a couple of companies out there that have proprietary SQL Server back-end apps with some front-end, where it gets installed and supported at clients sites, but they scramble the hell out of the the object names such that nobody can replicate it.
This is an imaging system, so every document has 1 or more "keyword"s associated with.
Each keyword has a table and in the table the PK is referred to a scanned document / image.
so, for instance, if there are 10 different INVOICE related type documents then there is ONE keyword for the $$$   the invoice amount, right..
then, this means that there is a one table called KeyItemXXXX where the XXXX is the PK to the Invoice Amount keyword as part of the table name and the content ::
key, value, status...   where the key is the document primary key and the value is the $$$..
good grief.. hehehe

thank you for your patience..
thx

JohnE