查询来自SQL Server的选择列 [英] query for select columns from sql server

查看:226
本文介绍了查询来自SQL Server的选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从sql server中的表中选择随机列
(未指定列名称)

How to select random columns from table in sql server
(columns name not specify)

推荐答案

您无法轻松完成-尽管可以这样做,但没有直接按其序号访问列的机制通过查询information_schema表以将序数转换为列名.您仍然必须在SQL外部执行随机数位或构建一些动态SQL来执行此操作.

讨厌执行,讨厌维护.在外部应用程序中维护表格或字典非常容易得多,该表格或字典将数字转换为SQL列并以这种方式获取它们.但是,您想要这样做可能意味着您的SQL数据库设计是错误的,并且可能有一种更好的方法可以完全做到这一点.您想达到什么目的,为什么您会认为这是一个很好的解决方案?
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?


您可以通过动态查询来实现

例如:-
you can do it by dynamic query

example:-
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

exec uspSelectData @FieldNames ='colum1,colum2',@TableName='urtableName'



您可以查看并在1 to 210
中给列Alias Number 您可以生成每次随机数和数字,并将其传递给语句
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
SELECT CONVERT(INT, (210+1)*RAND())-- return Random int


希望它能给您带来想法
最好的问候
米特瓦里(M.Mitwalli)


Hope it gives you idea
Best Regards
M.Mitwalli


这篇关于查询来自SQL Server的选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆