?
Solved

Select Column Index in Transact-SQL

Posted on 2005-03-17
12
Medium Priority
?
3,297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 2

Author Comment

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

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 69

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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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 69

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 69

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 69

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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

800 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