Technical Article

Returning data with col number

,

This script enables you to return data from SQL Server's table without specyfing the name of the column .All you only need to supply is the column number, that you want the data from. In the example we want to chose all rows from the second column (categories table - northwind) Column number is based on colorder from the syscolumns of every database.

-----------------------------------
----Michal Krawczyk
----Returning data with col number
-----------------------------------

CREATE PROCEDURE return_with_number 
@table varchar(30), 
@col_num int 
AS
DECLARE @sql varchar(60) 
DECLARE @number1 varchar(30) 
SELECT @number1 = COL_NAME(OBJECT_ID(@table), @col_num) 
SET @sql = 'select ' + @number1 + ' from ' + @table 
EXEC (@sql)


For example:

USE northwind
EXEC return_with_number 'categories',2

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating