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
John EsraeloDatabase / SQL DeveloperAsked:
Who is Participating?
 
maggiec58Commented:
declare @query varchar(500)

set @query = (select 'Select '+ (select column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'MyTable' and ORDINAL_POSITION = 1) +', ' +
(select column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'MyTable' and ORDINAL_POSITION = 2) +
' from MyTable order by 2,1')

execute sp_sqlexec @query
0
 
tim_csCommented:
No, that isn't possible.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
John EsraeloDatabase / SQL DeveloperAuthor Commented:
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
0
 
John EsraeloDatabase / SQL DeveloperAuthor Commented:
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
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
0
 
John EsraeloDatabase / SQL DeveloperAuthor Commented:
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
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0
 
John EsraeloDatabase / SQL DeveloperAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.