Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Select Column Index in Transact-SQL

Posted on 2005-03-17
12
Medium Priority
?
3,307 Views
Last Modified: 2012-08-13
Hello,
Does anybody know of a way to select the columns based on the column index in transact-sql.  Much like if you were going to select a row from a resultset in asp or java, like myResultSet("column_1") or myResultSet(0)<--This is what I want.  I'm trying to stay away from building a sql string in a stored procedure and executing it, because I know it has to compile the string statement before it executes, and one of the benefits of a stored procedure is that its already compiled.  

The reason is the user wants to be able to view specific rows based on numeric data being passed in.  For an example, if the user passes in 1,3,5, I would only select the first column index, the third column index and the fifth column index.  

Thanks for your anticipated help...
0
Comment
Question by:ispcorp
  • 5
  • 4
  • 3
12 Comments
 
LVL 2

Author Comment

by:ispcorp
ID: 13565966
specific columns I mean...
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1600 total points
ID: 13566036
There is no direct way.  You could lookup the column names yourself and build a query string.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 13566059
Note, however, you would have to use a table variable / temp table / cursor because the colids can have gaps, so you have to retrieve all columns up to the last one indicated.  That is, if 1, 3, 5 were requested, you would have to retrieve the first 5 column names and select the appropriate ones not just the ones with colids of 1,3,5 because the ids may have gaps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:ispcorp
ID: 13566106
What do you mean by gaps on the colids?
0
 
LVL 21

Expert Comment

by:Kevin3NF
ID: 13566195
1,2,3 has no gaps
1,3,4 has a gap in the numbering (colid 2 is missing)
0
 
LVL 2

Author Comment

by:ispcorp
ID: 13566301
Well, I think the best way to go about this is to select the results of sp_column into a temp_table, and start a loop of those results building a string as I go.  If the number is not charindex() > 0 then I will skip over that particular row.  Finally it wil execute the string.

Each number is predefined on a particular row, which makes my life alot simpler.  

What do you guys think.  Better way?
0
 
LVL 21

Assisted Solution

by:Kevin3NF
Kevin3NF earned 400 total points
ID: 13566395
Yep...that is pretty much where ScottPletcher was indicating....seems like a good route :-)
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 13566531
Yes.  You can also use INFORMATION_SCHEMA.columns view instead of sp_columns and save yourself a little trouble :-) .

I would just use syscolumns, but that's because I'm stubborn :-) .
0
 
LVL 21

Expert Comment

by:Kevin3NF
ID: 13566591
Aw man...those points were not mine...all I did was concur with Scott's answer (sorry Scott...wasn't trying to poach yer points....)
0
 
LVL 2

Author Comment

by:ispcorp
ID: 13566624
I'm just trying to be fair...
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 13566629
Not a problem at all ... it was a good confirmation, and you just got "confirmation" points, not like you got half or anything.  [I think I can spare a few points :-) :-) ].
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 13566646
ispcorp:

I agree; I think it was fair too.  [But I don't think that half, for example, would have been fair.]
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

572 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