ispcorp
asked on
Select Column Index in Transact-SQL
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...
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...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
What do you mean by gaps on the colids?
1,2,3 has no gaps
1,3,4 has a gap in the numbering (colid 2 is missing)
1,3,4 has a gap in the numbering (colid 2 is missing)
ASKER
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?
Each number is predefined on a particular row, which makes my life alot simpler.
What do you guys think. Better way?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :-) .
I would just use syscolumns, but that's because I'm stubborn :-) .
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....)
ASKER
I'm just trying to be fair...
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 :-) :-) ].
ispcorp:
I agree; I think it was fair too. [But I don't think that half, for example, would have been fair.]
I agree; I think it was fair too. [But I don't think that half, for example, would have been fair.]
ASKER