Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to select random columns from table in sql server
(columns name not specify)
Posted
Comments
Prasad_Kulkarni 19-Jun-12 3:21am    
Why you want to do this?
and what does this means?
Please elaborate.
bhartimithilesh 19-Jun-12 3:34am    
I am want to show some columns from database , I have 210 table again and again do not need to right

You can't do it easily - there is no mechanism for accessing columns by their ordinal number directly, although you could do it by querying the information_schema table to convert the ordinals to column names. You would still have to do the random number bit outside SQL or build some dynamic SQL to do it.

Nasty to implement, nasty to maintain. Much, much easier to maintain a table or dictionary in your external app which translates numbers to SQL columns and fetch them that way. However, that you want to do this may mean that your SQL database design is wrong and there may be a better way to do it altogether. What are you trying to achieve, and why do you think that this would be a good solution?
 
Share this answer
 
you can do it by dynamic query

example:-
SQL
create proc uspSelectData
@FieldNames varchar(max)=null
@TableName varchar(30)=null
as 
begin
declare @Qry nvarchar(max)

set @Qry='Select '+ @FieldNames +'  from '+ @TableName + ' 
EXECUTE  (@Qry)
end  

call it as
SQL
exec uspSelectData @FieldNames ='colum1,colum2',@TableName='urtableName'
 
Share this answer
 
Hi ,
You can make view and Give your columns Alias Number from 1 to 210
and you can generate every time random and number and you can pass it to your statement
SQL
SELECT CONVERT(INT, (210+1)*RAND())-- return Random int

Hope it gives you idea
Best Regards
M.Mitwalli
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900