[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL:  TSQL:: statement

Posted on 2012-08-20
10
Medium Priority
?
367 Views
Last Modified: 2012-08-20
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
0
Comment
Question by:John Esraelo
10 Comments
 
LVL 15

Expert Comment

by:tim_cs
ID: 38312979
No, that isn't possible.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38312982
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
 
LVL 1

Accepted Solution

by:
maggiec58 earned 1000 total points
ID: 38313468
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

Author Comment

by:John Esraelo
ID: 38313532
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
 

Author Comment

by:John Esraelo
ID: 38313555
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
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38313574
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
 

Author Comment

by:John Esraelo
ID: 38313842
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
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38313920
>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
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38313968
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
 

Author Comment

by:John Esraelo
ID: 38313976
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question